kernel.Bind<IManager>().To<Manager>().Named("Registration").WithConstructorArgument("settings", new Custom1Settings());
kernel.Bind<IHandler>().To<Handler>().WithConstructorArgument("manager", ctx => ctx.Kernel.Get<IManager>("Registration"));
How to depend by parent class
-
04-06-2022 - |
Question
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)
{
...
}
}
Solution 2
OTHER TIPS
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>();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow