Home > .NET > RIA Services, Shared Code and Silverlight

RIA Services, Shared Code and Silverlight

November 30, 2009 Leave a comment Go to comments

Using the shared code feature, you can define code that RIA services will project ‘as is’ to the client. Using a Folder entity as an example we will project a method ‘BreadCrumbs’ to the client.

For this example we will assume a file (Folder.cs) exists with the following code.

using System; using System.Collections.Generic; using System.ComponentModel; public partial class Folder : IEditableObject { public Int32 Id { get; set; } public String Name { get; set; } public String Description { get; set; } public Int32 ParentId { get; set; } public IList<Document> Documents { get; set; } public IList<Folder> SubFolders { get; set; } public IList<Tag> Tags { get; set; } public Folder Parent { get; set; } #region IEditableObject implementation public void BeginEdit() { } public void CancelEdit() { } public void EndEdit() { } #endregion }

The projection of this class to the client side is described by attributes in a Metadata file (see Using Metadata for validation in Silverlight).

As well as this decription we also want to project some additional code to the client, this is defined in another file (Folder.shared.cs), the code looks like this.

using System; using System.Collections.Generic; using System.ComponentModel; public partial class Folder { public String BreadCrumbs() { String breadCrumbs = ""; Folder currentFolder = this; while (currentFolder != null) { breadCrumbs = String.Format(@"{0}\{1}", currentFolder.Name, breadCrumbs); currentFolder = currentFolder.Parent; } return breadCrumbs; } }

Notice the class definitions in Folder.cs and Folder.shared.cs are both partial.

When you build a project containing shared code, you will notice the code is generated on the client also.

shared code

You can then access the method easily.

shared intellisense

kick it on DotNetKicks.com

About these ads
  1. December 2, 2009 at 12:42 am | #1

    Why would you go to all of this trouble and risk a breakdown in code generation when, based on what you ask of the Folder class here, you can simply link to Folder.cs from your Silverlight project?

    Don’t see a benefit from the Metadata file hack either; you wrote Folder, you can (and should) put the validation attributes in the Folder.cs class itself.

    This approach is confusing. It suggests that Breadcrumbs is the “shared” code when in fact you’re using Folder.shared.cs to trick RIAS into projecting the class.

    You’re asking the dev to maintain the code for a single simple class over three separate files. Yuck.

    Where is the RIA Services added value to make this worthwhile? I’m missing something.

  2. December 2, 2009 at 6:13 pm | #2

    Thanks for the comment. You had a few points, so I wrote another post about them.

  1. December 1, 2009 at 4:14 am | #1
  2. December 2, 2009 at 1:01 am | #2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: