Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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.
  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.

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.

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top