Prevent lazy loading in nHibernate?

If you want to defer loading, then set lazy="true" in your map.

I've tried lazy="true" and lazy="false". Neither give the desired effect. I don't want the DocumentContent to be loaded - lazily or otherwise unless I explicitly do it via a select criteria.

I've used LLBL Gen previously and in the adapter model the relationships are not loaded unless explicitly requested via a Prefetch. I want to do something similar in nbernate – Ciaran O'Neill May 12 '10 at 6:19.

You should use DTOs for your service and leave all with lazy = true instead of serializing you domain model. It's a big performance gain.

I don't want to change my app over to DTO's now. A lot of my classes are quite lightweight and in most cases are working perfectly – Ciaran O'Neill May 13 '10 at 1:26 1 Then use DTOs for the one's that aren't working perfectly. – James Gregory May 24 '10 at 2:36.

First rule of thumb: do not distribute your domain objects martinfowler.com/bliki/FirstLaw.html.

2 Following every law blindly isn't always possible. – Ciaran O'Neill May 24 '10 at 2:13.

The scenario you have described is exactly what lazy loading is designed for. You want to display a list containing summary information and when required load the heavier stuff. This is lazy loading.It is "lazy" because it avoids doing extra work until it absolutely needs to.

What you want is for the JrmDocumentContent to be loaded lazily and you're well on th way to that. To get this you have to remove the lazy=false. Lazy=true is the default in nhibernate but you may put lazy=true to be sure.

You'll have to restore the virtuals though. Loading blobs lazily like this or any property for that matter, I think is supported in nhibernate's latest release now by setting lazy=true on the property definition. The method you have used by separating the heavy content into a separate class/table was the only way to lazily load a blob before.

I don't know what version of nhibernate you're using but the strategy you have adopted is correct. You should embrace lazy loading. You also need to remove lazy=false on the Content property of the JrmDocumentContent class.

Apart from that, I see no other reason why it shouln't work.

I've had exactly this problem. Here's what I've done: Create my own custom proxy (using Castle DynamicProxy, but you can try others) that returns null on all un-initialized properties and collections, and returns a copy of each initialized collection. I wrap each root object result in my service with such a proxy.

Pass this proxy as the return value of my service - almost works, except that I couldn't get my proxies themselves to be serialized correctly. That's why I tried method 2: (Horrible) Deeply clone all my entities into new objects, skipping uninitialized properties and collections. This works, but sucks.

Currently I'm trying to use DataContractSurrogate to solve the problem in 1. Or to replace the use of proxies altogether. The problem I'm currently trying to solve is that you can't "filter" object properties - you can't return null from GetObjectToSerialize - which is exactly what I want to return if the object being serialized is an uninitialized Nbernate proxy or collection.

I'll update this answer when I progress. Currently I'm using solution 2 as a temporary workaround. I'd share the code but it's a mess currently (since I wasn't intending deep-cloning to be the solution).

This was solved usint the DataContractSurrogate. I can check if something is a proxy and overwrite the property and set it to a new default instance. I.e.

Var proxy = obj as IN if (proxy! = null) { var initializer = proxy. If (initializer.

IsUninitialized) { return Activator. CreateInstance(obj.GetType(). BaseType); } else { return initializer.

GetImplementation(); } } – Ciaran O'Neill Apr 1 at 4:26 I've considered this solution, but then we serialize empty instances instead of nulls. Not only is this a waste in communications, it also means the client can't tell whether the object it received was really not fetched from the DB, or simply has all its fields set to default values (empty instance) in the database. – sinelaw Apr 3 at 7:18 BTW, instead of running a deep clone I now pass my custom proxies directly to the serializer in combination with a DataContractSurrogate that just replaces the contract type of the custom proxy to the base class (proxied class) type.

The Nbernate proxies contained within are automatically masked out by my custom proxy. Still not a perfect solution, but at least the resulting communication is what I want it to be, and no deep clone is necessary. – sinelaw Apr 3 at 7:21.

I had a problem with where my relationship was being managed (inverse=true) and also with my session instantiation as it was being injected in. Using that in conjunction with a custom DataContractSurrogate that prevented some of the lazy loading issues now works. Thanks var proxy = obj as IN if (proxy!

= null) { var initializer = proxy. If (initializer. IsUninitialized) { return Activator.

CreateInstance(obj.GetType(). BaseType); } else { return initializer. GetImplementation(); } }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions