-
Notifications
You must be signed in to change notification settings - Fork 778
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
Add DI extensions that accept implementations as type parameters. #5947
base: main
Are you sure you want to change the base?
Conversation
@SteveSandersonMS had experimented with adding these previously but ultimately decided not to: |
🎉 Good job! The coverage increased 🎉
Full code coverage report: https://dev.azure.com/dnceng-public/public/_build/results?buildId=957828&view=codecoverage-tab |
Interesting. I followed a different approach in this implementation. Rather than calling |
So aside from If that's the only remaining objection, one option might be to register the Also, given that the instance parameter name is |
Am I right to think this approach relies on Could you give an example of what the code looks like to obtain transient copilot chat clients without this? It would be good to get a sense of how much payoff there is, if there's also some drawback that people will try to use it wrongly (e.g., to register an OpenAI-based chat client) and get confused. |
What's interesting about the
The motivating use case is Copilot extensions: Clients hitting the Github API need to be passed the Github token from the request headers. Not having the generic method necessitates writing this instead: builder.Services.AddChatClient(provider => new GithubChatClient(provider.GetRequiredService<IHttpContextAccessor>()), ServiceLifetime.Scoped).UseFunctionInvocation(); |
In general the expected DI pattern is that any concrete inner client that wants to streamline the DI registration experience should define its own extension methods that do that. This allows them to take in whatever parameters are required, as well as encapsulate acquiring whatever other DI services are needed. For example we already have If we had |
Out of all the IChatClient implementations I've seen, that's the only one that might benefit. But even there, I'd expect the library exposing it to provide an .UseDistributedCache(new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions()))) and the easy way: .AddDistributedMemoryCache() I'm concerned at the idea of adding overloads that'll help almost no one but could easily be a pit of failure for the majority, eg someone getting stuck on: .AddChatClient<OllamaChatClient>() which can't work. |
These are useful in the context of defining transient Copilot chat clients.
Microsoft Reviewers: Open in CodeFlow