In my previous Article ADO .NET Evolution: Part I: 2-Tier to 3-Tier I have explained the mistakes we make when developing ADO .Net applications.
Here in this Article I will try to explain a real implementation of the 3-Tier Application with an example. You might be thinking "During this new era of Data Access Technology what is the need of learning a Basic 3-Tier Application development process?" Yes you are correct, but before implementing any new .Net Design Patterns like LINQ, Entity Model, MVC, MVP or MVVM we need to understand the basics of 3-Tier Application.
Basic Rules and Concept of 3-Tier Application
This Article will also guide you the Path way to develop a 3-Tier Application with some simple Steps.
- There will be clear separation of layers. such as Presentation Layer, Business Logic Layer, Data Access Layer and Entity Layer.
- This kind of separation of layers will give the facility to host Business Logic in Business Server and Data Access in Database Server.
- Data Encapsulation done by Entity Layer which can serialized across the layer, from Presentation to Business and Business to Data Access and vice versa.
- Business Logic will encapsulated in Business Logic layer.
- Data Access process and Security will be encapsulated in Data Access Layer.
- Finally the Presentation Layer will access only the Business Logic Layer to Add a new record, Modify an existing record, to View records by list or a particular Record.
- Presentation Layer will never communicate directly with Database Object or Data Access Layer.
Step - 1
Add 4 Projects to an empty Visual Studio Solution. Of of these, 3 of the projects should be a Class Library (For Class Library the output will be a dll) and the other one can be your application (Windows Form Application or Web Application or Web site). You should follow a better naming convention for the projects. For example you can have your projects in your Solution like this.
|Projects ||Application ||Type of Project|
|Entity ||ApplicationName.Entity ||Class Library|
|DAL ||ApplicationName.DAL ||Class Library|
|BLL ||ApplicationName. ||Class Library|
|Application ||Application ||Win/Web Application/Site|
So now the Solution Explorer will looks like this.
Step - 2
Now add Entity classes to the Entity Project. A simple Entity Class will look like this.
Step - 3
Add a reference of Entity to the DAL Project, so we can access the Entity Classes within the DAL. Inside a DAL class you can write methods to access and to manipulate data from a Database. A simple DAL class can have all the following methods:
In the Attached Sample code you can find the code for DAL, but for understanding purposes see the Upsert method bellow, which insert/Update a record in Database. (To know more about Upsert Method Read my previous Article How to: Implement UPSERT () Method in C#).
And the Select Method will look like this.
Step - 4
Now add Classes to BLL for each Entity and you can have the methods list like this. Before that do not forget to add reference from Entity and DAL.
Step - 5
So you are done with the Entity Layer, Data Access Layer and Business Logic Layer. Now you can add a very good look and feel and user friendly UI as the Presentation Layer. To access the Entity and Business Logic layer add references from both the projects.
- In your Save button Click event you can create an object of BLL and can call the Add() method of it.
- To modify an existing record you can call the Edit() Method of BLL.
- And to bind data to a GridView you can call GetList() Method.
So cheer up... now your old 2-Tier Application is ready with a new Flavor of 3-Tier Architecture.