Author - StudySection Post Views - 1,885 views
pattern

Service Layer pattern in C#

As in the repository pattern, we introduced a data layer (Repository class) to deal with data or to separate the data access from the controller class. Here, In the service layer pattern, we have introduced a new layer called Service Layer to deal with the business logic or we can say validation logic.

SERVICE Layer is a class between the controller and repository in which we will implement business logic so as to separate the Presentation and Data Access.

Presentation Layer (Web App/REST API)
Service Layer (Service Library implementing the business logic)
Data access Layer (Repository class communication with database)
pattern

Let’s Understand with an Example:

Suppose we have a Student repository that is getting student data, and saving student data to/from the database.
Now suppose we want to add data to the database but we want to modify it or we want to validate the data before we insert that into the database. So according to the Service Layer pattern, we will introduce a new service class between the controller and the repository that class will contain all the logic to modify the data or validate it.

We have a student repository (As shown below) that contains two methods to add new students to the database and get the list of all students.
service layer pattern1

Then there is one Student controller (As shown below).The controller layer talks to the service layer. The service layer talks to the repository layer.

The controller will receive HTTP requests and then it will call the methods in the service layer.
service layer

Then there is one Service class implementing its interface. The service layer talks to Repository/Data Layer. It contains all the logic that we want to have in our application. (Suppose the user wants to get all the students whose Age is greater than 10 and save the new students in the database after validating only)

So there are three methods

GetStudents() – In this method, we have called the repository method to get all the students and then applied a condition to filter those students whose Age is greater than 10 and then send it back to the controller method to display the student data.

ValidateStudent() – This method is used to validate the student if the Name and Date of birth of the student are not empty.

AddStudent() – This method will first call to ValidateStudent() method and then if it’s a valid student it will call the repository method to add the student to the database. After adding the student to the database it will send a response to the controller.

service

service layer code

The presentation layer does not have any idea that there is any data access layer and vice versa.
The benefit of using this pattern is that suppose we want to have a REST API in ASP.NET MVC WebApi (want to change presentation layer) and for data access, we want to use ADO.net instead of Entity Framework or we want to use NHibernate in data access layer(want to change the Data access layer).
So if we have implemented the Service layer pattern then we can change the other two layers without tension/without touching the logic you have made. So it will reduce the developer’s headache if he needs to change the presentation or Data Access layers at any time.

jQuery presents a tree-like structure of all the elements on a webpage simplifying the syntax and further manipulating such elements. The jQuery Certification exam by StudySection will secure your fundamental knowledge and a basic understanding of jQuery as an asset to improve your skills.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri