Single Responsibility Principle

3

According to SOLID, Single Responsibility Principle says, It is of sole responsibility and says that each class must deal with only one task and that applies to its methods. I have a Save method which updates a record and also saves a record, it has two responsibilities.

if (HelpAccesoDatos.Existe)) {
   try {
      ClienteRepository.Actualizar(cliente);
      MensajeLogica = "REGISTRO ACTUALIZADO!!!";
   } catch (Exception) {
      MensajeError = "No se puedo actualizar el registro";
   }
} else {
   try {
      ClienteRepository.Guardar(cliente);
      MensajeLogica = "REGISTRO GUARDADO!!!";
   } catch (Exception) {
      MensajeError = "No se pudo guardar el registro";
   }
}

Should I separate this method in two? Is it a bad programming practice? From my point of view I feel comfortable with this method both for code maintenance and implementation. I thank you in advance for your recommendations.

    
asked by Pedro Ávila 14.04.2016 в 22:38
source

1 answer

4

> > Should I separate this method in two?

It is not necessary to separate the functionality in this case because, although it defines logic for two possible paths, they refer to a single type of operation, that of persisting data.

That practice SOLID refers to cases where the entity besides saving the entity for example sends an email, or maybe in addition to persisting it connects to a web service to perform another operation, there if you were clearly violating the SOLID principle that you mention.

Or if in the functionality of the class you define persistence for many different entity types.

In your case at most you are validating to perform an INSERT or UPDATE, but it is the same entity and a simple persistence operation.

SRP - Principle of simple responsibility

S.O.L.I.D Principle of Simple Responsibility

    
answered by 14.04.2016 / 22:50
source