DTO和WCF RIA

时间:2011-08-28 12:58:54

标签: wcf ria automapper dto

我有一个DTO,其中包含另一个DTO的集合,我在服务器端填充并发送给客户端。但是,此内部DTO集合不会返回给客户端。

我相信我需要使用[Include]和[Association]属性,以便WCF RIA服务知道该怎么做,但是我的问题是主DTO和内部DTO集合之间没有真正的关联,我只是用它来汇总各种来源的数据,以便返回客户端。

我的理解是错误的,如果不是我如何让WCF RIA发送这个内部DTO集合。

我应该补充一点,我使用的是自动播放器,并希望使用它来实现它。

这是一个例子,我想在一个块中发送回客户端;

  1. 员工的能力。
  2. 员工对工作所需的能力。
  3. GAP,即1和2之间的差异。
  4. public class CompetencyRequirementsDto
    {
        [Key]
        public string CompanyId { get; set; }
        [Key]
        public string EmployeeNo { get; set; }
    
        public string JobId { get; set; }
    
        [Include]
        [Association("EmployeeCompetencies","CompanyId, EmployeeNo","CompanyId, EmployeeNo")]
        public IList<EmployeeCompetencyDto> EmployeeCompetencies { get; set; }
        [Include]
        [Association("JobCompetencies","JobId, CompanyId","JobId, CompanyId")]
        public IList<JobCompetencyDto> JobCompetencies { get; set; }
        [Include]
        [Association("CompetencyGap", "JobId, CompanyId", "JobId, CompanyId")]
        public IList<JobCompetencyDto> CompetencyGap { get; set; }
    } }
    

    现在第1项工作正常,但2和3没有?我发现我的DTO是在服务器端创建的,但当它到达客户端时CompetencyGap(即使它没有值)已经 已获得JobCompetencies值。

1 个答案:

答案 0 :(得分:0)

如果您正在使用ADO.Net Entity数据模型并对其使用RIA服务,那么您可以选择创建关联的元数据。

因此,要在客户端获取引用实体,我们需要修改相应的元数据以及提取数据的域服务类的功能。

Here I am giving an example...

1. Just add [Include] attribute at the the top of  the referenced data for example.

[MetadataTypeAttribute(typeof(Customer.CustomerMetadata))]
    public partial class Customer
    {

        // This class allows you to attach custom attributes to properties
        // of the Customer class.
        //
        // For example, the following marks the Xyz property as a
        // required property and specifies the format for valid values:
        //    [Required]
        //    [RegularExpression("[A-Z][A-Za-z0-9]*")]
        //    [StringLength(32)]
        //    public string Xyz { get; set; }
        internal sealed class CustomerMetadata
        {

            // Metadata classes are not meant to be instantiated.
            private CustomerMetadata()
            {
            }

            public int CustomerID { get; set; }

            public string EmailAddress { get; set; }

            public string FullName { get; set; }

            [Include]
            public EntityCollection<Order> Orders { get; set; }

            public string Password { get; set; }
        }
    }


2. Modify the function in the domain service and add include there also for example. 

     public IQueryable<Customer> GetCustomers()
        {
             var res = this.ObjectContext.Customers.Include("Orders");
             return res;
        }

  In your case the first part is done you just need to modify your domain service query to get reference entities.