2011-08-28 67 views
2

我有一个DTO这里面出现一个集合另一个DTO我填充服务器端和客户端发送的。但是,这个内部DTO集合不会返回给客户端。的DTO和WCF RIA

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

我的理解错误,我试图实现,如果不是我如何获得WCF RIA发送这个内部的DTO集合。

我应该补充说我正在使用automapper,并希望使用它来实现它。

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

  1. 员工拥有的能力。
  2. 该雇员需要他们的工作能力。
  3. 的差距,这是1和2
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值。

+0

也许如果你包含一些代码?您是否创建了ria服务或自己编写它们?你使用linq2sql或实体框架? – Geoff

+0

ria服务已经产生了一些手工制作,这更像是一个关于如何在真正的DTO中使用DTO和RIA的一般问题,也就是一个数据桶,其中的数据并不一定都是相关的,但据我所看到的任何内部DTO需要与父DTO相关。谢谢。 – David

+0

你能表现出任何的代码?也许尝试用更简单的DTO对象复制问题并粘贴它? –

回答

0

如果您使用ADO.Net实体数据模型并针对它们使用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.