I think you should review your session management.

Usually, it's not a very good idea to manage the ISession in scope of one
IRepository call. A better way should be externalizing that and manage
sessions at higher level: remote/web call, at service boundary - it depends
on the type of your application.

Simplified, this means that at this point:
public Person GetPersonById(int id)
your session should be already opened and will be closed (and changes
committed) right after call.

Also, try google for "NHibernate session management", it will give you more
ideas how people are doing it...


On Thu, Aug 5, 2010 at 11:16 AM, Luka <[email protected]> wrote:

> Hi,
> I have a project where i am using repositories to access my data with
> nhibernate.
> So I have an IRepository<TDomain, TKey> interface and its
> implementation.
> This repository is from CodeSmith template and it has incapsulated
> session management from that template.
> Now when I want to use it I do:
>
> public Person GetPersonById(int id)
> {
>   Person person = null;
>   using (var rep = ObjectFactory.GetInstance<IRepository<Person,
> int>>())
>   {
>       person = rep.GetById(1);
>   }
>   return person;
> }
>
> now I have the person object but the problem is this:
> the Person class has a collection of Addresses that is lazy
> initialized.
> So when in the code I do:
>
> Person person = GetPersonById(1);
> foreach(var address in person.Addresses)
> {...}
>
> I get lazy initialization exception because there is no more session
> open.
>
> What must I do,  how must I use this repository so I wont get lazy
> exceptions?
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<nhusers%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to