The Elephant and the Silverlight

Thoughts and analysis of Silverlight for business applications

Page List

    To Share or Not to Share

    One of the really interested puzzles that occurred to me with the release of SL4 and the VS2010 version of WCF RIA Services is what the new ability of .NET 4 to reference SL4 code libraries means to the WCF RIA Services shared file feature. My thinking goes like this:

    1. The shared code feature was created because the Silverlight and Desktop CLRs couldn’t share assemblies.
    2. The Desktop CLR can now reference Silverlight 4 assemblies
    3. Given that the shared code feature is not being removed, there must still be a use case for shared code
    4. The use case for shared code must be uniquely related to RIA Services, otherwise the feature would become part of Visual Studio itself
    5. Given both 3 and 4, it follows that while the shared feature is important, we must be careful to limit our usage.
    The Use Case for Shared Code

    The use case is very simple, RIA Services uses a DAL or PM object and a RIA Services Entity on the client. Those are two completely different objects yet we have business logic and partial classes that we want to share between the two tiers.

    One way to fix this problem might be to apply a shared interface to the client and server side objects. If that interface was defined in a Silverlight class library then both the server and client could share that same interface. Then all of the business logic could be defined within a Silverlight class library and shared between the client and the server. However, creating interfaces and class libraries doesn’t really fit in with the “RAD for Silverlight” image that RIA Services has.

    The shared code feature handles this by copying un-compiled code between projects. This allows the same code to be compiled against two completely different objects (the server side DAL/PM object and the client side Entity) without having having to have any real interfaces. Also, to be frank, for most people putting interfaces on data objects is taking things a little too far.

    The Case Against Shared Code

    I am not going to get too deep into this one. Suffice to say, there is a reason that we use multiple projects that compile into dlls instead of just copying all of the code files for a solution into a single mega project and producing a single mega-dll. The shared code feature is part of RIA Services that Shawn Wildermuth would probably call a “Visual Studio Trick”. It is, much like the metadata file used to attach business rules, a bit of a kludge. I wouldn’t be surprised if the shared code feature doesn’t end up getting ripped out of Silverlight in a few years when some new feature of the CLR gives us a better way of solving the core use cases.More importantly, since the shared code feature is purely a compile time piece of trickery I don’t expect Microsoft to be worrying to much about keeping backwards compatibility of shared files too high on their must do list.

    Great, so what is the recommendation?

    To put it quite simply, for any code that needs to interact directly with both the Entity and the DLR/PM objects you should be using the Shared file feature. Any shared code should be kept as shallow as possible so keep as much of your code as possible in a Silverlight class library that is shared by both the server and client sides of your project.


    Categories: RIA Services
    Permalink | Comments (6) | Post RSSRSS comment feed

    Comments

    Justin Angel Canada

    Thursday, December 03, 2009 3:37 PM

    Justin Angel

    Hi colin,

    FYI, Assumption #2 has a typo. Looks like you meant to have a w instead of a t at the end of now.

    -- Justin Angel

    ColinBlair United States

    Thursday, December 03, 2009 3:43 PM

    ColinBlair

    Thanks for the correction Justin, assumption 2 did not make very much sense that way.

    Nikhil Kothari United States

    Thursday, December 03, 2009 8:43 PM

    Nikhil Kothari

    Its interesting and sort of funny - that is quite an important word in the context this topic - "now" Smile

    ColinBlair United States

    Thursday, December 03, 2009 9:17 PM

    ColinBlair

    Yes, I am very grateful that Justin noticed the error so quickly, the whole post pretty much depends on that word being correct.

    Tad United States

    Saturday, December 05, 2009 10:56 AM

    Tad

    Hmm...I tend to take a different view on you statement that "RIA Services is RAD for Silverlight".  I believe RIA Services does make the development easier and faster, but it does not mean that it can't be used in more complex architectures.  RIA Services provides a framework and a pattern for developing and interacting with your services.  I would have thought that if it was just meant for RAD then the class library concept would not have been added and the extensibility of RIA components would not have been built in.  

    ColinBlair United States

    Saturday, December 05, 2009 11:51 AM

    ColinBlair

    @Tad What you just said is one of the core tenents of this blog. The "RIA Services is RAD for Silverlight" statement is not my own, that has been said by the Microsoft guys. That doesn't make it untrue though, if you take the entire RIA Services stack from DomainService to DomainDataSource then what you get is RAD for Silverlight. However, the word "just" is not in that statement. RIA Services is "just" a RAD for Silverlight the same way that Barack Obama is "just" a lawyer. If you combine RIA Services with MVVM and an IoC container then you have something that goes way beyond RAD.

    Add comment


    (Will show your Gravatar icon)

      Country flag

    Click to change captcha
    biuquote
    • Comment
    • Preview
    Loading