Using WCF RIA Services with LightSpeed
To get started with WCF RIA Services with LightSpeed you would start your application in the usual manner, which is to create a standard Silverlight application and then enable WCF RIA Services using the checkbox in the New Silverlight Application dialog.
The next step is to set up a domain model. You will start by creating a LightSpeed model and designing it in the normal fashion. For more information on modelling please refer to the chapters on Creating Domain Models and Domain Modelling Techniques.
Because we will be shipping our objects over the wire make sure you include references to System.ServiceModel and System.Runtime.Serialization so the appropriate DataContract mark-up is generated for your entities. You will also want to make sure you flag any associations with a DataMember attribute as appropriate to ensure any child collections or related entities are sent to the client along with the parent entity. For more information on how to define this please refer to the chapter on Building Distributed Applications.
To expose our model using WCF RIA Services we need to create a domain service. You will need to do this manually rather than using the wizards that are available.
Domain Service Basics
To start, add a reference to the Mindscape.LightSpeed.ServiceModel.DomainServices assembly, which contains the helper classes you will need to build WCF RIA Services projects with LightSpeed. To create your domain service you will need to base your class on LightSpeedDomainService<TUnitOfWork> which provides the underlying mechanics for handling WCF RIA Services requests.
To implement your derived class you will need to implement the CreateLightSpeedContext method to return the strong-typed LightSpeedContext to be used for creating units of work. The domain service caches the LightSpeedContext so you don’t need to worry about ensuring it is a singleton.
Declaring a DomainService based on the LightSpeedDomainService<TUnitOfWork> |
public class ModelDomainService : LightSpeedDomainService<ModelUnitOfWork> |
Surfacing the Domain Entities
Once you have implemented the basics for the domain service you will want to expose any entities which are to be handled at the client. This needs to be done not only for the primary entities which are being exposed but also for any related objects which we will be accessing.
To expose an entity you must at a minimum define a queryable method for that entity type. This method must be marked with the[Query] attribute.
If you are intending to perform updates, you will also want to expose Insert, Update and Delete methods for that entity type.
Below is an example of a set of methods defined for a Film entity. Note the [Query] attribute on the query method.
Declaring the CRUD methods for a Film entity |
[Query] |
Generating and Using the Silverlight Data Source
Once you have defined your entities as part of the domain service you can re-compile your solution and WCF RIA Services will code generate proxy classes in your Silverlight application to allow for remote data access to occur. You will now be able to find your exposed entities under the Data Sources tab, for example if we exposed Film and Cinema entities then the view would be as below.
Now that your data is exposed you can continue programming normally and build up your Silverlight application. All other standard Silverlight/WCF RIA Services approaches can be applied such as exposing validation concerns by adding metadata classes to your entities, or exposing custom methods as part of the domain service.