简体   繁体   English

Ninject约定绑定到泛型类?

[英]Ninject conventions binding to generic class?

I have the following class, which is compiled into a DLL called ExternalTask.dll and is housed externally in my filing system 我有以下类,该类被编译成一个名为ExternalTask​​.dll的DLL,并从外部保存在我的文件系统中

public class ExampleJob : IJob 
{

    public void Execute(IJobExecutionContext context)
    {
        Console.WriteLine("ExampleJob is executing. {0}", System.DateTime.Now.ToUniversalTime());
    }
}

I have placed the dll in the following directory : C:\\External 我已将dll放在以下目录中:C:\\ External

I am attempting to use ninject to search the directoru and setup the bindings. 我正在尝试使用ninject搜索目录并设置绑定。

The following binding works if the class work in the same assembly as my main coded, but I want the dll to be separated off. 如果该类在与我的主编码相同的程序集中工作,则以下绑定有效,但我希望将dll分开。

kernel.Bind<IJobSetup>().To<JobSetup<ExampleJob>>();

Does anyone know the correct binging to setup using the ninject conventions? 有谁知道使用ninject约定进行设置的正确方法? I am currently trying this, but know I need more - I just cannot find much decent documentation to help. 我目前正在尝试此操作,但知道我需要更多-我只是找不到很多不错的文档来提供帮助。

kernel.Bind(scanner => scanner
                .FromAssembliesInPath(
                    @"C:\External\")
                .SelectAllClasses()
                .BindAllInterfaces());

If your project is going to get bigger use Ninject Conventions: 如果您的项目将变得更大,请使用Ninject Conventions:

public class ProjectRepository : IProjectRepository
{
....
}

and set binding as a module: 并将绑定设置为模块:

using Ninject.Extensions.Conventions;

    public class RepositoryModule : NinjectModule
    {
        public override void Load()
        {
            IKernel ninjectKernel = this.Kernel;

            ninjectKernel.Scan(kernel =>
            {
                kernel.FromAssemblyContaining<ProjectRepository>();                
                kernel.BindWithDefaultConventions();
                kernel.AutoLoadModules();
                kernel.InRequestScope();                

            });

       }
    }

then load it: 然后加载它:

IKernel kernel = new StandardKernel(new RepositoryModule());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM