zoukankan      html  css  js  c++  java
  • 使用Entity Framework和WCF Ria Services开发SilverLight之7:多个Domain Service间共享Poco实体

    在前两篇中,我们讲了在SL端通过Entity Framework和WCF Ria Services实现联表查询,其中最关键的是为实体类中的相关属性设定为Include特性,如下:

    image

    遗憾的是,这样的实体Include只能在单个域服务间共享实体。如果你尝试撰写多个域服务,并用到关联实体,会报如下错误:

    The entity type 'MiniNWModel.Entities.Product' is exposed by multiple DomainService types.
    Entity types cannot be shared across DomainServices.	SilverlightApplicationSample

    有人指出,域服务上下文应该是long life的,所以整个应用程序应该只有一个域服务。不过,这难道不是有点扯吗?应用程序所支撑的数据库动辄上百张表,不同的业务范畴建立多个域服务基本是必须的。

    一:数据库支撑

    本篇所采用的数据库来自于《Entity Framework 4.1 and Poco 使用存储过程联表查询》。

    二:数据实体

    本篇共涉及两个实体,要让两个实体在多个域服务间共享彼此,必须依赖特性ExternalReference。以下是主表实体:

    image

    以下是从表实体:

    image

    一定要注意正确匹配Association,否则关联数据的时候会不正确。

    三:域服务

    两个域服务类没有任何特殊之处。唯一需要注意的是,在调试的时候我们会发现EF联表查询中得到的数据,如果不做特殊处理,从表数据全部会丢失。如下图源码:

    image

    在方法GetCategoryWithProductsWithID中,我们会得到从表的数据,这是EF为我们得到的,但是千万不要以为数据会被Ria Service带到SL客户端。

    四:SL获取主从表数据

    如果SL的某个功能需要同时得到主从表数据,我们必须同时提供包含这两个实体的域服务,在下图代码中,我们首先创建好这两个域服务(图中1)。然后,首先我们必须获取主表数据(图中2),其次是从表数据(图中3),最后,SL客户端要指定两者的关联(图中4):

    image

    获取主从表的全部代码如下:

    public class PrinSubVm : NotificationObject
        {
            public DomainServiceCategory DomainServiceCategory { get; set; }
            public DomainServiceProduct DomainServiceProduct { get; set; }
    
            private IList<Category> categoryWithProducts;
            public IList<Category> CategoryWithProducts
            {
                get { return categoryWithProducts; }
                set { categoryWithProducts = value; this.RaisePropertyChanged<IList<Category>>(() => this.CategoryWithProducts); }
            }
    
            public PrinSubVm()
            {
                DomainServiceCategory = new Web.DomainServiceCategory();
                DomainServiceProduct = new Web.DomainServiceProduct();
    
                //获取CategoryID(cid)为1的目录
                DomainServiceCategory.Load<Category>(DomainServiceCategory.GetCategoryWithProductsWithIDQuery(1), new Action<System.ServiceModel.DomainServices.Client.LoadOperation<Category>>(this.GetCategoryWithProductsWithIDCallBack), null);
    
                //获取CategoryID(cid)为1的目录下的商品
                DomainServiceProduct.Load<Product>(DomainServiceProduct.GetProductsByCategoryIDQuery(1), new Action<System.ServiceModel.DomainServices.Client.LoadOperation<Product>>(this.GetProductsByCategoryIDCallBack), null);
    
                //为多个域之间共享实体
                DomainServiceCategory.AddReference(typeof(Product), DomainServiceProduct);
    
            }
    
            void GetCategoryWithProductsWithIDCallBack(LoadOperation<Category> arg)
            {
                CategoryWithProducts = arg.Entities as IList<Category>;
    
            }
    
            void GetProductsByCategoryIDCallBack(LoadOperation<Product> arg)
            {
                //ProductAndCategorys = arg.Entities as IList<Product>;
            }
        }

    有一点我们必须注意,如果要获取从表的数据,仅获取需要的从表记录就可以了,不要加载全部记录,想想那些动辄几百万记录的业务表。SL客户端会自动根据实体的KEY值去关联。

    反过来,我们也可以实现从表关联主表。在这里就不一一举例了。但是最后的UI可以作为演示。

    image

    image

    image

    本文源码下载:SLOperation20110705.rar

  • 相关阅读:
    maven下载出错
    Android中TextView和EditView常用属性设置
    在python3.6环境下使用cxfreeze打包程序
    Python安装环境配置和多版本共存
    python manage.py migrate出错
    python使用pip安装模块出错 Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None))
    类的继承
    显示一张大图两张小图思路!banner数据库设计
    微信模板
    微擎小程序第二次请求 promise
  • 原文地址:https://www.cnblogs.com/luminji/p/2098210.html
Copyright © 2011-2022 走看看