Stop lazy loading or skip loading a property in NHibernate? Proxy cannot be serialized through WCF

Posted by HelloSam on Stack Overflow See other posts from Stack Overflow or by HelloSam
Published on 2010-03-07T17:31:16Z Indexed on 2010/03/08 4:36 UTC
Read the original article Hit count: 877

Filed under:
|
|
|

Consider I have a parent, child relationship class and mapping. I am using NHibernate to read the object from the database, and intended to use WCF to send the object across the wire.

Goal

  • For reading the parent object, I want to selectively, at different execution path, decide when I would want to load the child object. Because I don't want to read more than what I needed.
  • Those partially loaded object must be able to sent through WCF. When I mean I don't load it, neither side will access such property.

Problem

  • When such partially loaded object is being sent through WCF, as those property is marked as [DataContract], it cannot be serialized as the property is lazy load proxy instead of real known type.

What I want to archive, or solution that I can think of

  • lazy=false or lazy=true doesn't work. Former will eagerly fetch all the relationships, latter will create a proxy. But I want nothing instead - a null would be the best.
  • I don't need lazy load. I hope to get a null for those references that I don't want to fetch. A null, but not just a proxy. This will makes WCF happy, and waste less time to have a lazy-load proxy constructed.
    Like could I have a null proxy factory?
    -OR-
  • Or making WCF ignoring those property that's a proxy instead of real. I tried the IDataContractSurrogate solution, but only parent is passed to GetObjectToSerialize, I never observe an proxy being passed through GetObjectToSerialize, leaving no chance to un-proxy it.

Edit

After reading the comments, more surfing on the Internet...

It seems to me that DTO would shift major part of the computation to the server side. But for the project I am working on, 50% of time the client is "smarter" than the server and the server is more like a data store with validation and verification. Though I agree the server is not exactly dumb - I have to decide when to fetch the extra references already, and DTO will make this very explicit.

Maybe I should just take the pain. I didn't know http://automapper.codeplex.com/ before, this motivates me a little more to take the pain.

On the other hand, I found http://trentacular.com/2009/08/how-to-use-nhibernate-lazy-initializing-proxies-with-web-services-or-wcf/, which seems to be working with IDataContractSurrogate.GetObjectToSerialize.

© Stack Overflow or respective owner

Related posts about c#

Related posts about wcf