Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nhibernate transactions:Avoiding Nhibernate dependency in the service layer

One of practices advocated by Nhibernate experts is to do all actions in inside a transaction.

In a typical 3 tier web architecture, Nhibernate depedency is limited the data layer.

In such a case is it fine to use

  using (var tr = NHibernateSession.Current.BeginTransaction()) {

at the controller level. Won’t this bring in a dependency on Nhibernate to the service layer?

like image 816
Quintin Par Avatar asked Dec 29 '22 21:12

Quintin Par


1 Answers

One way around this is the wrap the NHibernate session and transaction semantics in your own abstrated Interface / implementation class. This way if you ever wanted to say, switch to Linq2Sql, you could create a L2S implementation. However, this still means that transaction semantics will still be in the service layer, but not NHibernate specific calls. Google IRepository<T> for many examples of how to do this.

However, if you don't plan on switching out your ORM in the future, one could argue that having NHibernate in the service layer isn't necessarily a bad thing since NHibernate in itself is the abstraction over the data layer.

like image 75
Daniel Auger Avatar answered Dec 31 '22 13:12

Daniel Auger