MVC 3 中 ActionFilter 依赖注入的自定义 ActionInvoker 与自定义 FilterProvider
任何人都可以阐明使用自定义 ActionInvoker 的优点和缺点 像这样 对自定义 ActionFilter 执行依赖项注入,而不是使用自定义 FilterProvider 如此处所示?
在这两种情况下,您仍然希望避免在 ActionFilters 上进行构造函数注入,在我看来,在这种情况下,所有自定义 FilterProvider 所做的都是增加必须在容器中注册 ActionFilters 和提供程序的额外开销。
Can anyone shed some light on the advantages and disadvantages of using a custom ActionInvoker like so to perform dependency injection on custom ActionFilters as opposed to using a custom FilterProvider as demonstrated here?
In both cases you are going to still want to avoid constructor injection on your ActionFilters, and to me it seems that all the custom FilterProvider does in this case is adds additional overhead of having to register your ActionFilters and the provider in your container.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
过滤器提供程序的一大优点是,如果做得正确,它允许构造函数注入。您链接的示例没有正确演示这一点。要使用构造函数注入,您不能使用过滤器属性。相反,您必须将要应用过滤器的声明(例如使用属性)和过滤器实现分开两件事。
我不确定Unity是否有一个好的实现。请参阅 http:// www.planetgeek.ch/2010/11/13/official-ninject-mvc-extension-gets-support-for-mvc3/ 感觉如何。但这是一个 Ninject 示例。也许您可以在 github 上获取其实现并将其移植到 Unity。
The big advantage of filter providers is that it allows constructor injection if done right. The example you linked does not demonstrate this correctly. To use constructor injection you can not use filter attributes. Instead you have to separate the two things the declaration that you want to apply a filter (e.g. using an attribute) and the filter implementation.
I'm not sure if there is a good implementation for Unity. See on http://www.planetgeek.ch/2010/11/13/official-ninject-mvc-extension-gets-support-for-mvc3/ how it could feel like. But this is a Ninject example. Probably you can take its implementation on github and port it to Unity.