ASP.NET MVC 3依赖注入 - 控制器,视图和操作过滤器

nfp*_*lee 6 dependency-injection unity-container asp.net-mvc-3

我正在尝试使用Microsoft Unity在ASP.NET MVC 3应用程序中使用依赖注入工作.首先,我实现了自己的IDependencyResolver并在我的Global.asax文件中激活它,如下所示:

DependencyResolver.SetResolver(new UnityDependencyResolver(container));
Run Code Online (Sandbox Code Playgroud)

我发现我不需要做任何其他事情来控制器注入(通过构造函数和[Dependency]属性)来工作.使用默认视图引擎,我还发现我可以使[Dependency]属性在标准视图中工作,但不能在布局视图中工作.是否有可能使布局视图也适用于此?

但是我实现了自己的视图引擎,该引擎继承自VirtualPathProviderViewEngine,它覆盖CreateView/CreatePartialView方法并返回我自己的自定义视图(实现IView).请参阅下面的自定义视图的Render方法:

public void Render(ViewContext viewContext, TextWriter writer) {
    var webViewPage = DependencyResolver.Current.GetService(_type) as WebViewPage;
    //var webViewPage = Activator.CreateInstance(_type) as WebViewPage;

    if (webViewPage == null)
        throw new InvalidOperationException("Invalid view type");

    webViewPage.VirtualPath = _virtualPath;
    webViewPage.ViewContext = viewContext;
    webViewPage.ViewData = viewContext.ViewData;
    webViewPage.InitHelpers();

    WebPageRenderingBase startPage = null;

    if (_runViewStartPages)
        startPage = StartPage.GetStartPage(webViewPage, "_ViewStart", _viewStartFileExtensions);

    var pageContext = new WebPageContext(viewContext.HttpContext, webViewPage, null);
    webViewPage.ExecutePageHierarchy(pageContext, writer, startPage);
}
Run Code Online (Sandbox Code Playgroud)

使用注释掉的行我在视图中完全丢失了依赖注入,所以我将其更改为上面的行,这对于标准视图再次正常工作,但不适用于布局视图.如果您能告诉我如何修改上述内容以适用于布局视图,我会很感激吗?

最后我还试图让动作过滤器注入工作.我发现了两种不同的情况:

  1. 通过属性将过滤器应用于操作.

  2. 将其定义为全局过滤器,例如:

    GlobalFilters.Filters.Add(new TestAttribute());

似乎都没有使用依赖性解析器.因此我需要做一些额外的工作.如果有更好的方法,请纠正我.为了启用第一个场景,我执行了以下操作:

public class UnityFilterAttributeFilterProvider : FilterAttributeFilterProvider {
    private IUnityContainer _container;

    protected override IEnumerable<FilterAttribute> GetControllerAttributes(ControllerContext controllerContext, ActionDescriptor actionDescriptor) {
        var attributes = base.GetControllerAttributes(controllerContext, actionDescriptor);

        foreach (var attribute in attributes) {
            _container.BuildUp(attribute.GetType(), attribute);
        }

        return attributes;
    }

    protected override IEnumerable<FilterAttribute> GetActionAttributes(ControllerContext controllerContext, ActionDescriptor actionDescriptor) {
        var attributes = base.GetActionAttributes(controllerContext, actionDescriptor);

        foreach (var attribute in attributes) {
            _container.BuildUp(attribute.GetType(), attribute);
        }

        return attributes;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在我的Global.asax文件中定义它,如下所示:

FilterProviders.Providers.Remove(FilterProviders.Providers.Single(f => f is FilterAttributeFilterProvider));
FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(container));
Run Code Online (Sandbox Code Playgroud)

这工作正常,但我想知道这是否是正确的方法吗?为了解决第二种情况,我只是将我定义全局过滤器的位置更改为以下内容:

GlobalFilters.Filters.Add(DependencyResolver.Current.GetService<TestAttribute>());
Run Code Online (Sandbox Code Playgroud)

这现在再次起作用,但这是正确的方法吗?

我很感激你的帮助.谢谢

nfp*_*lee 0

自从我最初问这个问题以来已经有一段时间了,但我想我会分享我最终做了什么。

在无法使用构造函数或属性注入的情况下,我通过使用DependencyResolver(服务定位器模式)来解决它。例如,如果我需要该服务,IService我会简单地注入它,如下所示:

public IService Service => DependencyResolver.Current.GetService<IService>();
Run Code Online (Sandbox Code Playgroud)

虽然有些人可能认为这是一种反模式,但我发现它表现良好,问题较少,而且随着 C# 的新进展,我认为它看起来还不错。

但是,如果您使用 ASP.NET Core,则永远不必使用服务定位器模式,因为它已通过依赖注入作为其核心进行了重建。