简体   繁体   中英

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

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

I am attempting to use ninject to search the directoru and setup the bindings.

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.

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

Does anyone know the correct binging to setup using the ninject conventions? 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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