简体   繁体   中英

How to configure N-tier architecture for ASP.NET Core MVC with EF Core code first migrations

Apologies if some of my logic is flawed as I am new to medium scale application development in ASP.NET Core. I am using Entity Framework Core (code-first) and ASP.NET Core 5 MVC and want to be able to split my project up into 3 distinct tiers:

  1. Data Tier (Data Access Layer)

This would contain the migrations, ApplicationDbContext and POCOs representing the entities in my program.

  1. Services Tier

This would contain interfaces and their concrete implementations for accessing the data from the Data Layer.

// For example
public UserService : IUserService
{
   public async Task<IEnumerable<User>> GetAllUsers()
   {
      // ApplicationDbContext injected in constructor and stored in readonly _context variable
      await _context.Users.ToListAsync();
   }
}
  1. Presentation Layer (ASP.NET Core MVC UI)

This would contain the typical MVC things such as view models, controllers and Razor Views. In this layer I would like to inject the services into the controllers for example:

public class HomeController : Controller
{
   private readonly IUserService _userService;

   public HomeController(IUserService userService)
   {
     _userService = userService;
   }
   
   public async Task<IActionResult> Index()
   {
       return await _userService.GetAllUsers();
   }
}

The ideal outcome of this architecture being that at some point we could change the underlying implementation of the ORM and database we are using, and the presentation layer would not have to change.

Currently however, I still need to register the AppDbContext in the dependency injection container on startup in the presentation layer (as the UserService has a dependency on the ApplicationDbContext and the DI container is attempting to resolve this).

services.AddDbContext<ApplicationDbContext>(options => 
    options.UseSqlServer(Configuration.GetConnectionString("MyConnectionString")));
services.AddScoped <IUserService, UserService>();

Is there a way I can remove all references of data access (including the DbContext ) from the presentation layer, so that if the ORM (EF Core) or the underlying database used (SQL Server for MySQL for example) changed, the presentation layer would not need to be updated and redeployed?

Practically speaking changing underneath DB after your application goes into production rarely happens, and if it does you change the related code or may be re-write entire app.
Anyway take a look at this for implementing separation of concern https://dotnet.microsoft.com/learn/aspnet/architecture

  • Its okay to reference data layer in UI layer just in startup.cs for DI purpose.
  • Create entities in service layer, so UI layer can map Entities to Model

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM