I want to register multiple DbContext
s in my application to try to make it more modular. However, many of the necessary calls are now duplicated, e.g.
builder.Services.AddDbContext<DbContext1>((_, options) =>
{
// some configuration
}, ServiceLifetime.Transient);
builder.Services.AddDbContext<DbContext2>((_, options) =>
{
// the exact same configuration
}, ServiceLifetime.Transient);
// and also
using var scope = app.Services.CreateScope();
using var context1 = scope.ServiceProvider.GetRequiredService<DbContext1>();
context1.Database.Migrate();
using var context2 = scope.ServiceProvider.GetRequiredService<DbContext2>();
context2.Database.Migrate();
// etc.
The only thing different is the DbContext class, and this is true for the test setup, the migration, etc.
I’d love to have the option to be able to iterate over all the DbContexts each time, so it’s less likely I’ll forget to register one. Something like this:
public static void DoWithEachRegisteredDbContext(DoWithDbContext<> magic)
{
magic<DbContext1>();
magic<DbContext2>();
}
public delegate Type[] DoWithDbContext<TContext>();
// and this method is called like this for the above example
DoWithEachRegisteredDbContext(AddDbContext);
public Type[] AddDbContext<TContext>() {
builder.Services.AddDbContext<TContext>((_, options) =>
{
// some configuration
}, ServiceLifetime.Transient);
DoWithEachRegisteredDbContext(Migrate);
public Type[] Migrate<TContext>() {
using var scope = app.Services.CreateScope();
using var context = scope.ServiceProvider.GetRequiredService<TContext>();
context.Database.Migrate();
}
I would like to use a design pattern or language feature to help me solve this conundrum, because reflection isn’t worth it due to its maintenance costs.
Is something like this possible? How?
9
If I understood the question correctly, you are looking for, is to reduce redundant code, and Keep a method close to changes, yet open to extensions
There is a SOLID principle that is focused just around that, its called The Open Closed Principle (The “O” in SOLID) and it is a principle that helps us solving exactly these use-cases.
One way to solve it would be to use something like a dictionary, iterate through every DbContext in your instance and only add new contexts to that db,
However it doesn’t sound like the most elegant solution.
I’d recommend diving into OCP and checking out what solutions it offers, as this is more of a problematic design choice, which can be solved really, in many different ways.
I would highly recommend learning SOLID better if you are not familiar enough with it.
Good luck! 🙂
2