I have a class Handler. In the constructor I have a Manager class which depends on different settings.

kernel.Bind<IHandler>().To<Handler>().
                WithConstructorArgument("manager", 
                new Manager(kernel.Get<IDataAccessFactory>().CreateUnitOfWork(), new Custom1Settings()));

How do i create an injection for Custom1Settings in Manager and which in Handler

 class Handler
{
    Handler(IManager manager....)
    {
        ...
    }
}

class Manager
{
    Manager(IUnitOfWork unit, ISettings settings)
    {
        ...
    }
}
有帮助吗?

解决方案 2

kernel.Bind<IManager>().To<Manager>().Named("Registration").WithConstructorArgument("settings", new Custom1Settings());

kernel.Bind<IHandler>().To<Handler>().WithConstructorArgument("manager", ctx => ctx.Kernel.Get<IManager>("Registration"));

其他提示

Ninject automatically figures out dependencies. It's one of it's most useful features.

Define a binding for Custom1Settings and Manager and it will automatically inject it.

So

kernel.Bind<IManager>().To<Manager>();
kernel.Bind<ICustom1Settings>().To<Custom1Settings>();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top