0

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
eduardobr
  • 146
  • 15
  • "... 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." What Do you mean? Are you saying that the POCO classes won't serialize? – Umair Ishaq Nov 28 '12 at 18:08
  • Yes, won't serialize if the instance is generated by an EF query and ProxyCreationEnabled = true (default). If the instance is created manually, it serializes, because there isn't a proxy tracking changes. – eduardobr Nov 28 '12 at 18:35
  • What are you using proxies for - change tracking or lazy loading? I think you either need to disable proxy generation or clone the object to its base type instance. – Pawel Nov 28 '12 at 21:12
  • I'm testing the [ProxyDataContractResolver](http://blogs.msdn.com/b/adonet/archive/2010/01/05/poco-proxies-part-2-serializing-poco-proxies.aspx), this class is ready in .NET 4.5... I'm going to check if there is any side effect and post the results here – eduardobr Nov 29 '12 at 11:42

2 Answers2

1

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

eduardobr
  • 146
  • 15
0

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

jr pineda
  • 186
  • 7
  • [DataContract(IsReference=true)] works fine with Proxy disabled, but I want to take advantage of the ProxyCreaton, before transferring the object – eduardobr Nov 29 '12 at 15:29