MVC Ninject: How to add NinJect bindings from an MVC Area project

送分小仙女□ 提交于 2019-12-24 14:06:19

问题


I've been using this blog example:

http://blog.longle.net/2012/03/29/building-a-composite-mvc3-application-with-pluggable-areas/

I have the concepts working in my solution. However, I'm trying to figure out a good way only add bindings to the kernel if a user has permissions to access a module/area.

I've read up some on the ServiceLocator but I was trying to stay away from it.

One thing I'm trying just to get things to work is user Contructor injection in the default constructor for a module. It's working but is a hack.

The pattern I'm using, each module project you create an class that inherits from AreaRegistion. When each module project builds, relevant files are copied to the Areas folder of the main web project. Then when the main project loads, reflection is used to load all module assemblies. Then when AreaRegistration.RegisterAllAreas() is called, it detects and loads all the modules with a class that inherits AreaRegistration.

I'd like to figure out an good way to access the Ninject kernel and add bindings in the module class that inherits from AreaRegistration. I would imagine initiating code to add bindings from the RegisterArea() override.

I'm looking for any suggestions on how to do this without resorting to the ServiceLocator.

Any ideas would be greatly appreciated.


回答1:


For now I've found a better solution and that's to use NinjectModule. In the Plugin class, I'm going to create a class which inherits from NinjectModule. Then setup the pluging bindings in the Load overload.

Then use Kernel.Load in my main app bootstrapper to initialize the Load overloads in all plugin classes which inherit from NinjectModule.



来源:https://stackoverflow.com/questions/27724901/mvc-ninject-how-to-add-ninject-bindings-from-an-mvc-area-project

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