神仙?妖怪?谢谢!

Just do it...

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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


创建表示模型
需要用来维护数据完整性的数据库结构可能会比在客户端应用中需要的那些实体更复杂。我们可以通过把那些与应用相关的字段组合进一个表示模型,来简化这个数据结构。例如,在AdventureWorksLT示例数据库中,我们通过Customer,CustomerAddress,Address表来检索客户和地址数据。 
 
通过在服务端项目中创建一个类来创建表示模型,并定义需要的成员属性。这些定义的成员属性对应着你想从实体公开的成员属性。例如下面的示例,在服务端创建一个类,来表示那些从Customer、CustomerAddress、Address表中仅仅需要的字段。


在表示模型中查询和修改数据
在创建了表示模型后,通过添加一个和这个表示类型交互的domain service来向客户端公开。下面的示例展示了一个从DomainService类派生的域服务。

1 [EnableClientAccess()]   
2 public class CustomerDomainService : DomainService   
3 {   
4     AdventureWorksLT_DataEntities context = new AdventureWorksLT_DataEntities();   
5 }  
6 

 

 

< id="highlighter_843768_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://alexgorbatchev.com/pub/sh/current/scripts/clipboard.swf" id="highlighter_843768_clipboard" type="application/x-shockwave-flash" title="copy to clipboard" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_843768" menu="false" src="http://alexgorbatchev.com/pub/sh/current/scripts/clipboard.swf" height="16" width="16">
 

为了检索数据,我们在Domain service中添加了一个query方法。在query方法中,从数据访问层中的实体检索相关的数据,并把这些值赋值给新的表示模型实例中对应的成员属性。从这个query方法,要么返回一个这个表示模型类型的实例,要么返回一个表示模型类型的IQueryable。下面示例了Customer表示模型的查询方法。

代码
 1 public IQueryable<customerpresentationmodel> GetCustomersWithMainOffice()   
 2 {   
 3     return from c in context.Customers   
 4         join ca in context.CustomerAddresses on c.CustomerID equals ca.CustomerID   
 5         join a in context.Addresses on ca.AddressID equals a.AddressID   
 6         where ca.AddressType == "Main Office"  
 7            select new CustomerPresentationModel()   
 8            {   
 9                CustomerID = c.CustomerID,   
10                FirstName = c.FirstName,   
11                LastName = c.LastName,   
12                EmailAddress = c.EmailAddress,   
13                Phone = c.Phone,   
14                AddressType = ca.AddressType,    
15                AddressLine1 = a.AddressLine1,    
16                AddressLine2 = a.AddressLine2,   
17                City = a.City,    
18                StateProvince = a.StateProvince,    
19                PostalCode = a.PostalCode,   
20                AddressID = a.AddressID,   
21                AddressModifiedDate = a.ModifiedDate,   
22                CustomerModifiedDate = c.ModifiedDate   
23            };   
24 }

 

由于在数据访问层中并没有通过domain service公开实体(Customer、CustomerAddress、Address),所以不会在客户端生成这些类型。相反,在客户端仅生成表示模型类型。
如果想通过表示模型更新数据,我们需创建一个更新方法,并定义从表示模型向实体存贮值的逻辑。可以参考本节最后的示例。


把值返回给客户端
提交了更改后,我们可能需要把存贮到中间层逻辑或数据源中的值传回客户端。WCF RIA Services提供了Associate(TEntity,TStoreEntity)方法来映射从实体回到表示模型的值。在这个方法中,我们提供一个在提交后调用的回调方法。在这个回调方法中,我们把在中间层已改动的值赋值给表示模型。通过执行这个步骤来使客户端持有当前值。
下面是示例演示了如何更新实体中的值,并如何把修改后的数据映射回表示模型。

代码
 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

posted on 2010-06-05 01:31  E.Trock  阅读(829)  评论(0编辑  收藏  举报