ASP.NET MVC 3、动作过滤器和 Autofac 依赖注入 [英] ASP.NET MVC 3, Action Filters, and Autofac Dependency Injection

查看:32
本文介绍了ASP.NET MVC 3、动作过滤器和 Autofac 依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET MVC 2 我有一个名为 [Transaction]ActionFilterAttribute,它在执行操作并提交或提交之前启动一个 NHibernate 事务之后将其回滚,具体取决于是否抛出异常.ISession 实例是 HttpRequestScoped() 并由 注入Autofac.它看起来像这样并且效果很好:

On ASP.NET MVC 2 I have an ActionFilterAttribute called [Transaction] that starts an NHibernate transaction before executing the action and commits or rolls it back afterward, depending on whether or not an exception was thrown. The ISession instance is HttpRequestScoped() and injected by Autofac. It looks like this and works great:

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public sealed class TransactionAttribute : ActionFilterAttribute
{
    private ITransaction transaction;

    public TransactionAttribute()
    {
        this.Order = 0;
    }

    public ISession Session
    {
        get;
        set;
    }

    public override void OnActionExecuted(
        ActionExecutedContext filterContext)
    {
        if (this.Session != null && this.transaction != null)
        {
            try
            {
                if (this.transaction.IsActive)
                {
                    if (filterContext.Exception == null)
                    {
                        this.transaction.Commit();
                    }
                    else
                    {
                        this.transaction.Rollback();
                    }
                }
            }
            finally
            {
                this.transaction.Dispose();
                this.transaction = null;
            }
        }
    }

    public override void OnActionExecuting(
        ActionExecutingContext filterContext)
    {
        if (this.Session != null)
        {
            this.transaction = this.Session.BeginTransaction();
        }
    }
}

太棒了.似乎是一种常见的模式.

ASP.NET MVC 3 注释中,我在Breaking Changes"(强调我的)下看到了这个小信息:

In the ASP.NET MVC 3 notes, I see this little blurb under "Breaking Changes" (emphasis mine):

在以前版本的 ASP.NET MVC 中,除了少数情况外,每个请求都会创建操作过滤器.这种行为从来都不是有保证的行为,而只是一个实现细节,过滤器的契约是将它们视为无状态的.在 ASP.NET MVC 3 中,过滤器被更积极地缓存.因此,任何不正确存储实例状态的自定义操作过滤器都可能被破坏.

In previous versions of ASP.NET MVC, action filters were created per request except in a few cases. This behavior was never a guaranteed behavior but merely an implementation detail and the contract for filters was to consider them stateless. In ASP.NET MVC 3, filters are cached more aggressively. Therefore, any custom action filters which improperly store instance state might be broken.

糟糕.

  • 这是否意味着如果我升级到 MVC 3,我会很痛苦?
  • 如果不再为每个请求实例化操作过滤器,我们将如何将请求范围的依赖项添加到我们的操作过滤器中?

感谢您的任何见解.

推荐答案

我刚刚在谷歌论坛上问了一个类似的问题.这是链接 https://groups.google.com/forum/#!topic/autofac/a0qqp2b3WA8

I just asked a similar question on google forums. Here is the link https://groups.google.com/forum/#!topic/autofac/a0qqp2b3WA8

我得到了答案:

builder.RegisterType<ExtensibleActionInvoker>().As<IActionInvoker>();


builder.RegisterControllers(Assembly.GetExecutingAssembly()).InjectActionInvoker();

然后你可以在你的属性中使用属性注入.

Then you can use property injection in your attributes.

这篇关于ASP.NET MVC 3、动作过滤器和 Autofac 依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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