Coproject - a RIA Caliburn.Micro demo, part 2

by Augustin Šulc

In this part, we will create a domain model and a RIA service so that the client application will be able to access data in Coproject database.

For those of you, who would like to see the code running but don't have time to write it, check Coproject codeplex site.

1. Create domain model

In Coproject.Web, create a new folder called Models.

Add a new Entity Data Model called CoprojectModel to it.
image

In Entity Data Model Wizard, choose Generate from database. Choose the proper database connection (probably Database.mdf) and let VS to Save entity connection settings in Web.Config as CoprojectEntities. Finally set the last wizard step as follows:
image

Click Finish and wait for the model being created. Then rebuild the solution. Now, the server is able to access database.

2. Create RIA service

Create a folder called Services and add new Domain Service Class called CoprojectService to it.
image

Then, set the wizard as follows:
image

Two new files should be added to Services:
image

The first file contains the RIA service itself. You can see functions like GetToDoItems, InsertToDoItem, etc. Their purpose should be clear.

Set metadata

The other file called CoprojectService.metadata.cs contains several classes like:

internal sealed class ToDoListMetadata
{
	// Metadata classes are not meant to be instantiated.
	private ToDoListMetadata()
	{
	}

	public DateTime CreatedDate { get; set; }
	public string Description { get; set; }
	public string Name { get; set; }
	public Nullable<int> ProjectID { get; set; }
	public EntityCollection<ToDoItem> ToDoItems { get; set; }
	public int ToDoListID { get; set; }
	public Nullable<DateTime> UpdatedDate { get; set; }
}

These are metadata that describe what entities should be transferred to the client, what links are between them, how to localize labels for these properties or how to validate them. Take this as a configuration file for the RIA service. These settings, although written on server, are transferred by RIA Services to the client application and available there too. Now, we have to add some more metadata.

Add attribute [Key] to properties ToDoItemMetadata.ToDoItemID, and ToDoListMetadata.ToDoListID and UserMetadata.UserID. This will instruct RIA Services that these properties are primary keys for these classes. This is essential for updating data and handling links between entities.

Next, update other properties of ToDoItemMetadata to look like:

[Include]
[Association("FK_ToDoItems_ToDoLists", "ToDoListID", "ToDoListID", IsForeignKey = true)]
public ToDoList ToDoList;

[Include]
[Association("FK_ToDoItems_Users", "UserID", "UserID", IsForeignKey = true)]
public User User;

This will make RIA services to link proper entites into the respective references. The same has to be done for ToDoListMetadata:

[Include]
[Association("FK_ToDoItems_ToDoLists", "ToDoListID", "ToDoListID")]
public EntityCollection<ToDoItem> ToDoItems;

Build the solution to make sure that there are no errors. Now, the server side is ready to serve data for client.

Tags: Silverlight, Caliburn, Ria, Coproject

9 Comments

  • Boydayseage said

    hi, new to the site, thanks.

  • Leo said

    Hey,

    is there a way if I updated my model from the database and I want to refresh the service class is it possible? or do I have to always setup or manually add those functions in the service class

  • Augustin Šulc said

    We use T4RIA templates to generate services, metadata and resources. The template also supports separation of custom and generated code.

    You can find it here: http://riaservicescontrib.codeplex.com/wikipage?title=T4RIA

    Moreover, it is quite easy to customize the code generation to suite your needs.

  • Leo said

    Is it stable for EF 4.1/4.3? I had my own T4 template generator.

  • Augustin Šulc said

    To use it for EF4.3, we just had to update XML headers in the template to match the EDMX file. No other problems.

  • Leo said

    But are you still using Linq or DbDomainService for your services?

  • Augustin Šulc said

    Everything else remained the same. Just EF changed to a new version. And as it uses different XML schema (year is updated :-) for its EDMX files, we had to update the T4RIA templates accordingly.

  • Joseph Mellone said

    There is a typo in the instruction above ... make sure there are now errors" should be "no errors".

    Great tutorial !!!

  • Augustin Šulc said

    @Joseph Thank you, typo fixed.

Add a Comment