You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
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);
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
Activity
PawelGerr commentedon Apr 22, 2018
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
to
ipjohnson commentedon Apr 22, 2018
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
This basically adds an override export that will pull the Context from the injection context or generate it if it doesn't exist.
fixing circukar refernce problem issue #156
ipjohnson commentedon May 19, 2018
I'm going to release 6.3.4 with this fix and close this issue.