Well familiar with Webforms and Linq, I am new to ASP.NET MVC and NHibernate World. I worked on a project using examples from Bob Cravens . My application mainly reads and does not write sequentially, so usually I will not use transactions. But to implement the "Unit of Work" template, all my research, including the Ayende blog , I have to say.
I have a problem -
- Ninject creates a session and opens a transaction.
- Ninject injects repositories into services and services into controllers.
- I make some changes to the properties and child objects of the object and save them in the consolidated root. This calls Transaction.Commit (works fine, as expected)
- In another method, later in the controller, I try to save a separate object
- The second call fails because the transaction is no longer active.
I am going to add a “CommitNeeded” bool to UnitOfWork, which will be set by my Save () method and conditionally call Commit () on UnitOfWork.Dispose (). Is that a good idea?
Should I remove the transaction infrastructure? Should I change my Commit () to Flush ()?
Any tips to help fix my anti-pattern would be appreciated.
In response to the comments - I think I do not mind if they occur together or separately. Two things happen. The first modifies the Client object and then saves it. The second one records the record, which then calls the same Save method.
var Customer = ServiceLayer.GetCustomer(...); Transmogrify(Customer, Customer.Children, Widgets, ...); ServiceLayer.Save(Customer) ServiceLayer.RecordEvent(Customer, "Customer was frobbed")
where LogEvent looks like
public void RecordEvent(Customer customer, int eventTypeId, string description) { ... Save(customer); }
The RecordEvent method has its own “save” because it is called from other controllers that do not modify the data. I believe that the Save call does not belong in any of these places. The question is where? Service Level Dispose () Method? or Filter, like other users?