DI: Registration of services through a builder.
I'm making a library to be "registered" like
services.AddMyFeature(builder => ...);
The builder
can be used a bit like ASP's registration with the AddAuthorization
, AddPolicy
, etc.
For example:
I want one of these builder.AddSubFeature()
(the equivalent of the c.AddPolicy(...)
above) to add some more registrations in services
.
The way I was thinking about it was to have an internal List<Action<IServiceCollection>>
on the builder
in which builder.AddSubFeature()
would add custom registrations.
Any better suggestion?7 Replies
But that has absolutely nothing to do with my question.
My bad, perhaps I misunderstood.
It means my question wasn't clear then!
Based on this
builder.AddSubFeature()
maybe an extension would help.As of now, it is an extension method that adds the registration in
builder.CustomRegistrations
, which is internal. I'm wondering if there is a better known pattern to do that.Are you looking for a way to register you services separately?
A way for that extension method to register services, while this extension methods only has access to a builder, not the
IServiceCollection
. I'm not looking for a method that just works; mine does. I'm looking for a known pattern that I should follow rather than my makeshift method.