SL/RIA/POCO: Generated Code

Posted Tue, Jul 20 2010 13:38 by Deborah Kurata

This post provides a high-level description of what happens when you compile a Silverlight application that uses WCF RIA Services and plain old CLR objects (POCOs) and the resulting generated code. (See this prior post for a code example.)

After you define your Silverlight project, enable WCF RIA Services, and add your Domain Service class with appropriate method(s), you have enough to build your solution and view the generated code.

In Solution Explorer, select your Silverlight project and click the Show All  Files button in the toolbar. You will then see a Generated_Code folder containing a *.g.cs file. This contains the code generated by Visual Studio and defines the classes detailed below.

Entity class

This class is generated from information in the business layer (your POCO classes on the server) and includes all of the class's properties. It inherits from the DomainServices Entity class (not to be confused with the Entity Framework (EF) Entity class).

The compiler looks at the properties of the class and any attributes on those properties. It then generates a copy of those properties with their associated attributes in the generated code file.

How does it know which business layer class to use for the Entity class? It determines the appropriate class based on the parameters and/or return values on the methods in your Domain Service class.

If you have a query style method, it returns either a single entity or an IEnumerable or IQueryable of the entity. The compiler uses that information to determine which business layer class to use.

If you have an insert, update, or delete method, the parameter defines the entity. The compiler uses that information to determine which business layer class to use.

The compiler attributes the generated class as a DataContract and each property as a DataMember so it can be processed appropriate when the class is used as part of a WCF service. (WCF RIA Services uses WCF "under the covers".)

Domain Context class

This class is generated from information in the Domain Service class that you created. It inherits from DomainContext.

The compiler defines a WCF service contract in this class. It also defines wrapper methods for the methods in your Domain Service class.

When your Silverlight application calls methods in this class, the class makes the WCF service calls to the Domain Service class in the ASP.NET project.

Here it is in picture form:

image

Enjoy!

Comments

# SL/RIA/POCO: No Generated Code?

Tuesday, July 20, 2010 4:38 PM by Deborah's Developer MindScape

This prior post described the code that is generated when you compile a Silverlight application that

# re: SL/RIA/POCO: Generated Code

Tuesday, September 21, 2010 5:03 PM by srinivas

Can you please explain in detail how compiler will work for add, update and delete.

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: