当提交NHibernate的交易? [英] When to commit NHibernate Transaction?

查看:131
本文介绍了当提交NHibernate的交易?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然很熟悉WebForms和LINQ的,我是新手到ASP.NET MVC和NHibernate世界。我已经通过一个项目使用鲍勃奎文'例子已经工作。我的应用程序主要是读取和非连续写入,所以通常我不会用一个交易。但要实现单位按工作格局,我的所有研究包括的 Ayende的博客说我应该。

While very familiar to Webforms and Linq, I am a novice to the ASP.NET MVC and NHibernate World. I have been working through a project using Bob Cravens' examples. My application is mostly reads and non-sequential writes, so typically I would not use a transactions. But to implement the Unit-of-Work pattern, all my research including Ayende's blog says I should.

我的问题是这样的 -

The problem I have is this -


  • Ninject创建一个会话,并打开一个交易。

  • Ninject库注入到服务,服务到控制器。

  • 我做一些改变一个对象的属性和孩子,节省总根源。这就要求器transaction.commit(正常工作,如预期)

  • 在另一种方法后来在控制器中,我尝试保存一个独立的对象

  • 由于事务不再处于活动状态的第二个调用失败。

我想加一个boolCommitNeeded来)上UnitOfWork.Dispose将由我的Save()方法来设置和有条件触发提交的的UnitOfWork(()的。这是一个好主意?

I'm thinking of adding a bool "CommitNeeded" to the UnitOfWork which would be set by my Save() method and conditionally trigger a Commit() on UnitOfWork.Dispose(). Is this a good idea?

我应该删除事务基础设施?我应该改变我的提交()的冲洗()的?

Should I remove the transaction infrastructure? Should I change my Commit()'s to Flush()'s?

任何意见,这将有助于解决我的反模式是AP preciated。

Any advice that would help fix my anti-pattern would be appreciated.

在回应的意见 - 我想,如果他们发生一起或单独我不介意。有两件事情怎么回事。第一个变化是客户对象,然后将其保存。第二,使一个日志条目,然后调用相同的保存方法。

In response to the comments - I guess I don't mind if they happen together or separate. There are two things going on. The first one changes a "Customer" object, and then saves it. The second makes a logging entry 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")

在这里的LogEvent看起来像

where LogEvent looks like

public void RecordEvent(Customer customer, int eventTypeId, string description)
{
    ...

    Save(customer);
}

该RecordEvent方法都有自己的拯救,因为它是从做任何数据变化其他控制器调用。我相信保存调用不会在任何这些地方的归属。现在的问题是在哪里?服务层的Dispose()方法?或者像其他用户过滤器建议?

The RecordEvent method has its own "save" because it is called from other controllers that do no data changes. I believe the Save call doesn't belong in either of those places. The question is, where? The Dispose() method of the Service Layer? or a Filter like the other users suggested?

推荐答案

使用ASP.NET MVC中,我用一个动作过滤器事务范围到控制器动作执行的生命周期绑定。这个伟大的工程的大部分时间,但你必须要小心,不要让交易开得长。

Using ASP.NET MVC, I use an action filter to bind the transaction scope to the controller action execution lifecycle. This works great most of the time, but you have to be cautious not to keep transactions open too long.

public class UnitOfWorkActionFilter : ActionFilterAttribute
{
    public IUnitOfWork UnitOfWork { get; set; }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        UnitOfWork.Start();
        base.OnActionExecuting(filterContext);
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (filterContext.Exception == null)
        {
            UnitOfWork.Commit();
        }
        else
        {
            UnitOfWork.Rollback();
        }
        UnitOfWork.Dispose();
        base.OnActionExecuted(filterContext);
    }
}

在我的情况下,我通过自定义使用属性注入 ControllerActionInvoker 来获得 IUnitOfWork 依赖到 ActionFilterAttribute

In my case I'm using property injection via a custom ControllerActionInvoker to get the IUnitOfWork dependency into the ActionFilterAttribute.

这篇关于当提交NHibernate的交易?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆