Ninject, passing constructor argument to the kernel

自作多情 提交于 2020-01-02 02:31:32

问题


Here is my problem: I want to pass in one of the values to the constructor every time I request an instance form the kernel. I written some code below to illustrate the problem. The test is not failing so I guess that this works, but it does look pretty ugly. Is there a better, cleaner way to accomplish this with Ninject? Or should I rethink my design? All suggestions are appreciated.

[TestFixture]
public class Sandbox
{
    [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 IParameter[] { new Parameter("connection", connection, false) });

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

    public class Downloader : IDownloader
    {
        public Downloader(Connection connection, ILogger logger)
        {
            Connection = connection;
            Logger = logger;
        }

        public Connection Connection { get; private set; }

        public void Download()
        {
            Logger.Log("Downloading...");
        }

        public ILogger Logger { get; private set; }
    }

    public interface IDownloader
    {
        Connection Connection { get; }
        void Download();
    }

    public class ConsoleLogger : ILogger
    {
        public void Log(string message)
        {
            Console.Out.WriteLine(message);
        }
    }

    public interface ILogger
    {
        void Log(string message);
    }

    public class Connection
    {
        public Connection(string info)
        {
            Info = info;
        }

        public string Info { get; private set; }
    }

    public class Module : NinjectModule
    {
        public override void Load()
        {
            Bind<ILogger>().To<ConsoleLogger>();

            Bind<IDownloader>().To<Downloader>()
                .WithConstructorArgument("connection", context =>
                {
                    var p = context.Parameters.First(x => x.Name == "connection");
                    return p.GetValue(context, null);
                });
            }
    }
}

回答1:


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


来源:https://stackoverflow.com/questions/9310847/ninject-passing-constructor-argument-to-the-kernel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!