.NET RIA Services For Silverlight


Published on

Ian Blackburn from bbits.co.uk gives the Silverlight UK User Group a talk on the new .NET RIA Services and its implementation within Silverlight applications.

This presentation was given on 29th September 2009 at the EMC Consulting offices in London.

Published in: Technology
1 Comment
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

.NET RIA Services For Silverlight

  1. 1. .Net Ria Services for Silverlight<br />Ian Blackburn<br />Download RIA Services and documentation from http://www.microsoft.com/downloads/details.aspx?FamilyID=76bb3a07-3846-4564-b0c3-27972bcaabce&displaylang=en<br />This presentation is a summary of the ~200 page document from there (plus some examples ;-) <br />
  2. 2. Overview<br />www.bbits.co.uk<br />
  3. 3. Where do RIA Services Exist?<br />www.bbits.co.uk<br />
  4. 4. Presentation Technologies and DALs <br />The current preview illustrates the pattern primarily through the end-to-end experience with Silverlight and EF or Linq to Sql<br />but RIA Services are designed to work other Presentation and DALS as well<br />www.bbits.co.uk<br />
  5. 5. Getting Started<br />www.bbits.co.uk<br />
  6. 6. The DomainService Class<br />This is the main class for authoring operations <br />Such as CRUD behaviours<br />It can be generated code in the first instance.<br />You add your custom application logic to this class<br />The [EnableClientAccess] attribute causes the tools to generate the corresponding client-tier classes when the project is built<br />www.bbits.co.uk<br />
  7. 7. The DomainContext Class<br />A client side DomainContext class is generated for each DomainService<br />This provides basic services such as invocation of operations, accumulation of results, object identity and change tracking <br />www.bbits.co.uk<br />
  8. 8. Sharing Code Between Client and Server<br />Code that is written in the server side can be reused in the client side. <br />To do this<br />create a new class in the server side with a .shared.cs or .shared.vb suffix <br />Mark the class and methods you want to share with the [Shared] attribute<br />After you build the solution, Visual Studio will copy this file to the Silverlight client application:<br />www.bbits.co.uk<br />
  9. 9. Creating Query Methods in the Service<br />Define Query Methods in your Domain Service<br />These must:<br />return a single instance T, or an IEnumerable&lt;T&gt; or IQueryable&lt;T&gt; where T is a valid Entity type<br />takes 0 or more parameters <br />Optionally be adorned with the QueryAttribute<br />
  10. 10. Using Generated Query Methods in Silverlight<br />When a query method is defined in the domain service, the code generator automatically creates <br />A query factory method named [NameOfQueryMethod]Query<br />The entity Type of the query method <br />You use it by:<br />createinganEntityQuery instance<br />Applying LINQ query operators to it as needed<br />Passing the query instance into DomainContext.Load<br />
  11. 11. Example<br />Note that the “orderby” is still happening on the server, and the Load is asynchronous<br />
  12. 12. Support Query Operators in Silverlight<br />EntityQuery exposes only a subset of LINQ query operators. <br />These are: <br />Where <br />OrderBy / ThenBy<br />Skip / Take <br />
  13. 13. Metadata classes<br />To add some validation rules that are applied on both tiers, you create a metadata class for the Domain Service<br />affectionately called the “buddy class”<br />This can be generated when you create the domain service by checking the box shown<br />Or you can just add the MetadataType attribute manually to an entity class<br />www.bbits.co.uk<br />
  14. 14. Using Include on MetaData<br />You need to mark the Suppliers metadata with an equivalent Include attribute so the correct code is generated on the client<br />
  15. 15. Adding Validation rules to the Metadata class<br />www.bbits.co.uk<br />This is achieved through attributes on the properties of the class on the server<br />This causes the generated entity class in the client to have the same validation applied<br />
  16. 16. Modifying Data<br />If you use the DomainService class wizard to generate DomainService class, then the generated service class has Create/Read/Update/Delete (CRUD) operations already<br />
  17. 17. EntityContainer, EntityList and Change Tracking <br />Entities loaded into a DomainContext are stored and change tracked by its EntityContainer<br />This is the Entities collection on the context object<br />An EntityContainer manages a set of EntityLists<br />there is a 1:1 mapping between EntityLists and the entity Types exposed by your service<br />You can get a list of entities for a particular type using GetEntityList&lt;T&gt;<br />You can inspect the Entity.EntityState of an entity to determine the state of the entity<br />Unmodified, Modified, New, Deleted<br />
  18. 18. Change Sets (Unit of Work) <br />A change set is a set of one or more entity operations to be processed as a single unit of work and can contain<br />Entity Insert/Update/Delete operations <br />Custom domain operations<br />When SubmitChanges is called on the context the changes are all sent to the service all together <br />You can get the current ChangeSet using the GetChanges method of the EntityContainer<br />You can discard all pending changes, by calling RejectChanges on the context. <br />
  19. 19. Server Side Change Set Processing <br />
  20. 20. Error Handling <br />You can throw validation exceptions in the Domain Service and then handle them in the client SubmitOperation completed event<br />
  21. 21. Other Supported Features<br />Concurrency Handling<br />Concurrency errors are reported from the DomainService to the client<br />You can handle them in the SubmitOperation completed event<br />You can also create a Resolve method on your service to deal with resolution<br />Transactions Support<br />No built in support from the client, but you can of course wrap your domain methods in transactions<br />Identity Management <br />The Key attribute on entities is used to keep track of local entities<br />Whenever an entity is loaded if there is a cached instance locally with the same identity or key value(s) the local instance will be retained, and the newly read instance is discarded<br />You can change this behaviour with the MergeOption in the context Load method<br />
  22. 22. Custom Methods and Service Operations<br />Using Custom Methods<br />You can create custom server methods in addition to basic create/update/delete operations<br />These also support tracking on entities and deferred execution (e.g. When SubmitChanges is called)<br />Mark them with the [Custom] attribute<br />Call from the context or the entity itself in the client<br />Service Operations <br />More closely related to traditional web service<br />Use the [ServiceOperation] attribute<br />Don’t support tracking of deferred execution<br />
  23. 23. Other features<br />Silverlight DomainDataSource<br />Use declarative code to define the interactions between your UI and the data <br />Supports filtering, sorting, grouping, editing...<br />Restrict Access to DomainService Operations <br />You can authenticate using the membership system in asp.net<br />Then configure access against operations roles and users<br />Code Generated Hook Points <br />Partial methods are supported in the generated code to give extensibility points<br />E.g. OnCreated, OnPropertyNameChanged<br />
  24. 24. Other Features<br />.NET RIA Services Class Libraries<br />permits packaging business logic into N-tier class library components rather than the Silverlight client and Web Site<br />Ado.Net Data Services<br />Stated goal is to align the two technologies such that Microsoft .NET RIA Services uses the same protocol as ADO.NET Data Services <br />Business Applications Templates<br />VS templates that builds on the Silverlight Navigation Application and using .NET RIA Services and provides support for Authentication and User Registration<br />
  25. 25. Questions?<br />
  26. 26. Concurrency Error<br />
  27. 27. Naming Conventions<br />Insert <br />“Insert”, “Add”, “Create” prefixes <br />Method signature : void InsertX(T entity), where T is an entity Type <br />Update <br />“Update”, “Change”, “Modify” prefixes <br />Method signature : void UpdateX(T current), where T is an entity Type <br />Delete <br />“Delete”, “Remove” prefixes <br />Method signature : void DeleteX(T entity), where T is an entity Type <br />Query <br />Method signature returning a singleton, IEnumerable&lt;T&gt; , IQueryable&lt;T&gt; where T is an entity Type, and taking zero or more parameters <br />Resolve <br />“Resolve” prefix <br />Method signature : boolResolveX(T curr, T original, T store, boolisDelete) where T is an entity type <br />