使用Castle Windsor将依赖项注入CustomAttribute
在我的ASP.Net MVC应用程序中,我实现了一个Custom ActionFilter来授权用户。
我使用CastleWindsor为所有控制器提供dependency injection,如下所示:
protected virtual IWindsorContainer InitializeServiceLocator() { IWindsorContainer container = new WindsorContainer(); ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(container)); container.RegisterControllers(typeof(HomeController).Assembly); ComponentRegistrar.AddComponentsTo(container); ServiceLocator.SetLocatorProvider(() => new WindsorServiceLocator(container)); return container; }
在我的CustomAttribute中,我需要一个由所有控制器使用的依赖项,但是我无法在属性中使用基于构造函数的注入。
那么这里最干净的方式是什么? 我该如何提供依赖?
好的 – 这似乎是数据库注入到ASP MVC和Castle Windsor的validation属性的副本,已经得到了回答。
另外,我如何使用Windsor将依赖项注入ActionFilterAttributes 。
阅读完上述内容和参考文章 – 对我来说关键的一点是https://weblogs.asp.net/psteele/archive/2009/11/04/using-windsor-to-inject-dependencies-into-asp -net-mvc-actionfilters.aspx对于任何感兴趣的人。
你不能。 属性是元数据。 将行为置于其中是错误的。 放置依赖性更糟糕。
使用属性作为标记来表示要应用行为的对象,并在其他位置实现该行为。
在其他地方的 MVC中,通常意味着一个自定义动作调用程序,它使用属性中的数据来提供所需的行为。
上述就是C#学习教程:使用Castle Windsor将依赖项注入CustomAttribute分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1040908.html