C#C
C#2y ago
Yawnder

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:
services.AddAuthorization(c =>
{
    var tenantAuthRequirement = new TenantAuthorizationRequirement();
    c.AddPolicy(Policies.TenantAuthenticatedUser, p =>
    {
        p.AddAuthenticationSchemes(PreSharedKeyAuthenticator.Scheme, PartitionConstants.AuthenticationSchemaBearer)
          .RequireAuthenticatedUser()
          .AddRequirements(tenantAuthRequirement);
    });
}


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?
Was this page helpful?