I have a POCO classes that I am using with NHibernate in a WCF service layer. And I am thinking about trying to send the NHibernate proxy classes down to a client. This is a client that I control. We handle record updates with a system wide reservation so there can only be one writable copy of this entity sent to a client at any given time. My basic goal is to take advantage of NHibernates change tracking so I don’t need to fetch a copy from the database and replay the changes the client made to update the db.
Assuming I could get this to work what are the cons of this approch?
Lazy-loading will give you the biggest headache.
Some of the properties on your objects will contain NHibernate proxies, which are attached to the ISession. All of this will travel back to the client. When the client attempts to deserialise your objects, the proxies get invoked and attempt to retrieve more data. BOOM!
There are several workarounds people have posted (Google “nhibernate wcf” or “nhibernate wcf lazy loading“):
http://lunaverse.wordpress.com/2007/05/09/remoting-using-wcf-and-nhibernate/
http://timvasil.com/blog14/post/2008/02/WCF-serialization-with-NHibernate.aspx
http://whiletrue.nl/blog/?p=37
http://trentacular.com/2009/08/how-to-use-nhibernate-lazy-initializing-proxies-with-web-services-or-wcf/
I’m currently working on swapping out the proxies with custom ones, so they make a WCF call instead of triggering the Nhibernate lazy-load.