Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should an entity service class call another entity's service or its repository

I am working on an ASP.Net MVC 3 web application (EF 4.1) separated in layers: Models, Repositories, Services, Controllers, ViewModels in some cases, and Views.

Now my question is one of best practice. Should an entity service class that needs access to another entity use its service or its repository. For example, let's say that a service method for entity A needs to update entity B when A is created. Should A's service class use B's repository or service layer? Both are possible, but what is the best practice? Personally, I would prefer a service to access another service. That way, it as access to more evolved methods so to say.

like image 201
Jean-François Beauchamp Avatar asked Sep 04 '11 15:09

Jean-François Beauchamp


People also ask

When should I use service and Repository?

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.

Should controller Call Repository directly?

Yes, it is a bad practice. Controller should be used only to control the flow of your application, get input from client apps, call service and pass data to the view (JSON/HTML/XML/etc). Repository/DAO should be used to retrieve/persist the data without knowing any business logic.

Is service same as Repository?

The repository is where the data is stored. The service is what manipulates the data. In a real-world situation comparison, if your money is stored in a vault in a bank, the vault is the repository. The teller that deposits, withdraws, etc is the service.

What is @service and @repository?

@Service annotates classes at the service layer. @Repository annotates classes at the persistence layer, which will act as a database repository.


2 Answers

I would prefer calling between service classes, because you may need also some business logic from another service. But be careful to avoid circular dependencies. I'm recommending you to use dependency injection which will help you to avoid possible circular dependencies. Also consider to create interfaces for your service classes and use this interface from your client classes (pass the concrete implementation to the constructor).

Then your SericesA will look:

class ServiceA : IServiceA
{
    public ResultA Method1() { //some logic };
    public void Method2() { //some logic };
}

ServiceB which depends on ServcieA.

class ServiceB: IServiceB
{
   private IServiceA _serviceA;
   public ServiceB(IServiceA serviceA)
   {
      _serviceA = serviceA;
   }

    public ResultB Method()
    {
         var result = _serviceA.Method1();
         // get result from service A and create and return result for service B
    }
}
like image 150
Marian Ban Avatar answered Oct 21 '22 02:10

Marian Ban


I tend to fall back on commonly used principles and practices for these sorts of decisions; DRY (Don't Repeat Yourself) and KISS (Keep It Simple, Stupid) may be applicable here.

Unless you needed to repeat some logic as a result of bypassing entity B's service class, I would call entity B's repository directly from entity A's service class.

It is a minor detail, but it means there is one less class involved (ServiceClassA > RepositoryClassB rather than ServiceClassA > ServiceClassB > RepositoryClassB) which makes it a simpler solution in my mind.

HTH

like image 35
Teppic Avatar answered Oct 21 '22 01:10

Teppic