IDesign AddDbContext with an interface?

asp.net-core c# entity-framework-core

Question

I am trying to create a new asp.net core web application using Razor pages. I want to add my db context in the startup.cs file, but I am using something called IDesign.

My DbContext entity is in a project that I am not allowed to reference. I need to somehow add my context either at the accessor layer or something else. I'm not super familiar with how services work for .net core.

I have a solution set up like this:

  1. Clients Project
    1. The project with the cshtml files, startupcs and all of that is here
    2. Can only reference managers and utilities projects
  2. Managers Project
    1. Used for chaining calls to engines/accessors to accomplish tasks at a high level
    2. Used to make calls to accessors
    3. Can only reference accessors, engines, and utilities project
  3. Engines Project
    1. Used for business logic
    2. Can only reference the accessors and utilities projects
  4. Accessors Project
    1. resource accessors. Hits dbs/services.
    2. This is where my dbcontext class lives
    3. can't reference anything except for utilities
  5. Utilities Project
    1. Used to store global classes and utility functions that apply to all projects
    2. Can't reference anything

My problem is that if my DbContext lives in the accessors project, how do I pass that up from the managers so that I can use it in the clients? Has anyone had experience with this before?

Just to reiterate, I know that I could easily reference accessors project in the clients project and use the dbcontext from there. My problem is that I want to avoid being able to reference accessors so that other people who are working with this code aren't able to see any accessors classes.

1
0
9/29/2018 5:19:55 AM

Popular Answer

So I was able to accomplish this by following this guide: https://asp.net-hacker.rocks/2017/03/06/using-dependency-injection-in-multiple-projects.html

Instead of adding all of my services from the client, I was able to add them from the manager using an extension class and calling that extension method from the client.

Client Code:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddManagerDependencyInjection();
        services.AddGroupManagementDbContext();
        // etc...
    }

Manager Code:

public static class IServiceCollectionExtension
{
    public static IServiceCollection AddManagerDependencyInjection(this IServiceCollection services)
    {
        services.AddTransient<IAccessor, Accessor>();
        return services;
    }

    public static IServiceCollection AddDbContext(this IServiceCollection services)
    {
        var connection =
            @"Server=(localdb)\mssqllocaldb;Database=DbName;Trusted_Connection=True;ConnectRetryCount=0";

        services.AddDbContext<MyContext>(
            options =>
                options.UseSqlServer(connection));
        return services;
    }
}
0
9/29/2018 7:49:58 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow