Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Injecting values at runtime #156

Closed
davidkeaveny opened this issue Apr 19, 2018 · 3 comments
Closed

Injecting values at runtime #156

davidkeaveny opened this issue Apr 19, 2018 · 3 comments

Comments

@davidkeaveny
Copy link

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
Copy link

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
Copy link
Owner

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants