Search code examples
wcfentity-frameworkpocoproxy-classes

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. Then the results are returned as POCO entities to the Controller.

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.

I don't want to create DTOs because I use generated POCO entities, that's why they exist in my humble opinion. I want to track changes only before the POCO entities reach Service layer.

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.

Could I kill the proxy class of a POCO, in a way the object could be serialized when returned from the Service layer?

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. I'm looking for performance too.


Solution

  • The problem is solved using ProxyDataContractResolver. We must use [Serializable] and [DataContract(IsReference=true)] too. With this combination, ProxyCreation can be enabled.