在Ninject的所有程序集加载模块 [英] Loading modules in all assemblies in Ninject

查看:251
本文介绍了在Ninject的所有程序集加载模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个类库在我的项目和所有正在使用 Ninject IoC容器。我想无论是 INinjectModule 发现一气呵成加载在 StandardKernel 所有模块。所以我用:

I have couple of class libraries in my project and all are using Ninject IoC container. I wanted to load all the modules in a StandardKernel at one go wherever an INinjectModule is found. So I used:

var kernel = new StandardKernel();
kernel.Load(AppDomain.CurrentDomain.GetAssemblies())

但是,这并不出于某种原因。谁能帮助?

But this doesn't work for some reason. Can anyone help?

推荐答案

好吧,绑定声明,但其他的模块被加载,其中该模块尝试解析绑定,尚未加载时这种情况经常发生。这是因为列表< INinjectModule方式> 不得以正确的顺序

Well, this often happens when bindings are declared but other modules are loaded where that module tries to resolve a binding which has not loaded yet. This happens because List<INinjectModule> may not in the right order.

如果您认为这种情况。按照这项决议。

If you think this is the case. Follow this resolution.

我们的想法是,我们将有一个 bootstapper 每个组件,其中引导程序将负责加载在其逻辑顺序的模块。

The idea is we will have a bootstapper for each assembly, where the bootstrapper will be responsible to load the modules in its logical order.

让我们考虑引导程序接口(此我们将使用查找组装引导程序)

Let us consider an interface for bootstrapper (this we will use to find the bootstrapper in an assembly)

public interface INinjectModuleBootstrapper
{
    IList<INinjectModule> GetModules();
}

现在考虑为您的数据访问组件,落实 INinjectModuleBootstrapper

Now consider for your DataAccess assembly, implement the INinjectModuleBootstrapper:

public class DataAccessBootstrapper : INinjectModuleBootstrapper
{
    public IList<INinjectModule> GetModules()
    {
        //this is where you will be considering priority of your modules.
        return new List<INinjectModule>()
                   {
                       new DataObjectModule(),
                       new RepositoryModule(),
                       new DbConnectionModule()
                   };
        //RepositoryModule cannot be loaded until DataObjectModule is loaded
        //as it is depended on DataObjectModule and DbConnectionModule has
        //dependency on RepositoryModule
    }
}

这是你如何defne的引导程序您所有的组装。现在,从程序的启动,我们需要在 StandardKernel ,所有的模块被加载。我们将这样写:

This is how you defne the Bootstrapper for all your assembly. Now, from your program startup, we need the StandardKernel where all the modules are loaded. We will write something like this:

var assemblies = AppDomain.CurrentDomain.GetAssemblies();
return BootstrapHelper.LoadNinjectKernel(assemblies);

和我们的 BootstrapperHelper 类是:

public static class BootstrapHelper
{
    public static StandardKernel LoadNinjectKernel(IEnumerable<Assembly> assemblies)
    {
        var standardKernel = new StandardKernel();
        foreach (var assembly in assemblies)
        {
            assembly
                .GetTypes()
                .Where(t =>
                       t.GetInterfaces()
                           .Any(i =>
                                i.Name == typeof(INinjectModuleBootstrapper).Name))
                .ToList()
                .ForEach(t =>
                             {
                                 var ninjectModuleBootstrapper =
                                     (INinjectModuleBootstrapper)Activator.CreateInstance(t);

                                 standardKernel.Load(ninjectModuleBootstrapper.GetModules());
                             });
        }
        return standardKernel;
    }
}

这篇关于在Ninject的所有程序集加载模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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