-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[API Proposal]: ServiceCollection support post service registration #110926
Comments
This sounds like an abuse of the service locator? |
Can you update the options by registering a generic service for |
@colejohnson66 Understand. To be honest, this feature is not for most developers. If package author can follow the convention, the issue would go away. For example, |
If my understand is correct, you means Options Pattern? |
@Li7ye, IDbContextOptionsConfiguration<TContext> was added in dotnet/efcore#32518 and shipped in Entity Framework Core 9.0.0. In EntityFrameworkServiceCollectionExtensions, all AddDbContext methods eventually call AddCoreServices, which registers DbContextOptions<TContextImplementation> as a service with a factory delegate that calls CreateDbContextOptions<TContextImplementation>, which gets all IDbContextOptionsConfiguration<TContext> services from the IServiceProvider and calls their Configure methods. Typically, the IDbContextOptionsConfiguration<TContext> service is registered by ConfigureDbContext<TContext>, and this registration applies only to the specific TContext type; but I think you could bypass that and register a service for the IDbContextOptionsConfiguration<> open generic type instead, so that it would apply to every TContext. |
It looks like this API proposal is not complete and one would also need to add at least the following: namespace Microsoft.Extensions.DependencyInjection
{
public partial class ServiceCollectionServiceExtensions
{
public static IServiceCollection AddPostRegistration<T>(
this IServiceCollection services)
where T : IPostServiceRegistration, new();
}
} And perhaps also: namespace Microsoft.Extensions.DependencyInjection
{
public partial class ServiceCollectionServiceExtensions
{
public static IServiceCollection AddPostRegistration(
this IServiceCollection services,
IPostServiceRegistration registration);
}
} And designs for:
|
The new IPostServiceRegistration interface feels a bit unnecessary; Action<IServiceCollection> could be used instead. This change would result in shorter C# code in the AddPostRegistration call. Interfaces make sense in IConfigureOptions<TOptions> etc. because they allow other services to be injected to the constructor of the class that implements the interface; but IPostServiceRegistration would be used before the service provider is built, so the services are not yet available for injection. |
Background and motivation
I'm a framework programmer. Usually, I often add extension method for
IServiceCollection
. But sometimes, my extension method is required to knowIServiceCollection
state.For instance,
services.AddUnitOfWork()
need to know allDbContextOptions
in services and updateDbContextOptions
. ifservices.AddUnitOfWork()
is added after registration of all dbcontexts, it can work properly. Otherwise, can get side effect(unexpected behavior). Some users maybe callservices.AddUnitOfWork
before or between dbcontexts registration by accident, becauseAddUnitOfWork
is built onIServiceCollection
and there is no restriction.API Proposal
I suggest has an interface can be invoked after all services registered but before build to provider.
API Usage
Hence
services.AddUnitOfWork
can use the new feature to register own services or add validation, and can be added out of order by user. User only know I need to call this method without other knowledge.Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: