This article will change your perspective on working with the Single Responsibility Principle (SRP). The main point is to focus on finding balance when designing object-oriented systems and applying SRP.
Let’s start by explaining the most fundamental ideas to make this easier to understand.
SRP means that each module should have one reason to change. This means that those designing a module should understand who owns it and what type of changes might be needed. And remember, there should be only one reason for change!
Everything is clear: our goal is to break down one module into sub-modules and connect them using a single method (like message passing). The main benefits are preventing spread, keeping changes localized, and reducing the risk of breakage.
Our anti-SRP example represents one of the balances in Object-Oriented Design (OOD). In fact, there are many examples.
Example of Single Responsibility Obsession (Anti-SRP)
Using any principles in an obsessive way reminds us of a silver bullet, but since there is no silver bullet, obsession takes us in the opposite direction of the principle and can have very serious consequences.
For that reason, we should not approach it in an obsessive way; an obsession with SRP turns it into anti-SRP (it means it leads to many small classes/methods and spreads the logic between them.).
Let’s assume we have a data-driven application and need to communicate with database tables. In this case, it would be a repository. This means our context (which defines the work area and its boundaries) is a database service that fits all the functionality together and cannot be changed.
I will skip the CRC (Class-Responsibility-Collaboration) card and start by declaring functionalities to help demonstrate our context.
First of all, we will start with a demonstration of anti-SRP. The idea of the anti-single responsibility principle starts with obsessive use. Let’s start with an example code.
public class GetRepo<T, Guid> where T : class
{
private readonly DatabaseService _dbService;
public GetRepo(DatabaseService dbService)
{
_dbService = dbService;
}
public IEnumerable<T> GetAll()
{
return _dbService.ToList();
}
public T Get(Guid id)
{
if (id == Guid.Empty)
throw new Exception("Id can not be empty!");
return _dbService.SingleOrDefault(x => x.Id == id);
}
}
public class InsertRepo<T> where T : class
{
private readonly DatabaseService _dbService;
public InsertRepo(DatabaseService dbService)
{
_dbService = dbService;
}
public bool Insert(T instance)
{
if (default(T) == instance)
throw new Exception("Instance can not be empty!");
int rowEffected = _dbService.Insert(instance);
return rowEffected != 0;
}
}
public class UpdateRepo<T> where T : class
{
private readonly DatabaseService _dbService;
public UpdateRepo(DatabaseService dbService)
{
_dbService = dbService;
}
public bool Update(T instance)
{
if (default(T) == instance)
throw new Exception("Instance can not be empty!");
int rowEffected = _dbService.Update(instance, x => x.Id == instance.Id);
return rowEffected != 0;
}
}
public class DeleteRepo<T, Guid> where T : class
{
private readonly DatabaseService _dbService;
public DeleteRepo(DatabaseService dbService)
{
_dbService = dbService;
}
public bool Delete(Guid id)
{
if (id == Guid.Empty)
throw new Exception("Id can not be empty!");
int rowEffected = _dbService.Delete(x => x.Id == instance.Id);
return rowEffected != 0;
}
}
Join the conversation! Your thoughts help the community grow.