Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

November 10, 2009

Silverlight and RIA: Sharing Your Business Object Files

Filed under: ASP.NET RIA Services,C#,Silverlight,VB.NET @ 2:23 pm

So I have read in dozens of places that you can easily share a source code file between your business objects and your Silverlight project. So I thought I would try it. No go.

[To begin with an overview of Silverlight, RIA, and your business objects, start here.]

Supposedly, if you created a file in your business object of the form myName.shared.vb or myName.shared.cs the compiler will automatically generate the file in your Silverlight project as well. This allows you to share the same code between the business object and Silverlight.

Many hours of trying, Bing’ing, and reading later … still no go.

Then I re-read the RIA Services Overview document that is provided with the RIA download. Reading it carefully, it starts to become clear that there are TWO things that a file needs before it will be shared with your Silverlight project:

  1. It must be named appropriately: myName.shared.vb or myName.shared.cs
  2. It must exist in the project that is linked to the Silverlight project.

This second item is the more challenging item because in most cases, your Silverlight project is linked to a Web project.

As far as I know, it is not possible to re-point the link from a Web project to a business object class library component because Silverlight cannot reference a standard class library. It can only reference a Silverlight class library or a Web project.

So I did the following:

  1. Named the file using the .shared.vb/.shared.cs syntax.
  2. Used the Add Link feature of Visual Studio projects to manually define a link between the shared file and the Web project.

The second step required the following:

1. Right click on the Web project and select Add | Existing Item.

2. In the Add Existing Item dialog, select the file and select Add As Link from the Add button drop down menu.

image

This adds the file as a link to the Web project.

Now that the shared file is in the Web project, when you compile Silverlight will generate the copy. You can confirm this by following these steps:

1. Select the Silverlight project.

2. Click the Show All Files button in the Solution Explorer toolbar.

3. Open the Generated_Code node.

The shared file should exist under this node.

Use this technique any time you have code in your business objects that you want to share "as is" with the Silverlight project.

[For a more detailed example of using this technique to share a file, see this post.]

Enjoy!

3 Comments

  1.   Ian Smith — November 10, 2009 @ 11:49 pm    Reply

    I think it’s crazy to attempt using something as complex as RIA Services without reading the RIA Services Overview document in its entirety. That document is at great pains, even in the very first quick walkthrough, to point out that a file must be correctly named.

    It IS possible to link a Silverlight project to a non web project although it’s a bit painful to do this at the moment because the GUI doesn’t help you – you have to manually edit the csproj file. You can find more information on this in Dave Hill’s blog entry here: http://blogs.msdn.com/dphill/archive/2009/08/17/prism-and-net-ria-services.aspx

  2.   Evan — May 8, 2010 @ 4:17 am    Reply

    Deborah, thanks for the helpful post.

    Ian – Yes, and I think it is crazy that I have to go back to square one and relearn everything, every time Microsoft decides to develop a different .net technology (ado.net, webservices, wcf, vs wcf ria, etc etc). I should be able to 1) do a Enum.GetNames, 2) link a business object library and use the classes, something I have done dozens of times, without wading through a bunch of documentation to find out that I have to follow a particular naming convention for the latter, and that I have to rewrite my code to emulate the former.

  3.   Shmee — August 25, 2010 @ 4:52 am    Reply

    If your web project has a reference to you business objects project output (dll), it will pick up the “shared” object.
    i.e. build your web project before building your view project to be able to reference the shared business object from the view.
    Cazzy, yes, but does mean you don’t need to link files.

RSS feed for comments on this post. TrackBack URI

Leave a comment

*

© 2014 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs