Skip to content
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

Support of ASP.NET Core dependency injection out of the box #71

Open
robin-83 opened this issue May 9, 2023 · 0 comments
Open

Support of ASP.NET Core dependency injection out of the box #71

robin-83 opened this issue May 9, 2023 · 0 comments

Comments

@robin-83
Copy link

robin-83 commented May 9, 2023

If you have support for EF Core (#61) ready, it would be a great addition to support the built in dependency injection framework in ASP.NET: Currently if you only register the implementation like this:
builder.Services.AddScoped<IDbContextScopeFactory, DbContextScopeFactory>(); builder.Services.AddScoped<IAmbientDbContextLocator, AmbientDbContextLocator>();
the context must have a parameterless constructor. As DBContextCollection includes the following line:
Activator.CreateInstance<TDbContext>().

To support a context with parameters provided by DI (like auto generated from scaffolding), the library could have a default factory like this:

`internal class DBContextLocator : IDbContextFactory
{
private readonly IServiceProvider _serviceProvider;

    public DBContextLocator(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
    }

    public TDbContext CreateDbContext<TDbContext>() where TDbContext : class, IDbContext
    {
        return ActivatorUtilities.CreateInstance(_serviceProvider, typeof(TDbContext)) as TDbContext ??
            throw new NotImplementedException(typeof(TDbContext).Name);
    }
}`

Perhabs there are better ways to create the context, but I think you get the idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant