What is .NET 3-Tier Architecture?

3-tier architecture is a software design pattern that divides an application into three logical and physical tiers: the presentation tier, the application tier, and the data tier.

The presentation tier is responsible for displaying the user interface and collecting user input. The application tier contains the business logic of the application, such as validation, calculation, and persistence. The data tier is responsible for storing and retrieving data from a database or other data source.

Benefits of using 3-tier architecture in C#:
  1. Improved separation of concerns: 3-tier architecture helps to improve the separation of concerns by dividing the application into three distinct tiers. This makes the application easier to design, develop, and maintain.
  2. Improved scalability: 3-tier architecture makes it easier to scale the application by allowing you to scale each tier independently.
  3. Improved security: 3-tier architecture can help to improve security by isolating the data tier from the other tiers. This makes it more difficult for attackers to gain access to sensitive data.

3-tier architecture can be implemented in any programming language, including C#. To implement 3-tier architecture in C#, you would typically use a layered architecture. This means that each tier would be implemented as a separate layer, and the layers would communicate with each other through well-defined interfaces.

Presentation Tier

The Presentation Layer is responsible for handling user interactions, displaying information to users, and receiving user inputs. It typically consists of user interfaces (UI), such as web pages, desktop applications, or mobile app screens. This layer should focus on presenting data to the user and collecting user inputs. It should not contain business logic or direct database access.

Example (ASP.NET MVC Controller):
public class ProductController : Controller { private readonly ProductService productService; public ProductController() { this.productService = new ProductService(); } public ActionResult Index() { var products = productService.GetAllProducts(); return View(products); } // Handle user interactions, input validation, and UI-related logic. }

Business Logic Tier

The Business Logic Layer (also known as the Application Layer) is responsible for implementing the application's business rules, logic, and workflows. It serves as an intermediary between the Presentation Layer and the Data Access Layer, ensuring that data is processed correctly and consistently. This layer encapsulates business logic, performs data validation, and orchestrates interactions with the Data Access Layer.

Example (C# Business Logic Class):
public class ProductService { private readonly ProductRepository productRepository; public ProductService() { this.productRepository = new ProductRepository(); } public IEnumerable<Product> GetAllProducts() { // Business logic, validation, and coordination of data access. return productRepository.GetAll(); } public void CreateProduct(Product product) { // Business logic for creating a new product. productRepository.Add(product); } // More business logic methods... }

Data Access Tier

The Data Access Layer is responsible for interacting with the data storage, such as a database or external APIs. It encapsulates data access operations like querying, inserting, updating, and deleting data. This layer abstracts the details of data storage and retrieval, ensuring data integrity and security.

Example (C# Data Access Class with Entity Framework):
public class ProductRepository { private readonly DbContext dbContext; public ProductRepository() { this.dbContext = new ApplicationDbContext(); // Entity Framework context } public IEnumerable<Product> GetAll() { // Data retrieval logic using Entity Framework. return dbContext.Products.ToList(); } public void Add(Product product) { // Data insertion logic using Entity Framework. dbContext.Products.Add(product); dbContext.SaveChanges(); } // More data access methods... }

In a 3-Tier Architecture, these layers work together to maintain a clear separation of concerns, making the application more maintainable, scalable, and testable. The Presentation Layer focuses on the user interface, the Business Logic Layer handles business rules, and the Data Access Layer deals with data storage, ensuring a well-organized and modular codebase.

Example | 3-tier architecture implementation in C#

Here is a complete source code example of a simple 3-tier architecture implementation in C#:

// Presentation tier public class CustomerForm { private readonly ICustomerService customerService; public CustomerForm(ICustomerService customerService) { this.customerService = customerService; } public void LoadCustomerById(int id) { var customer = customerService.GetCustomerById(id); // Display the customer information in the form } public void SaveCustomer() { // Get the customer information from the form customerService.SaveCustomer(customer); } } // Application tier public interface ICustomerService { Customer GetCustomerById(int id); void SaveCustomer(Customer customer); } public class CustomerService : ICustomerService { private readonly ICustomerRepository customerRepository; public CustomerService(ICustomerRepository customerRepository) { this.customerRepository = customerRepository; } public Customer GetCustomerById(int id) { return customerRepository.GetById(id); } public void SaveCustomer(Customer customer) { customerRepository.Save(customer); } } // Data tier public interface ICustomerRepository { Customer GetById(int id); void Save(Customer customer); } public class CustomerRepository : ICustomerRepository { private readonly SqlConnection connection; public CustomerRepository(SqlConnection connection) { this.connection = connection; } public Customer GetById(int id) { // Get the customer from the database } public void Save(Customer customer) { // Save the customer to the database } }

In this example, the CustomerForm class represents the presentation tier. The CustomerService class represents the application tier. The CustomerRepository class represents the data tier.

To use the 3-tier architecture, you would simply create an instance of the CustomerForm class and call its LoadCustomerById() or SaveCustomer() method. The CustomerForm class would then communicate with the CustomerService class to get or save the customer information. The CustomerService class would then communicate with the CustomerRepository class to get or save the customer information from the database.

Conclusion

The 3-Tier Architecture in C# is an architectural pattern that divides an application into three distinct layers: Presentation, Business Logic, and Data Access. The Presentation Layer handles user interfaces, the Business Logic Layer manages application rules and workflows, and the Data Access Layer deals with data storage and retrieval, promoting modular, maintainable, and scalable software design.