如何在DomainService - WCF中传递多个参数

时间:2011-01-08 15:17:29

标签: c# silverlight wcf wcf-ria-services

假设我有一个窗口,应该在客户端提交3个模型(Silverlight客户端应用程序)。我的问题是每次提交表单时,我从客户端传递的服务器端的数据都是空的。

我使用了包含模型的嵌套类,而不是将多个对象作为参数传递,但它不再起作用。

我的人事数据传输对象代码是这样的:

[DataContract]
public class PersonnelDTO : EntityObject
{
    [Key]
    [DataMember]
    public int PersonnelId { get; set; }

    [Include]
    [DataMember]
    [Association("Personnel_ID", "PersonnelId", "Personnel_ID")]
    public Personnel Personnel { get; set; }

    [Include]
    [DataMember]
    [Association("Personnel_Info_ID", "PersonnelId", "Personnel_Info_ID")]
    public Personnel_Info PersonnelInfo { get; set; }
}

我填写此模型以将数据从客户端传递到服务器(DomainService)。 我的域名服务代码是:

    [Invoke]
    public void AddPersonnel(PersonnelDTO personnelDTO)
    {
        // Model are EMPTY in DTO
        ObjectContext.AddToPersonnels(personnelDTO.Personnel);
        ObjectContext.AddToPersonnel_Info(personnelDTO.PersonnelInfo);
        ObjectContext.SaveChanges();
    }

我不知道是否有办法在WCF Service方法中传递多个参数包括Generic List。

提前致谢。

1 个答案:

答案 0 :(得分:2)

首先,您不希望在服务方法上使用Invoke。你只想要一个Insert操作。所以你的方法应该是这样的:

public void InsertPersonnel(PersonnellDTO personnelDTO)

不需要[Insert]属性,因为RIA会根据方法命名的约定自动生成它。

您必须处理的下一个障碍是RIA如何处理密钥。它使用键来确定变更跟踪。通过DEFAULT - 如果认为您发送的对象不是新的,RIA会将EMPTY对象发送到服务层。它可以节省带宽。

您将对象包装在DTO中;根据我的经验,RIA在这种情况下表现不佳。它真正期望的是一个Personnel对象,PersonnelInfo对象作为子对象,PersonnelId作为键。然后,您需要设置与IsForeignKey = true的关联,以便正确更新密钥。

我将发布一个复杂的根聚合对象的示例,我将在一个示例应用程序中使用它,我将很快发布博客(我们正在使用RIA与POCO和Oracle并且它有效;但它需要一些计算出)。

[MetadataType(typeof (TicketMetadata))]
  public partial class Ticket
  {
    internal sealed class TicketMetadata
    {
      [Key] public int TicketId;

      [Required]
      public DateTime IncidentDate;

      [Required(ErrorMessage = "Missing Customer")] 
      public int CustomerId;

      [Required(ErrorMessage = "Missing Product")] 
      public int ProductId; 

      [Include]
      [Association("Ticket_Customer", "CustomerId", "CustomerId", IsForeignKey = true)]
      public Customer Customer;

      [Include]
      [Association("Ticket_Product", "ProductId", "ProductId", IsForeignKey = true)]
      public Product Product;

      [Include]
      [Composition]
      [Association("Ticket_TicketActions", "TicketId", "TicketId")]
      public List<TicketAction> TicketActions;
    }
  }

我建议查看关联和外键的工作方式,并重新思考对象结构,并可能远离DTO。做得对,整件事情做得很好。

相关问题