.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.
- Build a Simple Application with .Net RIA Services (Silverlight 3) – Part 1
- Build a Simple Application with .Net RIA Services (Silverlight 3) – Part 2
- .Net RIA Services Part 3: DataForm and Validation
- Using DomainDataSource in ASP.Net
- ASP.Net DomainDataSource with Select Parameters
- ASP.Net QueryExtender Control and DomainDataSource
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).
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:
public class CustomerValidation
public static bool IsEmailEndsWithDotCom(string email)
After you build the solution, Visual Studio will copy this file to the Silverlight client application:
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.