Friday 20 July 2012

Shared code from business logic with RIA Services


RIA Does a lot of automation to simplify the data access from your UI when using the entity framework. This is great but in certain situations it is very useful to have some features available in both the UI and the DAL.

This is where the shared classes are useful. When renaming any of you classes to *.shared.cs from any projects of your solution, the RIA will generate a corresponding class in your RIA client.

When building the RIA Enabled project , a Generated_Code folder will add the corresponding folder hierarchy and generate copies of your shared classes making the code available to your UI.

Keep in mind

You cannot modify these files from the UI as they are clones of the ones hosted in your parent projects.

Any project that is referenced by the ASP.NET hosting project will see their *.shared classes generated in the Silverlight UI project.

The classes renamed to *.shared will use Silverlight as a target framework, even if they are present in a project that target another framework profile.

You can call your code from both the business logic and UI layer.

Sample

From a standard class library , DemoClass.shared.cs

    public class DemoClass
    {
        public string SayHello()
        {
            return "Hello!";
        }
    }

From the silverlight project

 DemoClass dc = new DemoClass();
 TextBlock tb = new TextBlock();
 tb.Text = dc.SayHello();

 LayoutRoot.Children.Add(tb);>

Download the demo project

 



No comments:

Post a Comment