开发者

ASP.NET MVC 2 , Nhibernate and transaction across layers

I am creating a solution in ASP.NET MVC 2, NHibernate and DDD. I am using a semi CQRS type Model.

ASP.NET Controller send validated messaged to Service Layer which updates state of an Domain object. I have my Domain Dispatch "Events" and these are then caught by "Event Handlers" who act on them. Each of these Event Handler have access to Repository Layer and can commit an Domain Object State.

Event Handlers also insert records directly into reporting bas开发者_运维知识库ed tables using a (non NHibernate ) Repository. Event Handlers may also do non database related operations like sending emails.

Event Handler can also change state of an object thereby creating new set of events.

How can I assure that all database operation that occur during a single asp.net Request are inside a single Transaction. I have been reading some blogs ( like Kevin Williams , Matt Wrock and Davy Brion) and have got information on how to start a Session object in Begin and End Request ( Again I will be using Structure Map here) but not sure how the transaction is maintained. This was compounded by the fact that start and end Requests may be called on different threads.

My Repository Class takes NHibernate ISession in its Parameter. If I create ISession as Hybrid Scope ( StructureMap) will that ensure that during a request ISession parameter that is passed by StructrueMap remain same.

Please advise and also let me know if my question is not clear.

Thank you,

Mar

The Mar


You can consider implementing the Unit Of Work pattern for each web request. The unit of work creates an NHibernate session and also handles transactions. There are several implementations that you can find on the web such as this and this.


in addition to WorldIsRound's answer- you haven't specified in your description where transactions are created and commited (from your explanation i'm assuming it should be somewhere in the Service layer).
I konw that there are tools that'll manage your sessions, and maybe your transactions, for you, but in my opinion, you want to explicitly control your transactions.
I use UOW pattern in my projects as well, to create a Session object in the Begin_Request event, and then I use that object to create transactions when I need them.
Again, that's just my opinion, but I think you should have complete control over opening and closing your transactions.
Good luck

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜