.Net 6 + WebApi Basics 7 - 3-Tier Architecture
2023-01-15 22:08:47  .Net  >> .Net6  >> WebApi

3-Tier Architecture for .NET Framework

Regarding 3-Tier Architecture, you can find a lot of information on the web.

C# Corner

GeekforGeek

Simply put, 3-Tier Architecture contains three layers:

Presentation Tier (UI)

Business Tier (BLL)

Data Tier (DAL)

Generally speaking, however, the three-tier architecture is not only these three tiers, but there is also a
Models tier, which corresponds to the entity classes in the database.

Each of these three tiers has its own task and is interconnected but does not interfere with each other.

UI : Interact directly with users, like web pages.

BLL: this tier processes the business logic, like execute command, make calculations or middlewares. It is also responsible for the interaction between DAL and UI tiers.

DAL: Only responsible for the interaction between BLL and database.

3tier

New 3-Tier Architecture for .NET

After Microsoft published the .NET Core, the previous 3-Tier Architecture has been modified to new views: Repository, Services, and Api, which are more intuitive to understand what tier’s job is respectively.

However, even the names of tiers have been changed, their actual job keeps the same.

Repository can be seen as DAL, Database.

Services can be seen as BLL, Business logic.

API can be seen as UI, Front-end

Models No Changes

Let’s make some practice with the weather app to figure out how each of them works.

Build three Class Libray projects, and named Repository, Services and Models, repectively.

Class Libray
Class Libray2

After creation of each tier, you need to link them based on their processing logics by Project Reference.

Respository project reference with Models
Alt text

Services project reference with Respository
Alt text

WebApplication1 (API or UI) project reference with Services
Alt text

Why 3-tier?

Decoupling, polymorphism, specification development.

How to use 3-tier?

NEXT, Let’s figure out the code part.

Create new class in Models, Repository, and Services, and name them UserClass.cs, UserRepository.cs, UserService.cs, respectively.

3-tier class

UserClass.cs

You can move UserClass.cs from main program to Models.

Note: Install SqlSugar database in NuGet for Models, as it presents the entities of database.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
namespace Models
{
/// <summary>
///
/// </summary>
[SugarTable("Table_User")]
public class UserClass
{
[SugarColumn(IsIdentity = true, IsPrimaryKey = true)]
public int ID { get; set; }
[SugarColumn(ColumnName = "User")]
public string UserName { get; set; }
public int Age { get; set; }
}
}

UserRepository.cs

Build the object to connect with database, and GetList() to get all of the data from Models

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
namespace Repository
{
public class UserRepository
{
private ISqlSugarClient Db { get; }
public UserRepository(ISqlSugarClient db)
{
Db = db;
}
/// <summary>
/// Get data from DB from UserRepository
/// </summary>
/// <returns></returns>
public List<UserClass> GetList()
{
return Db.Queryable<UserClass>().ToList();
}
}
}

UserService.cs

GetUsers() to build a business logic to gain data from Repository tier.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
namespace Services
{
public class UserService
{
public UserService(UserRepository userRepository)
{
UserRepository = userRepository;
}
public UserRepository UserRepository { get; }
public List<UserClass> GetUsers()
{
return UserRepository.GetList();
}
}
}

Now, you have already built the link with Models(Entity) tier in the Repository(Database) tier and use them in the Service(Business Logic) tier.

Then, we need to figure out the API(Front-end) tier.
All what needs to do is adding services to the IOC container by Dependency Injection in Program.cs.

1
2
builder.Services.AddScoped<UserRepository>();
builder.Services.AddScoped<UserService>();

AddSingleton vs AddScoped vs AddTransient

Video

Practice


Next: .Net 6 + WebApi Basics 8

Last: .Net 6 + WebApi Basics 6


Good Day
😎