I'm using NH to map DB to an object named `location` which has some properties and on List `<Attachments>` attachments (one-to-many relation with instance of `lcocation`).
I use lambda expression to init `location` istance:
public IList<T> GetAllByExpression(Expression<Func<T,bool>> expression) { using (ISession session = NHibernateHelper.OpenSession()) { return session.Query<T>().Where(expression).ToList(); } }
at some later point in the code I try to access `location.attachments` collection but get an exception:
> {"Initializing[Location#543c7367-08fa-4469-a252-8e2c6163be22]-failed > to lazily initialize a collection of role: Location.Attachments, no > session or session was closed"}
I understand the session is closed, but there is no much I can do.
I understand the session is closed, but there is no much I can do.
I have a dilema: 1) The code uses the collection after fetching from the DB and needs the session to still be open. leaving the session open for the whole time is a bad practice. (without `using`).
2) Even if i leave the session open during the whole application runtime, I have build an n-tier web application. The DAL should be general and not expose some implementation's session (NH in this case).
3) Eager loading seems to be the solution, but it's wastefull - as I'll have to eager load all the data in case of an object with list of other objects in it. (And that can even continue recurrsively)
What should I do?
TIA
|