Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET service vs repository layers

What is the difference between a service layer and a repository? I have worked through a lot of demo ASP.NET MVC apps and most of them have just repositories. And some have a mixture of both. When do you use just repositories and when do you use services / or both? The same is true for ASP.NET web apps.

like image 549
Brendan Vogt Avatar asked Dec 06 '10 15:12

Brendan Vogt


People also ask

What is the difference between service layer and repository layer?

Repository layer is implemented to access the database and helps to extend the CRUD operations on the database. Whereas a service layer consists of the business logic of the application and may use the repository layer to implement certain logic involving the database.

What is a repository layer?

Repository layer is added between the domain and data mapping layers to isolate domain objects from details of the database access code and to minimize scattering and duplication of query code. The Repository pattern is especially useful in systems where number of domain classes is large or heavy querying is utilized.

What is repository Service pattern?

Overview. The Repository-Service pattern breaks up the business layer of the app into two distinct layers. The lower layer is the Repositories. These classes handle getting data into and out of our data store, with the important caveat that each Repository only works against a single Model class.


2 Answers

Repositories act just as gateways to your data storage (sql database, xml file etc.) while services usually implement some business rules on your data before sending the data to be saved in the database via a repository.

consider this example:

class UserRepository : IUserRepository {    public void Create(User userToCreate)    {        //update tracking and save to repository        _userToCreate.DateCreated = DateTime.Now;        _dataContext.AddNew(userToCreate);    } }   class UserService : IUserService  {    private IUserRepository _repository;     public UserService(IUserRepository repository)    {         _repository = repository;    }     public void Create(User createdByUser, User userToCreate)    {        //implement some business rules        if(!createdByUser.HasRights(UserRights.CanCreateNewUser))            throw new Exception("This user '"+createdByUser.Name+"' does not have the rights to create a new user");         //update rules auditing        _userToCreate.CreatedByUserId = createdByUser.Id;         //save entity to repository        _repository.Create(userToCreate);    } } 

Then in your Controller action you will use the service directly where all your business rules can be applied. That way you can test you controllers, business rules (services) and persistence (repositories) separately/independently using mocks.

    public ActionResult CreateUser(User newUser)     {         if(ModelState.IsValid)         {            _userService.Create(this.CurrentUser, newUser);            if(newUser.Id > 0)                return RedirectToAction("UserCreated");         }         return View(newUser);     } 
like image 94
Tawani Avatar answered Oct 11 '22 19:10

Tawani


A repository typically handles data-access only. A service layer will use a repository, and apply any additional business logic. Think of the repository as a re-usable layer than could be used by anything that wants to access your data. Different apps might have different business rules (that would go in the service layer), but could all use the same repository layer implmentation

like image 22
kenwarner Avatar answered Oct 11 '22 19:10

kenwarner