I dont' understand have functions with arguments works #57
-
I don't understand how func with arguments work This example .To<Func<int, IDependency>>(ctx =>
dependencyId =>
{
// Builds up an instance of type Dependency
// referring the source code statement "dependencyId"
ctx.Inject<Dependency>(out var dependency);
return dependency;
}) I guess dependencyId is somehow wired to the int id argument to the constructor of Dependency class. But I want to understand how you'll achieve that if you have two int arguments for example. My guess is that it should emit compile time error. In this case what are my options? To create the instance with new operator? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
DI.Setup(nameof(Composition))
.Bind<IClock>().As(Lifetime.Singleton).To<Clock>()
// Binds a dependency of type int
// to the source code statement "dependencyId"
.Bind<int>().To<int>("dependencyId")
.Bind<Func<int, IDependency>>()
.To<Func<int, IDependency>>(ctx =>
dependencyId =>
{
// Builds up an instance of type Dependency
// referring the source code statement "dependencyId"
ctx.Inject<Dependency>(out var dependency);
return dependency;
})
.Bind<IService>().To<Service>()
// Composition root
.Root<IService>("Root"); The key here is Note Please see the generated code to understand the idea, it's pretty simple. But Using a binding of the form |
Beta Was this translation helpful? Give feedback.
The key here is
.Bind<int>().To…