c# - How to manage NHibernate sessions when using Repositories/Transactions -


i'm getting stuck nhibernate @ moment , i'm trying work out how best correctly design repositories whilst correctly managing lifetime of session.

from examples i've seen, seems common practise inject isession each of repositories follows:

public class somerepository : irepository {     private readonly isession _session;      public somerepository(isession session)     {         _session = session;     }      public ilist<t> loadsomedata()     {         return _session.query<t>().where(...).tolist();     } } 

so fine, can see couple of cases there problems:

1) there may not 1 session lifetime of app.

2) when accessing nhibernate should wrap our calls in transaction - if transaction faults must rollback transaction , close session.

in both these cases repository defunct references closed session.

my particular application long running process, call nhibernate, expect high turnover sessions instead of keeping 1 session open lifetime of app.

i therefore wondering established pattern dealing particular situation? see several potential solutions, it's hard see best practise:

1) rebuild repositories (and create new session) every time process needs db work.

2) inject repositories sessionfactory. repository exposes sessionfactory. consumer of each repository opens new session @ same time starting transaction.

3) create unitofwork class injected repositories , responsible managing session lifetimes & transactions. each time call repository made, calls unitofwork, creates brand new session , call executed within transaction. repositories therefore have no knowledge of sessions/transactions.

to me, 3) seems best solution, , i've seen few examples of online, in of examples create 1 session within unitofwork, , don't recreate if transaction rolled back.

in addition, limitation 3) unitofwork tied particular repository, , couldn't have transaction made calls different repositories.

hope makes sense , appreciate guidance.

thanks

actually session in nhibernate can used unitofwork. if want use pattern, here's implementation: http://nhibernate.info/doc/patternsandpractices/nhibernate-and-the-unit-of-work-pattern.html.

but best solution in list 1. need create session , repositories every time need make job db.

using unitofwork looks like:

using(var uow = new unitofwork()) {    var rep1 = new somerepository1(uow);    rep1.dosomejob();     var rep2 = new somerepository2(uow);    rep2.dosomeotherjob(); } 

using native isession:

using(var session = sessionfactory.opensession()) using(var tr = session.begintransaction()) {    var rep1 = new somerepository1(session);    rep1.dosomejob();     var rep2 = new somerepository2(session);    rep2.dosomeotherjob();     tr.commit(); } 

also can use power of di/ioc containers manage sessions lifetime.

if interested in last option can show example to.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -