Ninject, passing constructor argument to the kernel

Pelle picture Pelle · Feb 16, 2012 · Viewed 8.6k times · Source

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

Answer

nemesv picture nemesv · Feb 16, 2012

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