lundi 24 juin 2019

Single Responsibility Principle for Dao Classes

This is a sample code which haven't followed the single responsibility principle,

public class EmailSender
{
  public void SendEmail(string customerID, 
                       string emailNotificationType)
  {
    //STEP1: load customer details
    //STEP2: get email content
    //STEP3: send email (using SmtpClient class)
  }

  public string GetEmailContent(Customer customer, 
                 string emailNotificationType)
   {
    // Build the email notification content
   }
}

I agree, It will create the issue in case If I need to do following,

-> Changes in the way, you are loading customer details.

-> Changes to the email content because of requirement enhancements / changes.

-> If there any change in the way you are sending the email instead of using SmtpClient class or something like that.

so we need to apply Single Responsibility Principle to separate the classes. I totally agree on this principle. Say I need to create three classes like

EmailSender - which only focus on sending email CustomerRepository - which only focus on fetching customer data EmailContentBuilder - which parse the email content

But say If I have a Dao like CustomerDao, As of now I have all the curd operations related to CustomerDao in the same class like below

CustomerDao class
- add()
- update() - get()
- getAll()
- update()

Do we need to apply SingleResponsibilityPrinciple here? If so How to apply for CustomerDao class?

Thanks,
Harry

Aucun commentaire:

Enregistrer un commentaire