Provide CommandContext in DI #480
-
This is what I do: var containerBulder = new ServiceCollection();
// inject stuff via containerBuilder
// ....
var container = containerBulder.BuildServiceProvider(
new ServiceProviderOptions()
{
ValidateOnBuild = true,
});
app.UseMicrosoftDependencyInjection(
container,
argumentModelResolveStrategy: ResolveStrategy.ResolveOrThrow,
commandClassResolveStrategy: ResolveStrategy.ResolveOrThrow); During the injection process, it'd be much helpful to be able to access the I'm currently using ParameterResolver to work around this, which has access to context but it's weird. Is there any recommended way to do this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
It's not something the framework explicitly supports at the moment. There is a |
Beta Was this translation helpful? Give feedback.
-
What information would you be looking for in the CommandContext? As you can see from order the middleware is run below, BeginScope is the 4th step and is run in the PreTokenize stage. That is when runInScope will be executed if specified. If we created a ContextLocator class and it was registered as scoped with the container, we could set the instance on the locator in that step. This is something you could do now by adding your own middleware step to run after the BeginScope step. Create the ContextLocator and register it with the container and then resolve and set it in the middleware step. You could then resolve that locator in your registration method and change what is returned accordingly. If it works for you, I'd accept a PR to enhance this framework with it.
|
Beta Was this translation helpful? Give feedback.
It's not something the framework explicitly supports at the moment. There is a
runInScope
delegate you could use to create the scope that gives access to the CommandContext. I would need to do a little research to see how to make it available in the registration methods. If you know how to do it or have examples, I'd be interested in adding it or accepting a PR for it.