Correct use of the NHibernate Unit Of Work pattern and Ninject

前端 未结 3 794
余生分开走
余生分开走 2020-12-07 22:17

I have the following implementation and would like some feedback as to whether it makes correct use of NHibernate for sessions and transactions.

public inter         


        
3条回答
  •  执笔经年
    2020-12-07 22:34

    I have a mostly CRUD type of application, and I implemented the Unit Of Work with Repository pattern, but couldn't really get away from the Session/Transaction split. Sessions and Transactions need different lifetimes. In the desktop world, a Session is usually "per-screen" and a Transaction is "per-user-action".

    More information in this excellent article.

    So what I ended up with was:

    • IUnitOfWork -> Wraps session, implements IDisposable
    • IAtomicUnitOfWork -> Wraps transaction, implements IDisposable
    • IRepository -> Provides Get, Save, Delete and query access

    I made it so that you need an IUnitOfWork to build an IAtomicUnitOfWork and you need an IAtomicUnitOfWork to build an IRepository, so that enforces proper transaction management. That's really all I gained by implementing my own interfaces.

    As jeroenh said, you are almost just as well to use ISession and ITransaction but in the end I felt a little better writing all my code against an interface that I defined.

提交回复
热议问题