您的位置:首页 > 其它

WCF RIA 服务 (十六)- 表示模型

2010-06-05 01:31 316 查看
WCF RIA Services允许我们创建数据模型来综合从数据访问层得到的不同实体数据。这个模型就是表示模型。当我们不想把数据层的数据直接公开给客户端时,会使用这个特性。当使用表示模型时,可以只修改表示模型而不是客户端来回应数据访问层中的改动。还可以设计一个综合那些仅与客户端用户相关的字段的模型,来简化客户端代码。

创建表示模型
需要用来维护数据完整性的数据库结构可能会比在客户端应用中需要的那些实体更复杂。我们可以通过把那些与应用相关的字段组合进一个表示模型,来简化这个数据结构。例如,在AdventureWorksLT示例数据库中,我们通过Customer,CustomerAddress,Address表来检索客户和地址数据。
代码

1 [Update]
2 public void UpdateCustomer(CustomerPresentationModel customerPM)
3 {
4 Customer customerEntity = context.Customers.Where(c => c.CustomerID == customerPM.CustomerID).FirstOrDefault();
5 CustomerAddress customerAddressEntity = context.CustomerAddresses.Where(ca => ca.CustomerID == customerPM.CustomerID && ca.AddressID == customerPM.AddressID).FirstOrDefault();
6 Address addressEntity = context.Addresses.Where(a => a.AddressID == customerPM.AddressID).FirstOrDefault();
7 customerEntity.FirstName = customerPM.FirstName;
8 customerEntity.LastName = customerPM.LastName;
9 customerEntity.EmailAddress = customerPM.EmailAddress;
10 customerEntity.Phone = customerPM.Phone;
11 customerAddressEntity.AddressType = customerPM.AddressType;
12 addressEntity.AddressLine1 = customerPM.AddressLine1;
13 addressEntity.AddressLine2 = customerPM.AddressLine2;
14 addressEntity.City = customerPM.City;
15 addressEntity.StateProvince = customerPM.StateProvince;
16 addressEntity.PostalCode = customerPM.PostalCode;
17 CustomerPresentationModel originalValues = this.ChangeSet.GetOriginal(customerPM);
18 if (originalValues.FirstName != customerPM.FirstName ||
19 originalValues.LastName != customerPM.LastName ||
20 originalValues.EmailAddress != customerPM.EmailAddress ||
21 originalValues.Phone != customerPM.Phone)
22 {
23 customerEntity.ModifiedDate = DateTime.Now;
24 }
25 if (originalValues.AddressLine1 != customerPM.AddressLine1 ||
26 originalValues.AddressLine2 != customerPM.AddressLine2 ||
27 originalValues.City != customerPM.City ||
28 originalValues.StateProvince != customerPM.StateProvince ||
29 originalValues.PostalCode != customerPM.PostalCode)
30 {
31 addressEntity.ModifiedDate = DateTime.Now;
32 }
33 context.SaveChanges();
34 this.ChangeSet.Associate(customerPM, customerEntity, MapCustomerToCustomerPM);
35 this.ChangeSet.Associate(customerPM, addressEntity, MapAddressToCustomerPM);
36 }
37 private void MapCustomerToCustomerPM(CustomerPresentationModel customerPM, Customer customerEntity)
38 {
39 customerPM.CustomerModifiedDate = customerEntity.ModifiedDate;
40 }
41 private void MapAddressToCustomerPM(CustomerPresentationModel customerPM, Address addressEntity)
42 {
43 customerPM.AddressModifiedDate = addressEntity.ModifiedDate;
44 }

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/blackant2/archive/2010/04/08/5461633.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: