Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of Interfaces on a service layer

In our project architecture we are using a classic MVC pattern including a classic service layer (opening the transaction and calling the DAO layer).

For each service we have an implementation and his interface. But to be honest, I'm pretty sure that for one service and his interface, we will never have more than one implementation. So ok maybe it's more clear to have the public method declared in the interface helping to know what the service does, but an interface is used to have multiple implementation and if we know that we won't have more than one implementation, should we keep them?

like image 380
Emilien Brigand Avatar asked Feb 24 '15 10:02

Emilien Brigand


People also ask

What is a service interface?

Service Interfaces A service interface is a published interface used to invoke a service. An interface can be implemented using any number of technologies, including WSDL, XML-RPC, COM+, CORBA (IDL), Java RMI, and others. A service interface can be described using multiple methods.

What is the purpose of an interface?

The purpose of interfaces is to allow the computer to enforce these properties and to know that an object of TYPE T (whatever the interface is ) must have functions called X,Y,Z, etc.

What is the use of service layer?

A service layer is an additional layer in an ASP.NET MVC application that mediates communication between a controller and repository layer. The service layer contains business logic. In particular, it contains validation logic. For example, the product service layer in Listing 3 has a CreateProduct() method.

Should every service have an interface?

No, it's not necessary for every class to implement an interface. Use interfaces only if they make your code cleaner and easier to write. If your program has no current need for to have more than 1 implementation for a given class, then you don't need an interface.


2 Answers

From the documentation:

Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler.

If you know that you will only have one implementation, the implementation itself will define the contract, so you can remove the interfaces.

But writing an interface could help you to better define the contract and also, you could need at a given point to write a mock for the service, in such a case you would benefit from the use of interfaces.

like image 53
antonio Avatar answered Sep 21 '22 19:09

antonio


i think this is a good approach for keeping interfaces.

reasons: 1. say you want to write junits for the same with a different implementations ex. inspite of getting data from database you want to get data from a separate datasource then a different implementation will suffice.

like image 21
anurag gupta Avatar answered Sep 25 '22 19:09

anurag gupta