1

我有一些 POCO 对象设置为与 Entity Framework Code First 一起使用。

我想从我的 ASP.NET MVC 4 网站中的 ApiController 返回其中一个对象,然后在客户端应用程序中使用它。

我最初在服务器端对对象的序列化有问题,因为实体框架妨碍了(请参阅Can an ApiController return an object with a collection of other objects?),它试图序列化 EF 代理对象而不是普通的 POCO 对象。所以,我在我的 DbContext 中关闭了代理生成以避免这种情况 - 现在我的序列化对象看起来不错(在我看来)。

有问题的对象是“标签”——这是我的 POCO 类:

public class Tag
{
    public int Id { get; set; }

    public int ClientId { get; set; }
    public virtual Client Client { get; set; }

    [Required]
    public string Name { get; set; }

    [Required]
    public bool IsActive { get; set; }
}

相当标准的东西,但请注意 ClientId 和 Client 成员。这些是 EF Code First“导航”属性。(每个标签只属于一个客户)。

这是我从我的 ApiController 中得到的:

<Tag xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Foo">
  <Client i:nil="true"/>
  <ClientId>1</ClientId>
  <Id>1</Id>
  <IsActive>true</IsActive>
  <Name>Example</Name>
</Tag>

Client 成员为零,因为禁用了代理生成,我没有自动加载引用的对象。这很好,在这种情况下 - 我不需要客户端的数据。

所以现在我试图在客户端反序列化这些对象。我曾希望我能够在客户端应用程序中重用相同的 POCO 类,而不是创建新类。干燥等等。所以,我正在尝试:

XmlSerializer xmlSerializer = new XmlSerializer(typeof(Tag));

var tag = xmlSerializer.Deserialize(stream);

但是我遇到了两个问题,这两个问题都是由于 EF Code First 约定:

问题 1:因为我的 Tag 类有一个 Client 成员,所以 XmlSerializer 抱怨它不知道如何反序列化它。我想这很公平(尽管我曾希望因为 XML 中的成员是 Nil,所以它不会在意)。我可以在 XmlSerializer 构造函数中传递额外的类型,当我尝试这样做时,它会抱怨 Client 使用的其他类。由于 Client 引用了各种其他对象,我最终不得不将它们全部传入!

我尝试使用 [DataContract] 和 [DataMember] 属性从 XML 中删除 Client 成员(通过不将其标记为 DataMember)。这确实将它从 XML 中删除,但并没有阻止 XmlSerializer 抱怨它。所以我想问题不是在 XML 中,而是在类定义中。

问题 2:当我尝试将 typeof(Client) 作为额外类型传入时,它还抱怨它无法反序列化该类,因为它包含一个接口成员。那是因为 - 再次由于 EF Code First 约定 - 它有一个 Tags 成员,如下所示:

`public virtual ICollection<Tag> Tags { get; set; }`

所以看起来即使我解决了引用类型的问题,我仍然无法使用我的 POCO 类。

Is there a solution to this, or do I have to create new DTO classes purely for use at the client side, and return those from my ApiController?

4

1 回答 1

0

I just tried using DataContractSerializer instead of XmlSerializer, and for the Tag class that seems to work. I've yet to try it with a class that has a virtual ICollection<T> member...

Update: tried it, and it "works". It still manages to reconstruct the object, and leaves the ICollection member at null.

Update 2: OK, that turned out to be a dead end. Yes, it meant that I could correctly serialize and de-serialize the classes, but as everyone kept telling me, DTO classes were a better way to go. (DTO = Data Transfer Objects - classes created specifically for transferring the data across the wire, probably with a subset of the fields of the original).

I'm now using AutoMapper (thanks Cuong Le) so that I can easily transform my POCO entities into simpler DTO classes for serialization, and that's what I'd recommend to anyone faced with the same problem.

于 2012-08-01T13:54:48.580 回答