您的位置:首页 > 运维架构

Coproject - a RIA Caliburn.Micro demo, part 2

2015-09-03 06:54 302 查看
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.




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:




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.




Then, set the wizard as follows:




Two new files should be added to Services:




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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: