-
Notifications
You must be signed in to change notification settings - Fork 97
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
[Feature Request] Adding the Commands Class to the service Container to enable access to it from other services via DI? #148
Comments
var instance = provider.GetService<MyCommands>() ?? new MyCommands(); /* when T is concrete class, fallback */ If this satisfies your needs, I will consider implementing it. |
Hola @neuecc - been distracted, thanks for coming back on this! So the assumption will be that both the concrete class as well as its interfaces (per my example above) that are registered in the DI container. This could work, but does have a slight smell in that other services "could" grab (DI) the entire command. If this is what you can do that that would be great! Another approach dependent on the desired degree of coupling between the library and the DI container, could be: That there be a number of The Services Container would be accessible as an additional property, similar to the I then guess the As above - I'd be happy to work with the more basic approach. At some point there might be a request to support different DI containers, sure sure how that would then play in either way. |
Hmm, by the way, are you actually struggling with this in a real-world case? |
yes, I have a specific case for this. It's kinda of proprietary so it difficult to share the specifics, but do have a case when the Command launches other services that are DI'd, and there is status monitoring between then and the "state" of completeness of the cli command. I'd suggest this is not an uncommon pattern? I've a workaround that solves this for now by using an adapter service also registered in the DI container that implements
Per you reply above what are you consideration timelines, and other change consideration per other issues - as would tie to a release? I'd also suggest this issue is also similarly valuable to the current use case - #142 |
To be honest, I don't understand more than half of what's being said. |
Is is correct that the following construct:
and specifically
app.Add<MyCommands>()
is just a code signature the code generator runs on as the implementation of the resultantConsoleApp.Add<T>()
is empty:In the case that one is using DI, is it not unrealistic that a registered service might want to interact with the command class via an interface that could be injected into it?
Today is seem the code just
new
's up the Commands class (getting services from the DI container) and calls the requisite public function.Could and alternative approach be to register the Command Class as say a Singleton (say), and any interfaces on it that one wants to leverage from other services.
And then I the generated use the
ConsoleApp.ServiceProvider
to instantiate the the Command Class (potentially updating these lines in theEmitter.cs
- assuming the parser has the requisite information):The DI container will the inject the required services as needed based the constructor, and invoke the Command Function as before.
Also a potential advantage on registering and getting the Command Class back from the DI Container, is that is will (from Net8.0) handle Async/Disposal in the container - not that the library has not already solved for that otherwise - in a non DI way.
The text was updated successfully, but these errors were encountered: