DCSIMG
.Net RIA Services: Sharing Code between the Client and Server - Guy Burstein's Blog

Guy Burstein's Blog

Developer Evangelist @ Microsoft

News

Guy Burstein The Bu

Disclaimer
Postings are provided 'As Is' with no warranties and confer no rights.

Guy Burstein LinkedIn Profile

TwitterCounter for @bursteg

Links

Articles

Blogs I Read

.Net RIA Services: Sharing Code between the Client and Server

.Net RIA Services: Sharing Code between the Client and Server

This is another post in a series of posts about building applications with Microsoft .Net RIA Services and Silverlight 3 and ASP.Net.

In this I’ll talk about how to share code between the server side and the Silverlight client application.

One of the great things in .Net RIA Services is that code that we wrote in the server side can be resued in the client side. To do that, create a new class in the server side called with .shared.cs or .shared.vb suffix (CustomerValidation.shared.cs in this sample).

.Net RIA Services Sharing Code between Client and Server

In this class, code what you’d live to share with the client side, under the rational limitations. To make the code sections shared between the server and the client, decorate them with System.Web.Ria.Data.SharedAttribute. For example:

[Shared]

public class CustomerValidation

{

  [Shared]

  public static bool IsEmailEndsWithDotCom(string email)

  {

    if (email.EndsWith(".com"))

      return true;

    return false;

  }

}

After you build the solution, Visual Studio will copy this file to the Silverlight client application:

.Net RIA Services Sharing Code between Client and Server

and now, you can use this code in the client application, as you did in the server side. Here is an example of me using the shared code in the HomePage class in the Silverlight client application.

.Net RIA Services Sharing Code between Client and Server

Enjoy!

Comments

Summary 15.04.2009 « Bogdan Brinzarea’s blog said:

Pingback from  Summary 15.04.2009 « Bogdan Brinzarea’s blog

# April 15, 2009 7:40 PM

Yoav said:

למה אי אפשר לבצע הפרדה של הקוד לכמה פרוייקטים וביצוע של קישור בין הפרוייקטים?

# April 16, 2009 11:53 AM

Woshiernog said:

Thank you so much. I was able to use this to cast server side entities to client side objects.

# April 29, 2009 8:50 PM

Geoff Hudik said:

Two questions:

1) How can I share initialization (i.e. ctor logic) between client and server (if at all)? [Shared] cannot be applied to constructors but there may be some logic that I may want automatically done on both client and server when an entity is created (setting some properties, perhaps default validation etc.)

2) On the server side my entities can take advantage of functionality inherited down from base types but those members do not make it into the client side classes, regardless of [Shared].  Is there a way to change generation so client classes do not inherit directly from Entity and DomainContext, but from my own base classes that inherit from those classes?

# May 18, 2009 5:00 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: