MVC Ninject: How to add NinJect bindings from an M

2019-08-10 21:47发布

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条回答
唯我独甜
2楼-- · 2019-08-10 22:19

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.

查看更多
登录 后发表回答