Letting Ninject manage my transaction state, practice concerns

后端 未结 1 2084
青春惊慌失措
青春惊慌失措 2021-02-08 03:36

I\'m letting Ninject manage my ISession and ITransaction state in Fluent nHibnerate with the following registration method - I am wondering if it is su

1条回答
  •  醉话见心
    2021-02-08 03:54

    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

    0 讨论(0)
提交回复
热议问题