'Configure 'sub-dependencies' with Microsoft .NET Core DI container
I am working on a solution which provides easy access to mail templating functionality for a set of e-mails. Now I want to add a 'cqrs plugin' for this library I am creating:
services.AddEmail(setup => setup.AddSubconfiguration(sub => sub. [...]);
I want to provide the subconfiguration in a separate nuget package. How to extend the dependency injection extensions to allow to use 'sub dependencies'
public static class DependencyInjectionExtensions
{
public static IServiceCollection AddEMail(Action<EmailOptionsBuilder> emailOptionsBuilderAction)
{
var builder = new EmailOptionsBuilder();
action.Invoke(builder);
builder.Build();
}
}
So how to take into account the submodule in the builder.Build method without tightly coupling the submodule?
I can write an extension method for the EmailOptionsBuilder but how to let the build method know it has to do something extra? And also how to allow the ServiceCollection to be populated with the right dependencies?
Edit:
After looking into how things are being done in the .NET core libraries, I found this:
It does not appear to be a 'real builder' as described on https://en.wikipedia.org/wiki/Builder_pattern#:~:text=The%20builder%20pattern%20is%20a,Gang%20of%20Four%20design%20patterns.
Because the AuthenticationBuilder class just adds services to the collection and uses the IServiceCollection and does not have a Build() method or something which builds something, the only thing what is happening is populating the service collection with new services.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
