Need help understanding Nhibernate Transactions and how not to duplicate code (if possible)?

The recommended practice when using the ISession directly is this: (you should use a transaction even for reads).

The recommended practice when using the ISession directly is this: (you should use a transaction even for reads) using(var session = factory.OpenSession()) using(var tx = session. BeginTransaction()) { // all the code that uses the session goes here // use session to load and/or save entity } This is basically creating an unit of work. Depending on your operation context ( web request, wcf request ) you might want to have all the operation inside a single unit of work.

For web request see this for WCF operation see this. Also as Sixto Saez said calling SaveOrUpdate is a smell. In most of the cases you have one of these two cases: 1 You create a new entity and call session.

Save(entity); or 2 You get an entity or more from the session ( with Get/Load or by query ), modify the entity and the changes will be saved by nhibernate on session disposal. ( unless you change the session flush mode but that is not the point ).

I would steer clear of micro-managing the transaction within a repository like that. This post does a good job of explaining why adding database semantics to your repository is not a good idea when using Nbernate (or any other ORM). The best practice is to implement a unit of work pattern for your business logic.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions