-
Notifications
You must be signed in to change notification settings - Fork 33
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
Questions about Func<> factories #308
Comments
Hi @jods4, I hope your question is still relevant 😄 Short answerUse this and you will be able to resolve Func<IinjectionScope,String,IExporter> to get an exporter with a given key from the provided scope. Long answer
This is possible because Grace will try to propagate Delegate's parameters to all constructors of your target and related dependencies. And, coincidentally, Scoped<> constructor has a string parameter that allows you to define the scope name. Let me know if I could help you with anything else! And here is the complete example:
|
@Lex45x Thanks for answering. The export factory approach is interesting. Some thoughts:
Given all this, I suppose injecting |
As this is a discussion, not an issue I'm just gonna close it. (I would have opened a discussion if it was enabled for the repo). |
BTW, speaking of keyed injections: @Lex45x @ipjohnson I'm sure you've seen .net 8 introduces built-in support for keyed services? I suppose you'll have to implement new interfaces to integrate Grace with the new built-in API such as |
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). |
@ipjohnson not putting any pressure, just for my information: |
@ipjohnson I've looked at what is required exactly and have partially done it in PR ipjohnson/Grace.DependencyInjection.Extensions#36. It's incomplete because I think at least one new feature requires core Grace support: keyed registration that satisfy any key. |
Hi @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 anIExporter
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 aFunc<>
.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 likefactory(scope)
to get aT
from a localscope
.Thanks!
The text was updated successfully, but these errors were encountered: