ipjohnson / Grace

Grace is a feature rich dependency injection container library
MIT License
336 stars 33 forks source link

Injecting values at runtime #156

Closed davidkeaveny closed 6 years ago

davidkeaveny commented 6 years ago

Is it possible to resolve a type from the container, change some properties on it, then use that instance when resolving other types that take the first type as a dependency, rather than returning a new instance?

From what Intellisense suggests, the extraData parameter on Locate() should be my friend, so here's the basics of a test that demonstrates what I want to achieve (I've attached a proof-of-concept):

var context = container.Locate<Context>();
context.Id = Guid.NewGuid();

var command = new TestCommand();
var handler = container.Locate<ICommandHandler<TestCommand>>(extraData: new { context });
handler.Handle(command);

Assert.AreEqual(context.Id, command.Result);

However, the test fails as the context I sent to Grace is not the same context that is injected into my handler (I've tried modifying the test to use Assert.Same on the two contexts, but it fails also).

So, is what I am trying to do possible, or am I just doing it wrong?

grace-injectable-values.zip

PawelGerr commented 6 years ago

Hi, I've discovered this DI framework 5 min. ago, so I'm not an expert :) But the feature set and syntax is so similar to autofac, so I downloaded your sample and tried something I would do in autofac and it worked. Your unit test pass.

I've changed the line from

var handler = container.Resolve<ICommandHandler<TestCommand>>(new { context });

to

var handlerFactory = container.Resolve<Func<Context, ICommandHandler<TestCommand>>>();
var handler = handlerFactory(context);
ipjohnson commented 6 years ago

Hi Guys,

I meant to comment on this earlier but life has a funny way of going sideways. The reason david specific syntax doesn't work is because grace only looks in the extra data context as a last resort.

Paul's syntax is correct because you're telling Grace hey I'm commiting to passing in a Context so Grace knows to use the instance that is passed.

To go along with Paul's suggestion I'm also going to add a little code to make this syntax possible as well

c.ExportFactory<IInjectionContext,Func<Context>,Context>((injectionContext, instanceFactory) => 
    (Context)injectionContext.Values.FirstOrDefault(v => v is Context) ?? instanceFactory());

This basically adds an override export that will pull the Context from the injection context or generate it if it doesn't exist.

ipjohnson commented 6 years ago

I'm going to release 6.3.4 with this fix and close this issue.