Unit Of Work pattern concepts in .NET

The Repository pattern and Unit of Work pattern are used together in most time. Unit of work pattern can increased the software flexibility and maintainability, improve the ORM model in the web application architecture structures.

The unit of work pattern is a critical concept in the repository pattern because all software programs require a statistics base framework wherein it communicates(CRUD) to save and get better data. The unit of work pattern can provide the block concepts that execute the CRUD in the same database. On the other hand, if the programs want to perform other database data, the developers will provide the new unit of work pattern source code to run the business logic layers(BLL) or other operations.

It can quickly classify three repository pattern categories: the non-repository model, the repository model, and the repository add the unit of work pattern. These don’t care about what kind of ORM should be used in the application, and these only foci on what type of communication will give more executing effectiveness between the applications and the databases, which also need to add the disposal of source concepts.

Fig 1 Non-repository V.S. Repository V.S. Repository + UnitOfWork

Non-repository model

It can directly understand the non-repository model is no one adornments in the core layer of the onion architecture, which means the data will be accessed directly from the DbContext at the controller. Sometimes, it will be locked by the client requests because the controller isn’t good at putting the business logic layer(BLL) here and disposing of resource operation, which is a kind of single transaction operation. Every client request is regarded as the database connection no., which occupies the number of database pooling or the number of database connections. The web server doesn’t have more of the number of sites to handle the client requests. For example, the IIS queue length default is 1,000. If the client request number is more than the IIS queue length default value, the IIS will return the 503 code to the client-side, which is the client-side doesn’t want to get the messages.

The non-repository model is given for the beginner to know what the MVC application is quick. It is not a good choice in the web application.

Fig 2 Non-repository

Repository model

It is a common way in the web application. Using the repository model is usually with the interface class objects, which add the IDispose of interface class object, which is in the core layer of the onion architecture, which means the data will be accessed via the repository and logic will be hidden from the application layers by source code. Therefore, the disposing of resource operation can be put in the repository model class files by developers. It can discover one thing: the repository model advantage is better than the non-repository model and solves the non-repository model shortcoming. Other than that, the non-repository and repository models can’t solve the times of the visit database when the business logic layer(BLL) has multiple repositories. Every time visits the database, that will increase the website application and the database loading. It is not a good design pattern solution and should design the decrease model the number of visit databases of website applications.

Fig 3 Repository

Repository + UnitOfWork

It will be a good choice when the developers want to use repository skills in website applications. Unit of work concepts provides the block concepts, which have multiple repository classes to visit the same database, which means all the transactions of repositories will be one single transaction. It can make the project easy to maintain and has malleability. When the program wants to visit another database, the program can execute another database unit of work, then the last unit of work can dispose of database resource operation. It can precisely control the website application resources and the request lifetime.

Fig 4 Repository + UnitOfWork

How to implement the UnitOfWork?

According to my GitHub sample projects, which are a few different parts with other blog site body descriptions, this article descript an exact thing that uses a different way to implement and provides another viewpoint to see the unit of work pattern and how to the UnitOfWork implement.

Page: 1 2 3

davidsky69

Recent Posts

API Gateway in .NET 5 with Ocelot

What is the API gateway? An API gateway is an API management tool that sits…

3 years ago

.NET 5 application with Onion architecture

The .NET 5 SDK is a kind of milestone in the .NET world. The .NET…

4 years ago

SOLID Principles – Dependency inversion principle

In object-oriented design, the dependency inversion principle is a specific methodology for loosely coupling software…

4 years ago

SOLID Principles – Interface segregation principle

In the field of software engineering, the interface segregation principle (ISP) states that no code…

4 years ago

SOLID Principles – Liskov substitution principle

Subtype Requirement: Let  be a property provable about objects  of type T. Then  should be true for objects  of type S where S is…

4 years ago

SOLID Principles – Open-closed principle

In object-oriented programming, the open–closed principle states "software entities (classes, modules, functions, etc.) should be…

4 years ago