5

I'm trying to inject TenantProvider into DbContext

public class AppDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, long>
{
    public int? _tenantId;

    public ITenantProvider _tenantProvider;

    public AppDbContext(
        DbContextOptions<AppDbContext> options,
        ITenantProvider tenantProvider
        )
    : base(options)
    {
        _tenantProvider = tenantProvider;
    }

but I don't understand how to register it correctly - if I put the breakpoint in the constructor - tenantProvider is null.

The bit from Startup.cs

services.AddDbContext<AppDbContext>(options => AppDbContextOptionsBuilder.Get());

the next line is required to inject the DbContext into a controller or a service (if I add ServiceLifetime.Scoped as a second parameter to the method above - AddDbContext - the feature doesn't work):

services.AddScoped(p => new AppDbContext(AppDbContextOptionsBuilder.Get(), p.GetService<ITenantProvider>()));

(Entity Framework is a separate project in my solution)


When using .AddScoped method - we can pass TenantProvider into constructor by resolving it using .GetService method.

Does anyone have an idea of how to resolve TenantProvider in .AddDbContext method?


Additional info:

I was trying to replace ITenantProvider in the constructor of DbContext with IHttpContextAccessor - the latter is registered as singleton. But the acessor parameter is still null.

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

1 Answer 1

4

I don’t really understand what your AddScoped call is supposed to do. AddDbContext will already register the database context properly with the service collection. So when you resolve the context through dependency injection, additional dependencies will be automatically resolved.

So it should be enough to do this:

services.AddDbContext<AppDbContext>(options => …);

services.AddSingleton<ITenantProvider, TenantProvider>();

And then, you can depend on your AppDbContext using constructor injection, e.g. in your controllers.


Two notes though:

  1. When configuring options, you should modify the passed options object. So you should not just return AppDbContextOptionsBuilder.Get() but instead use the passed options object and edit that.

  2. You should really think about whether your database context having a dependency on your tenant provider is the right thing to do. As per SRP, your database should only do a single thing and that is provide database access.

    Depending on how your tenant provider affects your database access, it might make more sense to move this dependency up one level into some service that uses both the database context and the tenant provider to query data in the right way.

5
  • Hi, and thanks for your answer! A couple of things - .AddDbContext method accepts action, so I can't pass options object directly to it. Another thing is .AddSingleton - provider is still null for some reason. Thanks for mentioning SRP. I wonder if I should use Repository pattern here (something like generic repository)? I'm supposed to filter data by TenantID and IsDeleted fields for now - I wonder what's the best way to go about it. Commented Jul 1, 2018 at 20:05
  • stackoverflow.com/a/50877329/5374333 the guys recommend to stay away from repositories when dealing with DbContext Commented Jul 1, 2018 at 20:13
  • regarding services.AddScoped(p => new AppDbContext(AppDbContextOptionsBuilder.Get())); - I've amended my question - made a mistake, sorry - in fact this is required to get DbContext working in a controller or a service by injecting it. Commented Jul 1, 2018 at 20:21
  • As I said, you should not pass an options object, but instead edit the object you get in the action. You are currently replacing the options object with your own object that you get from AppDbContextOptionsBuilder.Get(). That’s what you should not do. – And no, having a separate AddScoped call that registers the database context is not required and will most likely cause you problems. And no, don’t use a repository here but some higher-level service instead.
    – poke
    Commented Jul 1, 2018 at 20:24
  • I tend to agree the approach you've suggested is a lot better. But if I follow it - I'll end up having a filter expression in almost every service method. So, filtering data manually seems to be a laborious task, unless I missed something. Commented Jul 1, 2018 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.