Questions about Func<> factories
See original GitHub issueHi @ipjohnson !
I have a few question regarding factories if you don’t mind answering them. These questions are about use cases where I inject a scope locator and it works absolutely fine. I was just wondering if there was a way to avoid taking a dependency on the full container. I know Grace has tons of features but they’re sometimes hard to discover! 😉
So I know that you can inject:
Func<T>
for a factory resolvingT
in current scope.Func<Scoped<T>>
for creating a new (disposable) scope and resolvingT
inside of it.Func<string, Scoped<T>>
same as previous one, but you can give the scope a name.
Question 1: Can you get a factory to resolve a keyed T
, the key being dynamically provided when calling the factory? (as opposed to: when importing the factory)
An example use case is having an interface IExporter
and many implementation keyed by format (e.g. “xls”, “xlsx”, “cvs”) and then being to dynamically create from code an IExporter
based on a specific format.
Question 2: Can you do the same as Q1 combined with Scoped
?
I.e. creating a new scope, and resolving a keyed T
from code through a Func<>
.
Question 3: Is it possible to import factories in one scope (e.g. global scope) and then use them to resolve T
in a different scope provided dynamically?
Hypothetically I would import Func<IExportScopeLocator, T>
in a global singleton, then use it like factory(scope)
to get a T
from a local scope
.
Thanks!
Issue Analytics
- State:
- Created 6 months ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
I did see the new APIs, very exciting. My plan is to add the interfaces as we get closer to .net 8 release date (late Nov).
BTW, speaking of keyed injections:
@Lex45x @ipjohnson I’m sure you’ve seen .net 8 introduces built-in support for keyed services? https://weblogs.asp.net/ricardoperes/net-8-dependency-injection-changes-keyed-services
I suppose you’ll have to implement new interfaces to integrate Grace with the new built-in API such as
AddKeyedSingleton()
.