Letting Ninject manage my transaction state, practice concerns

半城伤御伤魂 提交于 2019-12-03 06:00:28

I'm no expert (and have no experience with ninject), but I do agree with your 3 conclusions, and that's what I do in my projects.
Another thing I can add is that, in my opinion, transactions should be controlled EXPLICITLY and per operation, and not globally (start and beginning of request and commit at the end) like your code suggests.
This is because I believe you want to control your transaction's behaviour- commit or not (or maybe not even start, if no DB access is necessary) for every operation individually.
What I use is a management (or workflow, if you prefer) layer, which is responsible just for that. for example:

public class SomeManager : ManagersBase
{
    public void DoSomething(DomainObject obj)
    {
        if (obj.Operation())
        {
            using (ITransaction tx = Session.BeginTransaction())
            {
                try
                {
                    Session.Update(obj);
                    tx.Commit();
                }
                catch (MeaningfulException ex)
                {
                    //handle
                    tx.Rollback();
                }
            }
        }
    }
}

hope this helps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!