Skip to content

Injecting values at runtime #156

Description

@davidkeaveny

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

Activity

  1. PawelGerr commented on Apr 22, 2018

    @PawelGerr

    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);
    
  2. ipjohnson commented on Apr 22, 2018

    @ipjohnson
    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.

  3. ipjohnson commented on May 19, 2018

    @ipjohnson
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions