N-Tier Architecture in ASP.NET Core

Part 4: Build layered architecture for separation of concern, scalability and maintenance

Classical N-Tier Architecture

3-Tier Architecture
BookRepository → Insert(book)
AuthorRepository → GetByName(firstName)
BookService → addBookWithAuthor(book) {
_unitOfWork.AuthorRepository.Insert(book.author)
_unitOfWork.BookRepository.Insert(book)
_unitOfWork.commit()
}
BookController   GET /authors
→ GetAllAuthors() → authorService.GetAllAuthors()

Create Solution for N-Tier Architecture

$ dotnet new sln --name aspnetcore

Create Data Access Layer

// Create class library and add this project to solution
$ dotnet new classlib --name webapi.data -f netcoreapp3.0
$ dotnet sln add webapi.data/webapi.data.csproj
// Navigate to DAL → cd webapi.data and Install Nuget packages
$ dotnet add package Microsoft.EntityFrameworkCore --version 3.1.0
$ dotnet add package Microsoft.EntityFrameworkCore.Design -v 3.1.0
$ dotnet add package Microsoft.EntityFrameworkCore.InMemory -v 3.1.0
Data Access Layer → webapi.data

Create Business Logic Layer

// Create class library and add this project to solution
$ dotnet new classlib --name webapi.business -f netcoreapp3.0
$ dotnet sln add webapi.business/webapi.business.csproj
// Navigate to BLL → cd webapi.business and reference to DAL project
$ dotnet add reference ../webapi.data/webapi.data.csproj
Business Logic Layer → webapi.business

Create Presentation Layer

// Create Web API project and add this project to solution
$ dotnet new webapi --name webapi.api -f netcoreapp3.0
$ dotnet sln add webapi.api/webapi.api.csproj
// Navigate to UI → cd webapi.api and reference to BL project
$ dotnet add reference ../webapi.business/webapi.business.csproj
Presentation/UI Layer → webapi.api

What about common entities such as models?

Solution References: One way references between projects for each layer
Solution References: All projects refer class library for core infrastructure
4-Tier Architecture: Infrastructure Layer for common entities.

Create Infrastructure Layer

// Create class library and add this project to solution
$ dotnet new classlib --name webapi.core -f netcoreapp3.0
$ dotnet sln add webapi.core/webapi.core.csproj
// Navigate to each of DAL, BLL, UI projects and refer Core project
$ dotnet add reference ../webapi.core/webapi.core.csproj
Core Infrastructure Layer → webapi.core

Configure dependency injection & circular dependency

How Composition Root resolves dependencies

Solution References: Class library project used for composition root

Create Dependency Injection Layer

// Create class library and add this project to solution
$ dotnet new classlib --name webapi.root -f netcoreapp3.0
$ dotnet sln add webapi.root/webapi.root.csproj
// Navigate to composition root and Install Nuget packages
$ dotnet add package Microsoft.Extensions.DependencyInjection.Abstractions -v 3.1.0
$ dotnet add package Microsoft.EntityFrameworkCore.InMemory -v 3.1.0
// Add reference to DAL, BL projects
$ dotnet add reference ../webapi.business/webapi.business.csproj
$ dotnet add reference ../webapi.data/webapi.data.csproj
CompositionRoot.cs
Composition Root Layer → webapi.root
// Navigate to webapi.api and refer webapi.root project
$ dotnet add reference ../webapi.root/webapi.root.csproj
public void ConfigureServices(IServiceCollection services)
{
CompositionRoot.injectDependencies(services);
services.AddControllers();
}
git clone https://github.com/udarabibile/aspnetcore-webapi.git
cd aspnetcore-webapi
git checkout layered-architecture
dotnet restore
dotnet run (or dotnet watch run)

Data Flow: Presentation → Business Logic → Data Access

// AuthorController
[HttpGet("{authorName}")]
public Task<Author> GetAuthorByName(String authorName) =>
authorService.GetAuthorByName(authorName);
// AuthorService
public Task<Author> GetAuthorByName(string firstName) =>
_unitOfWork.AuthorRepository.GetByName(firstName);
// AuthorRepository
public Task<Author> GetByName(string name) =>
context.Set<Author>().FirstOrDefaultAsync(a => a.Name == name);
Remove reference from one project to another
$ dotnet remove reference ../webapi.core/webapi.core.csproj
Remove project from solution
$ dotnet sln remove webapi.core/webapi.core.csproj
Remove Nuget package from project
$ dotnet remove package
Clean .NET Core solution or project if references are cached
$ dotnet clean
Publish ASP.NET Core Web API into given MacOSX version
$ dotnet publish aspnetcore.sln -c Release -r osx.10.14-x64 --output ./build --framework netcoreapp3.0
Run built assemblies in given OS
$ dotnet build/webapi.api.dll

Full Stack Developer | BiBi

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store