What’s three-layer architecture?Layer indicates the logical separation of components. Layered architecture concentrates on grouping related functionality within an application into distinct layers that are stacked vertically on top of each other. Each layer has unique namespaces and classes. Show
An N-layer application may reside on the same physical computer (same tier); each layer's components communicate with another layer’s components by well-defined interfaces. In C#, each layer is often implemented in a Dynamic Link Libraries (DLL) file. To help you understand more about the n-layer architecture, I’ve put together all pieces of information about three-layer with a practical example in this article as follows:
Advantages of three-layer architecture
How does it work?In three-layer architecture, the Presentation layer doesn’t communicate directly with the Data Access layer. The Business Logic layer works as a bridge between the Presentation layer and the Data Access layer. The three-layer architecture works as follows:
The diagram below illustrates how three-layer architecture works. How to build and deploy a three-layer application in C#?To guide you build and deploy a three-layer application, I have prepared a demo with the following components:
The Business Objects layer includes objects that are used in the application and common helper functions (without logic) used for all layers. In a three-layer architecture, this layer is optional. However, as we follow the OOP with C#, we should reduce the duplicate codes as much as possible. Therefore, using a layer to keep common codes instead of holding them in each layer is essential. To easily follow the article, you can download the demo of the three-layer architecture sample. It is built based on Repository + UnitOfWork pattern, which’s a well-designed pattern in C#. Here are the demo and practical examples of the Repository + UnitOfWork pattern. The diagram below explains how the application was designed.
Database Firstly, create a database with the design below to store the application’s data. You can execute the ThreeLayerSample.Database.sql which was placed inside the Data Access layer to create it. The demo will focus on getting the Work table items before representing them to users. Business Objects/Entity layerThreeLayerSample.Domain class project in this example. 1. Connect to the database with Entity Framework Core When we have the database, we need to create a mapping between the database and the application.
Once the command has finished, the database tables should be created into the code, name entities. As the entities could be used in all layers without any database or business logic, we should keep them in this layer. A DataContext class named “DemoContext” is also created. This class provides database access, so it should be placed in the Data Access layer. 2. Create Generic interfaces for Repository and UnitOfWork All data entities should have CRUD actions, so let’s create a generic interface named IRepository, and then the repository of each entity should implement this interface. ThreeLayerDomain/Interfaces/IRepository.cs
We need another interface named IUnitOfWork as follows: ThreeLayerDomain/Interfaces/IUnitOfWork.cs
They are interfaces without business/database logic here, so I put it in this layer. Data Access layerThreeLayerSample.Infrastructure class project in this example. 1. Implement generic classes By applying Generic Repository and Unit of Work design patterns, the data access layer classes are implemented as follows: ThreeLayerSample.Infrastructure/Repository.cs
ThreeLayerSample.Infrastructure/UnitOfWork.cs
And another DbFactory class that will initialize a DbContext when we use it. ThreeLayerSample.Infrastructure/DbFactory.cs
Business Logic layerThreeLayerSample.Service class project in this example. 1. Create an interface for the service Let’s create the interface for the WorkService that the Presentation layer depends on as we don’t want to tight the Presentation layer and Business Logic layer with a concrete implementation. ThreeLayerSample.Domain/Interfaces/Services/IWorkService.cs
2. Implement code for the service Next, implement business logic processing the Work service in a class named WorkService as follows. This is the business logic processing code, so we put this class in the Business Logic layer. As you can see, the service requests to get all items of the Work table via the Repository instance of Work entity, which was implemented in the generic repository. ThreeLayerSample.Service/WorkService.cs
Presentation layerThreeLayerSample.Web(Razor) ASP.NET Core Web App in this example. Follow this
tutorial to create an ASP.NET Core Razor page application. ThreeLayerSample.Web_Razor_/Extensions/ServiceCollectionExtensions .cs
Add your connection string into the appsettings.json file, and it’s the connection string which the Data Access layer will employ to establish a connection to the database (same value as the connection string in the Scaffold command). ThreeLayerSample.Web_Razor_/appsettings.json
Add an AppSettings class to the Entity layer. ThreeLayerSample.Domain/Models/AppSettings.cs
Open the Startup.cs file then add the following codes. At its constructor: read data from appsettings.json, then store it in the created AppSettings class. In the ConfigureServices: register instances for DataContext, its Factory, UnitOfWork, and WorkService to the application (using extension methods in ServiceCollectionExtensions class). ThreeLayerSample.Web_Razor_/Startup.cs
Open Index.cshtml.cs file, add the following code to inject WorkService, then get data from WorkService and set it to Works property. ThreeLayerSample.Web_Razor_/Pages/Index.cshtml.cs
In the Index.cshtml file, add the following code to present data to the UI. ThreeLayerSample.Web_Razor_/Pages/Index.cshtml
Testing Run the application to check for the result. Deployment Create a folder to store your source code. Open your IIS, then choose Create Website to host your application. Provide a name, a specific port, and a physical path to the source code folder for the Content Directory section. Make sure “Start website immediately” is ticked. Open your solution in your Visual Studio, then follow these steps: Right-click on ThreeLayerSample.Web(Razor), select Publish. Select “Folder”. Enter “Folder location” by creating the source code folder path below. Click to publish the project. Once you’ve done publishing, open the application on your browser to check for the result. Conclusion The layered architecture is well-designed for software development that helps organize your code with high maintainability, reusability, and readability. However, even though codes are well-organized in layers, they are deployed and run on the same physical machine, even on the same process. This may not be the right choice for complex applications, which require high availability and stability. We need to upgrade this architecture to another higher level named three-tier architecture which I will share in my next article. Thank you for reading, and happy coding! References
What are the layers of a three layer architecture quizlet?In three-layer architecture, the three layers consist of the view layer, the business logic layer, and the domain layer.
What is a computer that manages shared resources for access across a network called *?A server is a computer program or device that provides a service to another computer program and its user, also known as the client. In a data center, the physical computer that a server program runs on is also frequently referred to as a server.
What are the two key elements of describing the environment?An environmental analysis consists of two major components: internal factors and external factors.
When we use the term the Web What is the correct definition?The World Wide Web—commonly referred to as WWW, W3, or the Web—is an interconnected system of public webpages accessible through the Internet. The Web is not the same as the Internet: the Web is one of many applications built on top of the Internet.
|