Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding interface via ToMethod to a method with a parameter

Tags:

ninject

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!

like image 479
Igorek Avatar asked Sep 29 '11 08:09

Igorek


1 Answers

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.

like image 137
Remo Gloor Avatar answered Nov 02 '22 07:11

Remo Gloor