Compartir a través de


Next Steps – Windows Phone

It has been a bit quiet here, sorry to disappoint my many avid readers Smile.

The project is built and deployed and in use, and I took my life back for a while and hence haven’t documented everything I would like to.  In particular, my deployment experiences to GoDaddy are worth writing down since I know (from trying to find answers myself on the web) that it isn’t as simple as it could be.  I also “owe” (I think) my authorization architecture since that is another area I have seen people struggling with, particularly when it comes to deploying to a hoster like GoDaddy. 

But all that will have to wait.  I am excited about Windows Phone coming so have decided to build a phone app for our running club.  Truth be told, our club members don’t like sitting in front of computers but they would see utility in being to update club records from a phone, perhaps even at the run each week.  Press gang people into setting runs and commit them on the spot using the phone.

So, I have a pretty well functioning ASP.NET MVC site.  I have controllers and repository code that does a lot of what I need.  Now how to get it on the phone?  The phone apps, of course, are written in Silverlight and so I have to use my web site using some form of services.  I asked around and an architecture of:

  1. Add JsonResult actions to my controllers and reuse existing code
  2. Call the JsonResult methods using Http from Silverlight on the phone

 

The other option is probably ADO data services but that would mean rewriting large chunks of function that is already tested, works, and is secure.

So, I wrote my first simple bit of Json code, as follows:

        public JsonResult SilvGetNextRun()
        {
            Run r = rr.GetNextRun();
            JsonResult j = new JsonResult();
            j.Data = r;
            j.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return j;
        }

but on running it got the dreaded “A circular reference was detected while serializing an object of type ‘blah.blah.blah’.”

After a bit of web searching I found this article which explains the problem well; but in short form, LINQ to SQL generates two way references between classes and these can’t be serialized (presumably for data safety).  So now I am trying to work out what to do.  Since I have to build classes for my entities for the phone end of the app, I am thinking of using a “middle tier” object which has no references and can simply be used to serialize and move the data between server and phone.  If anyone has better ideas I am all ears.

There is a moral here somewhere.  I fell in love with LINQ to SQL because it was so easy to use, but as always you need to anticipate your architectural challenges and make sure design choices don’t come back to bite you.