I think what I'm looking for is something very simple, yet I am unable to find any examples. I'd like to use Ninject to create an object by having Ninject call a factory method with a parameter specified and not injected during the actual request to instantiate the object:
Request for an object here:
StandardKernel.Get<ISomeInteface>(new Ninject.Parameters.Parameter("dataContext", dataContext, true));
And I'd like to map the ISomeInterface to a method that is expecting a value to be passed to it at runtime.
Mapping an interface here:
Kernel.Bind<ISomeInterface>().ToMethod(SomeObject.Create(--> `what do I put here?`));
Is this possible? If so, how do I properly map my interface? Thanks!
ToMethod(ctx =>
SomeObject.Create(
(IDataContext)ctx.Parameters.Single(p =>p.Name == "dataContext")
.GetValue(ctx, null))
But you should rethink your design to avoid calling Get
from anywhere else than your composite root.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With