I am using Ninject for DI. I have Ninject Modules that bind some services to the Kernel and use binded object in other modules as a service.
To clear the situation let's see some lines of code:
This is my security module. It provides a service named PermissionManagerContainer.
public class SecurityModule : NinjectModule
{
public override void Load()
{
Bind<IPermissionManagerContainer>().To<PermissionManagerContainer>().InSingletonScope();
}
}
At the other hand I have a FormServices module that should add an item to the injected PermissionManagerContainer. I think the code must be something like this:
public class FormServicesModule : NinjectModule
{
[Ninject.Inject]
private IPermissionManagerContainer permissionManagerContainer { get; set; }
public override void Load()
{
permissionManagerContainer.RegisterManager(formServicesPermissionManager);
}
}
So in a page named ManagePermissions.aspx again I inject the PermissionManagerContainer and create user interface for Permission Managers of all modules. For example, I need to secure Forms in my FormServices module and define permissions for every form in that service.
But I think there are no guarantee for binding PermissionManagerContainer before injecting it in another module!
Actually, I have my own solution for this problem. I can write an abstract class named MyModule which is sub classed from NinjectModule and write an abstract method called InitializeModule. and call RegisterManager in that method. Then call the InitializeModule for every module loaded, after loading all modules in kernel.
But my questions are:
- Does Ninject has this feature internally or not?
- It is probable to Ninject manage this case internally and I could call the
RegisterManagerin the load method. Is it true?