简体   繁体   English

当我不想跟踪实体以后时,如何通过WCF服务发送POCO实体?

[英]How can I send POCO Entities through WCF Service when I don't want to track the entity *later*?

I have an ASP.NET MVC 4 project, where Controller calls a WCF Service layer, that calls Business Layer, that use a Repository of EF 5.0 Entities. 我有一个ASP.NET MVC 4项目,其中Controller调用WCF服务层,调用业务层,该层使用EF 5.0实体的存储库。 Then the results are returned as POCO entities to the Controller. 然后将结果作为POCO实体返回给控制器。

It works fine while the WCF Service is directly referenced as a Library, but I know it won't work referenced as a Service because they will need to be serialized, and with ProxyCreation enabled this is not possible. 当WCF服务被直接引用为库时,它可以正常工作,但是我知道它不能被引用为服务,因为它们需要进行序列化,并且启用ProxyCreation时,这是不可能的。

I don't want to create DTOs because I use generated POCO entities, that's why they exist in my humble opinion. 我不想创建DTO,因为我使用生成的POCO实体,这就是为什么它们存在于我的拙见中。 I want to track changes only before the POCO entities reach Service layer. 我只想在POCO实体到达服务层之前跟踪更改。

A lot of people talk about using DTOs even when they are identical to POCOs, if I do that, I could create auto-generated copied classes just with different names to be a "Proxy disabled POCO as DTO", what would be a little strange. 很多人都在谈论使用DTO,即使它们与POCO相同,如果这样做,我可以创建具有不同名称的自动生成的复制类,成为“代理禁用POCO作为DTO”,这有点奇怪。

Could I kill the proxy class of a POCO, in a way the object could be serialized when returned from the Service layer? 我可以通过从服务层返回对象时将其序列化的方式来杀死POCO的代理类吗?

Also I don't know if this idea is a good practice. 我也不知道这个主意是否是一个好习惯。 But would be great to send "clean" entities to my Controllers, ready to me mapped to ViewModels. 但是,将“干净的”实体发送​​到我的Controllers(将其准备好映射到ViewModels)就好了。 I'm looking for performance too. 我也在寻找性能。

The problem is solved using ProxyDataContractResolver . 使用ProxyDataContractResolver解决了该问题。 We must use [Serializable] and [DataContract(IsReference=true)] too. 我们也必须使用[Serializable] and [DataContract(IsReference=true)] With this combination, ProxyCreation can be enabled. 通过这种组合,可以启用ProxyCreation。

The way we handled this was by doing the following: 我们处理此问题的方法是执行以下操作:

  1. Customize the T4 generating the POCO classes so that it generates classes decorated with [Serializable()] and [DataContract(IsReference=true)] attribute. 自定义T4生成POCO类,以便它生成以[Serializable()] and [DataContract(IsReference=true)]属性[Serializable()] and [DataContract(IsReference=true)]
  2. Both frontend (views) and backend (wcf service / business layer) references the POCO generated classes, since you won't be using proxy due to IsReference=true. 前端(视图)和后端(wcf服务/业务层)都引用POCO生成的类,因为由于IsReference = true,您将不使用代理。

and that's basically it. 基本上就是这样。

With this, you don't have to create DTO and just use the POCO classes both in backend and frontend. 这样,您不必创建DTO,而只需在后端和前端都使用POCO类。

Keep in mind though, that WCF using IsReference=true handles does not like redundant objects (so this would be an issue on some POCO classes with navigation properties). 但是请记住,使用IsReference = true句柄的WCF不喜欢冗余对象(因此,这对于某些具有导航属性的POCO类是一个问题)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM