• Polymorphing different Types


    So my friend (though not good enough friend to be my ‘boatfriend’) ran into a programming issue the other day at work. He had two objectswith similar data and no behavior (vb6 style data/procedural programming). Unfortunately,these two objects did NOT inherit from the same base object and they were beingused similarly in an asp.net code behind page but there was no clearabstraction.

     

    If(foo.GetType() == typeof(recurringTransaction))

    {

    Foo1();

    }

    Else

    {

    Foo2();

    }

     

    So basically “foo” could be one of two different types… arecurring transfer or a single transfer. Depending on the type, we’d get adifferent UI behavior.

     

    This is clearly an opportunity for polymorphism, but we didNOT have control of these objects! They were vendor code.

     

    So what do you do in this situation?

     

    Well directly to the point, you would create a new baseclass called “Transfer” and then use composition to encapsulate the dataobjects. Making something like this:

     

    What’s that look like in code?

         class OldSingleTransfer

        {

            //Some data

        }

        class OldRecurringTransfer

        {

            //Some similiar data

        }

        abstract class Transfer

        {

            public abstract void MyUIBehavior();

        }

        class SingleTransfer : Transfer

        {

            OldSingleTransfer instance;

            public override void MyUIBehavior()

            {

                //Specific single transfer code

            }

        }

        class RecurringTransfer : Transfer

        {

            OldRecurringTransfer instance;

            public override void MyUIBehavior()

            {

                //specific recurring transfer code

            }

        }

     Now you can just do foo.MyUIBehavior(); and be done with it.

     I know what you're thinking... GOD that's a LOT of code just to do this one thing. That's not how i would do this.

    Well, yes you're right this is more code initially... however in reality what we had was 40-100 lines of 'type specific' code that was mostly all repeated for both types. We've eliminated all of that duplication and left ourselves with an easier set of operations to maintain and reduced the complexity by removing the conditional logic.

    There were bugs lurking in this code... for example what would happen if you made a third type of transfer? it would have hit the "else" condition... meaning "single transfer". What if it was a scheduled transfer?

    Full story

    Comments (7)

  • Init Params in Silverlight 3

    I converted my silverlight app over to silverlight 3 today and ran into an issue with InitParams because the silverlight hosting control is no longer valid for silverlight 3 nor provided in the vs2008 tools.

     

    The quick and dirty: I had to take all of the object tag string and move it to the code behind by inserting into the inner Html of the div tag that hosts it.

     

    Explanation:

     

    When you create a new Silverlight 3 application it asks you if you want to add it to an existing asp.net application. If you do that it generates a test aspx page that has something like this:

     

        <div id="silverlightControlHost">

            <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%"height="100%">

                <param name="source"value="ClientBin/JPeckham.xap"/>

                <param name="onError"value="onSilverlightError"/>

                <param name="background"value="white"/>

                <param name="minRuntimeVersion"value="3.0.40624.0"/>

                <param name="autoUpgrade"value="true"/>

                <href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0"style="text-decoration:none">

                        <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get MicrosoftSilverlight" style="border-style:none"/>

                </a>

              </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>

    What you need to do is change the div to be runat=”server” and do a CUT of the object tag contents. You’ll have this then:

    <div id="silverlightControlHost"runat="server">

    </div>

     

    Now inside of your code behind for the page, past that string and parse it using a string builder, like this:

    silverlightControlHost.InnerHtml =

    silverlightControlHost.InnerHtml =

                new StringBuilder(

                @"<object id=""SilverlightApp"" data=""data:application/x-silverlight-2,""

    type=""application/x-silverlight-2"" width=""100%"" height=""100%"" >

                  <param name=""source"" value=""../../ClientBin/JPeckham.xap""/>

                  <param name=""onError"" value=""onSilverlightError"" />

                  <param name=""background"" value=""white""/>

                  <param name=""minRuntimeVersion"" value=""3.0.40624.0"" />

                  <param name=""autoUpgrade"" value=""true""/>

              <param name=""InitParams"" value=")

     

                                                .Append(BuildSilverlightInitParametersFromQueryString())

               

                                                .Append( @"""/>

                  <a href=""http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0""style=""text-decoration:none"">

                        <img src=""http://go.microsoft.com/fwlink/?LinkId=108181"" alt=""Get Microsoft Silverlight"" style=""border-style:none""/>

                  </a>

              </object><iframe id=""_sl_historyFrame"" style=""visibility:hidden;height:0px;width:0px;border:0px""></iframe>").ToString();

     

    NOTE: Basically BuildSilverlightInitParametersFromQueryString()is the method that constructs your InitParams value string. So write thathowever you feel like. Make sure toHtmlEncode it! J

    Full story

    Comments (3)

  • POST data is missing in the POST

    So I was working on this WCF client to talk to the facebook REST api and ran into a really strange issue.

    If i utilized the client to call getLoggedInUser it would work when called from an ASP.NET page code behind but it would fail "POST data is missing in the POST" when I tried to call it from within my WCF service host.

    For clarification, I have a WCF service that I created and within that service I am calling over to facebook using my WCF REST client.

    Basically the client tier is the web browser.
    the server tier is the web server with my wcf service and my code behind file.

    the server tier uses a facebook WCF REST client to talk to facebook api.

    //end of clarification :)

    Anyhow so I fired up Fiddler and sure enough, every call from my wcf service over to facebook was being sent as a POST! not a GET.

    In the end i had to change my code to this:

     

            public long getLoggedInUser(string sessionKey)

            {

                Dictionary<string, string> args = CreateArgsDictionary();

                args.Add("method","users.getLoggedInUser");

                args.Add("session_key",sessionKey);

                args.Add("call_id",DateTime.Now.Ticks.ToString());

                stringsig = new Signature(args).ToString();

                stringqueryString = string.Format

                    ("?method=users.getLoggedInUser&api_key={0}&sig={1}&v=1.0&call_id={2}&session_key={3}"

                    , args["api_key"], sig,args["call_id"],args["session_key"]);

               

                //HACK:using this instead of WCF.

                varrequest = WebRequest.Create(channelFactory.Endpoint.Address.Uri.AbsoluteUri+ queryString);

        

                using(var response= request.GetResponse()) 

                {

                    varxs = new XmlSerializer(typeof(users_getLoggedInUser_response));

                    return(xs.Deserialize(response.GetResponseStream())as users_getLoggedInUser_response).value;

                }

               

            }

    Full story

    Comments (0)

  • Duplex Service in Silverlight

    I'm writing a silverlight app for facebook and I came across a couple of blogs that were extremely helpful on the subject of duplex services in silverlight.


    then this one helped know what to put in the web.config/app.config to setup the service host

    Possibly more to come as I figure out how to implement some of this.

    --UPDATE: i tried this out and it just locks up. Never got it working. Plus it's polling so it's probably not scalable so useless for what I'm doing.

    Full story

    Comments (0)