Ninject, passing constructor argument to the kernel

让人想犯罪 __ 提交于 2019-12-05 04:01:33

If you always want to specify the Connection when resolving a IDownloader then I think the ConstructorArgument (which is a IParameter) is what you are looking for:

[Test]
public void Run_Forrest_Run()
{
    using (var kernel = new StandardKernel(new Module()))
    {
        var connection = new Connection(Guid.NewGuid().ToString());
        var downloader = kernel.Get<IDownloader>(new [] { 
            new ConstructorArgument("connection", connection) });

        Assert.That(downloader.Connection.Info, Is.EqualTo(connection.Info));
    }
}

public class Module : NinjectModule
{
    public override void Load()
    {
        Bind<ILogger>().To<ConsoleLogger>();
        Bind<IDownloader>().To<Downloader>();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!