Custom ActionInvoker vs custom FilterProvider for ActionFilter dependency injection in MVC 3

时光总嘲笑我的痴心妄想 提交于 2019-12-12 08:39:40

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/8199691/custom-actioninvoker-vs-custom-filterprovider-for-actionfilter-dependency-inject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!