Coproject - a RIA Caliburn.Micro demo, part 2

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值