如何将对象注入到Ninject模块中 [英] How to inject an object into a Ninject Module

查看:80
本文介绍了如何将对象注入到Ninject模块中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ninject进行DI.我有Ninject模块,这些模块将某些服务绑定到内核,并在其他模块中将绑定的对象用作服务. 为了清除这种情况,让我们看一些代码行: 这是我的安全模块.它提供了名为PermissionManagerContainer的服务.

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();
    }
}

另一方面,我有一个FormServices模块,该模块应将一个项目添加到注入的PermissionManagerContainer中.我认为代码必须是这样的:

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);
    }
}

因此,再次在名为ManagePermissions.aspx的页面中,注入PermissionManagerContainer并为所有模块的权限管理器创建用户界面.例如,我需要在FormServices模块中保护Forms并为该服务中的每个表单定义权限.

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.

但是我认为在将PermissionManagerContainer注入另一个模块之前并不能保证将其绑定!

But I think there are no guarantee for binding PermissionManagerContainer before injecting it in another module!

实际上,我对这个问题有自己的解决方案.我可以编写一个名为MyModule的抽象类,该抽象类是NinjectModule的子类,并可以编写一个名为InitializeModule的抽象方法.并在该方法中调用RegisterManager.在内核中加载所有模块之后,然后为每个加载的模块调用InitializeModule.

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.

但是我的问题是:

  1. Ninject是否在内部具有此功能?
  2. 很可能Ninject在内部管理这种情况,我可以在load方法中调用RegisterManager.是真的吗?
  1. Does Ninject has this feature internally or not?
  2. It is probable to Ninject manage this case internally and I could call the RegisterManager in the load method. Is it true?

推荐答案

您误解了模块的用途.他们在那里配置Ninject.他们自己根本不应该有任何依赖关系. RegisterManager属于应用程序的服务或启动操作中的某个位置.或者,可能您必须更改权限管理器的工作方式,以便它占用所有已配置管理器的枚举,而不是注册它们.但是,要想从问题中找出最好的是几乎是不可能的-只是您滥用了模块中不想要的功能.

You are misunderstanding the purpose of modules. They are there to configure Ninject. They themselves shouldn't have any dependencies at all. The RegisterManager belongs somewhere in a service or startup action of your application. Or probably you have to change the way the permission manager works so that it takes an enumerable of all configured managers instead of registering them. But it's nearly impossible to tell what is best from the question -- just that you are abusing modules for something they are not intended for.

这篇关于如何将对象注入到Ninject模块中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆