Raif
Raif

Reputation: 9259

Nhibernate, determine if lazyloaded collection has been retrieved yet

I have a validation engine that iterates through all the properties of an object and checks if they are valid.

If I get an Entity and change a simple property and run it through the engine it hydrates all of the lazyloaded collections.

In other words it gets the collection and iterates through the entities in that collection. If the collection has already been retrieved than that is fine but if it has not been retrieved then there is no reason it would be invalid so there is no reason to go get it.

In short, can I inspect a collection and see if it has been retrieved yet. If I can do that then I can skip or iterate that collection accordingly.

Upvotes: 4

Views: 880

Answers (1)

Najera
Najera

Reputation: 2879

Use NHibernateUtil:

var isInitialized = NHibernateUtil.IsInitialized(entity.Collection);

Upvotes: 5

Related Questions