SRP or Single responsibility principle is one of the design principles of the SOLID principles.According to this principle a class should have only a single responsibility.So if a class is performing database operations then that class should not perform any input/output operations.
In the following example we will see an implementation of a class which follows this principle and another one which violates this principle.
The following Employee class represents and Employee and contains a GetEmployee method.The GetEmployee method reads the list of employees stores in a csv file and finds an employee with a given employee id.
class Employee { public string Name{get;set;} public string Address{get;set;} public int Id{get;set;} public Employee GetEmployee(int id) { var reader = new StreamReader(File.OpenRead(@"C:Employees.csv")); List<string> lstEmployees = new List<string>(); Employee emp=null; while (!reader.EndOfStream) { var line = reader.ReadLine(); var values = line.Split(','); if(values[0]==id.ToString()) { emp=new Employee{Id=id,Name=values[1],Address=values[2]}; } } return emp; } public bool UpdateEmployee(Employee emp) { //logic to update the employee details } }
Though the above class works perfectly but there is one problem in the above implementation.The Employee class is reading values from the csv file directly.So it is handling two responsibilities:
- It represents an Employee and contains operations for an employee
- It is reading the stored csv file.It contains the logic to access and read values from the file.
The problem with current implementation is that if the structure of the stored csv file changes then that means changing the Employee class as well.And if we are implementing the csv file access logic in the different classes then that means not only locating and changing all those files but also thoroughly testing the changes for the undesirable consequences.
Since class className violates this principle so if we modify this class hence other parts of the program can also get impacted as a result of this change.
In the following example we have moved the responsibility of reading the csv file from the Employee to the CSV class.The employee class is just calling the CSV class to read the values of the Employees in the file.
class Employee { public string Name{get;set;} public string Address{get;set;} public int Id{get;set;} public Employee GetEmployee(int id) { CSV<Employee> csv=new CSV<Employee>(); return csv.ReadCSV(id); } class CSV<T> where T:Employee ,new() { public T ReadCSV(int id) { var reader = new StreamReader(File.OpenRead(@"C:Employees.csv")); T emp=new T(); // Employee emp=null; while (!reader.EndOfStream) { var line = reader.ReadLine(); var values = line.Split(','); if(values[0]==id.ToString()) { emp=new T{Id=id,Name=values[1],Address=values[2]}; } } return emp; } }
Since the above implementation follows this principle ,so if the logic to handle the csv file changes we can confidently modify the CSV class without caring whether any other parts of the program will get impacted because of this change ,since the csv file handling logic belongs only to the CSV class.
This is the main advantage of Single responsibility principle changes in one class does not have any impact on other classes or any other parts of the program.
Leave a Reply