zoukankan      html  css  js  c++  java
  • Silverlight WCF RIA服务(十六)表示模型

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

    WCF RIA Services允许我们创建数据模型来综合从数据访问层得到的不同实体数据。这个模型就是表示模型。当我们不想把数据层的数据直接公开给客户端时,会使用这个特性。当使用表示模型时,可以只修改表示模型而不是客户端来回应数据访问层中的改动。还可以设计一个综合那些仅与客户端用户相关的字段的模型,来简化客户端代码。
    创建表示模型
    需要用来维护数据完整性的数据库结构可能会比在客户端应用中需要的那些实体更复杂。我们可以通过把那些与应用相关的字段组合进一个表示模型,来简化这个数据结构。例如,在AdventureWorksLT示例数据库中,我们通过Customer,CustomerAddress,Address表来检索客户和地址数据。

    通过在服务端项目中创建一个类来创建表示模型,并定义需要的成员属性。这些定义的成员属性对应着你想从实体公开的成员属性。例如下面的示例,在服务端创建一个类,来表示那些从Customer、CustomerAddress、Address表中仅仅需要的字段。
    在表示模型中查询和修改数据
    在创建了表示模型后,通过添加一个和这个表示类型交互的domain service来向客户端公开。下面的示例展示了一个从DomainService类派生的域服务。
    1
    2
    3
    4
    5
    6
    7
    [EnableClientAccess()]
    public class CustomerDomainService : DomainService
    {
        AdventureWorksLT_DataEntities context = new AdventureWorksLT_DataEntities();
    }

     
    为了检索数据,我们在Domain service中添加了一个query方法。在query方法中,从数据访问层中的实体检索相关的数据,并把这些值赋值给新的表示模型实例中对应的成员属性。从这个query方法,要么返回一个这个表示模型类型的实例,要么返回一个表示模型类型的IQueryable。下面示例了Customer表示模型的查询方法。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    public IQueryable<CUSTOMERPRESENTATIONMODEL> GetCustomersWithMainOffice()
    {
        return from c in context.Customers
            join ca in context.CustomerAddresses on c.CustomerID equals ca.CustomerID
            join a in context.Addresses on ca.AddressID equals a.AddressID
            where ca.AddressType == "Main Office"
               select new CustomerPresentationModel()
               {
                   CustomerID = c.CustomerID,
                   FirstName = c.FirstName,
                   LastName = c.LastName,
                   EmailAddress = c.EmailAddress,
                   Phone = c.Phone,
                   AddressType = ca.AddressType,
                   AddressLine1 = a.AddressLine1,
                   AddressLine2 = a.AddressLine2,
                   City = a.City,
                   StateProvince = a.StateProvince,
                   PostalCode = a.PostalCode,
                   AddressID = a.AddressID,
                   AddressModifiedDate = a.ModifiedDate,
                   CustomerModifiedDate = c.ModifiedDate
               };
    }

     
    由于在数据访问层中并没有通过domain service公开实体(Customer、CustomerAddress、Address),所以不会在客户端生成这些类型。相反,在客户端仅生成表示模型类型。
    如果想通过表示模型更新数据,我们需创建一个更新方法,并定义从表示模型向实体存贮值的逻辑。可以参考本节最后的示例。
    把值返回给客户端
    提交了更改后,我们可能需要把存贮到中间层逻辑或数据源中的值传回客户端。WCF RIA Services提供了Associate(TEntity,TStoreEntity)方法来映射从实体回到表示模型的值。在这个方法中,我们提供一个在提交后调用的回调方法。在这个回调方法中,我们把在中间层已改动的值赋值给表示模型。通过执行这个步骤来使客户端持有当前值。
    下面是示例演示了如何更新实体中的值,并如何把修改后的数据映射回表示模型。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    [Update]
    public void UpdateCustomer(CustomerPresentationModel customerPM)
    {
        Customer customerEntity = context.Customers.Where(c => c.CustomerID == customerPM.CustomerID).FirstOrDefault();
        CustomerAddress customerAddressEntity = context.CustomerAddresses.Where(ca => ca.CustomerID == customerPM.CustomerID && ca.AddressID == customerPM.AddressID).FirstOrDefault();
        Address addressEntity = context.Addresses.Where(a => a.AddressID == customerPM.AddressID).FirstOrDefault();
     
        customerEntity.FirstName = customerPM.FirstName;
        customerEntity.LastName = customerPM.LastName;
        customerEntity.EmailAddress = customerPM.EmailAddress;
        customerEntity.Phone = customerPM.Phone;
        customerAddressEntity.AddressType = customerPM.AddressType;
        addressEntity.AddressLine1 = customerPM.AddressLine1;
        addressEntity.AddressLine2 = customerPM.AddressLine2;
        addressEntity.City = customerPM.City;
        addressEntity.StateProvince = customerPM.StateProvince;
        addressEntity.PostalCode = customerPM.PostalCode;
     
        CustomerPresentationModel originalValues = this.ChangeSet.GetOriginal(customerPM);
     
        if (originalValues.FirstName != customerPM.FirstName ||
            originalValues.LastName != customerPM.LastName ||
            originalValues.EmailAddress != customerPM.EmailAddress ||
            originalValues.Phone != customerPM.Phone)
        {
            customerEntity.ModifiedDate = DateTime.Now;
        }
     
        if (originalValues.AddressLine1 != customerPM.AddressLine1 ||
            originalValues.AddressLine2 != customerPM.AddressLine2 ||
            originalValues.City != customerPM.City ||
            originalValues.StateProvince != customerPM.StateProvince ||
            originalValues.PostalCode != customerPM.PostalCode)
        {
            addressEntity.ModifiedDate = DateTime.Now;
        }
     
        context.SaveChanges();
     
        this.ChangeSet.Associate(customerPM, customerEntity, MapCustomerToCustomerPM);
        this.ChangeSet.Associate(customerPM, addressEntity, MapAddressToCustomerPM);
    }
     
    private void MapCustomerToCustomerPM(CustomerPresentationModel customerPM, Customer customerEntity)
    {
        customerPM.CustomerModifiedDate = customerEntity.ModifiedDate;
    }
     
    private void MapAddressToCustomerPM(CustomerPresentationModel customerPM, Address addressEntity)
    {
        customerPM.AddressModifiedDate = addressEntity.ModifiedDate;
    }

     

    Powered By D&J (URL:http://www.cnblogs.com/Areas/)
  • 相关阅读:
    Winform中设置BackgroundWorker在取消时关闭后台进程不生效-没有跳出循环
    Ionic中自定义公共模块以及在自定义模块中使用ionic内置模块
    Ionic创建页面以及页面之间跳转、页面添加返回按钮、新增底部页面
    Ionic介绍以及搭建环境、新建和运行项目
    格式化输出(%用法和fomat用法)
    ubuntu1804搜狗输入法乱码问题解决
    python测试网站访问速度
    linux常用命令手册
    docker打包flask简单程序
    docker命令集锦
  • 原文地址:https://www.cnblogs.com/Areas/p/2172176.html
Copyright © 2011-2022 走看看