I am learning SOLID and I am trying to understand the Single Responsibility Principle.
The class WalkingData storages a "date" and a "walked distance". The class also read the stored data.
public class WalkingData
{
public DateTime Date { get; set; }
public int WalkedDistance { get; set; }
private string _filePath = @"c:\Data\Json.txt";
//Read Data from Json File
public List<WalkingData> GetAll()
{
//If file does not exist returns an empty list
if (!File.Exists(_filePath)) return new List<WalkingData>();
string jsonData;
//Read the existing Json file
using (StreamReader readtext = new StreamReader(_filePath))
{
jsonData = readtext.ReadToEnd();
}
//Deserialize the Json and returs a list of WalkingData
return JsonConvert.DeserializeObject<List<WalkingData>>(jsonData);
}
//save an instance of WalkingData in Json file
public void Save()
{
List<WalkingData> lstExistingWalkingData = new List<WalkingData>();
//if existing data, load it into lstExistingWalkingData
if (File.Exists(_filePath))
lstExistingWalkingData = GetAll();
//Add the current instace into lstExistingWalkingData
lstExistingWalkingData.Add(this);
//Serialize lstExistingWalkingData
string output = JsonConvert.SerializeObject(lstExistingWalkingData);
//Save the Json file
using (StreamWriter w = new StreamWriter(_filePath))
{
w.WriteLine(output);
}
}
}
After I applied the Single Responsabibly Principle I have the new code that I would like to confirm if I applied the principle in a reasonable way:
//This class is located on a library called BOL and has a reference to DAL library
public class WalkingData
{
public DateTime Date { get; set; }
public int WalkedDistance { get; set; }
}
//This class is located on a library called BOL and has a reference to DAL library
public class WalkingDataManager
{
WalkingDataRepository walkingDataRepository = new WalkingDataRepository();
public List<WalkingData> GetAll()
{
return walkingDataRepository.GetAll();
}
public void Save(WalkingData walkingData)
{
walkingDataRepository.Save(walkingData);
}
}
//this class is located in library Called DAL
internal class WalkingDataRepository
{
private string _filePath = @"c:\Data\Json.txt";
//Read Data from Json File
internal List<WalkingData> GetAll()
{
//If file does not exist returns an empty list
if (!File.Exists(_filePath)) return new List<WalkingData>();
string jsonData;
//Read the existing Json file
using (StreamReader readtext = new StreamReader(_filePath))
{
jsonData = readtext.ReadToEnd();
}
//Deserialize the Json and returs a list of WalkingData
return JsonConvert.DeserializeObject<List<WalkingData>>(jsonData);
}
//save an instance of WalkingData in Json file
internal void Save(WalkingData walkingData)
{
List<WalkingData> lstExistingWalkingData = new List<WalkingData>();
//if existing data, load it into lstExistingWalkingData
if (File.Exists(_filePath))
lstExistingWalkingData = GetAll();
//Add the current instace into lstExistingWalkingData
lstExistingWalkingData.Add(walkingData);
//Serialize lstExistingWalkingData
string output = JsonConvert.SerializeObject(lstExistingWalkingData);
//Save the Json file
using (StreamWriter w = new StreamWriter(_filePath))
{
w.WriteLine(output);
}
}
}
You separated the responsibility of saving data (WalkingDataRepository
) and also encapsulated it within WalkingDataManager
. The refactoring work totally aligns with what SRP says. In short, the SRP is not just about breaking down the functionalities but also about encapsulating them! I also wrote an article about it.
What bothers me is the anemic WalkingData
model. This is an anti-pattern. You should rather let WalkingData
handle the responsibility of maintaining its own data rather than creating another class WalkingDataManager
to do that job.