SOLID Principles – Single-responsibility principle
The single-responsibility principle (SRP) is a computer-programming principle that states that every module, class or function in a computer program should have responsibility over a single part of that program’s functionality, and it should encapsulate that part. All of that module, class or function’s services should be narrowly aligned with that responsibility.
Wiki
The single-responsibility principle(SRP) is a common skill and usually uses skills in the computer-programing world. This idea starts from the principles of OOD article as part of the Principles of Object Oriented Design book by the Robert C. Martin. The originator of them expresses the SRP as “A class should have only one reason to change.” After the onion architecture advocate and the web API market growth, the developers start to use the SRP in them code and import to focus on the SRP design and application.
The SLP can make code easily maintainable and scalable in the projects and can use the generic type to implement, which achieves when the project’s runtime suddenly implements the instance to use, which is usually set under the scoped process. It also can separate the function in the class or the partial class file by the interface object. On the contrary, the interface object can inherit multiple classes, then implement the SLP. It has many application cases that can describe how it uses and isn’t only a way to implement. The developers should try to use a different way to implement SLP and let the code can become a beautiful article.
How to build the single-responsibility principle?
Basic part:
Step 1 Creating the data model class file.
public class EntityDemo
{
public string Code { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
Step 2 Creating the responsibility class file, which implement the CRUD and other etc.
public class BasicResponibility {
private readonly List<EntityDemo> _entities;
public BasicResponibility()
{
_entities = new List<EntityDemo>();
}
public void AddEntity(EntityDemo entity) => _entities.Add(entity);
public void RemoveEntity(int index) => _entities.RemoveAt(index);
public List<EntityDemo> Entities => _entities;
public override string ToString() => string.Join(Environment.NewLine, _entities.Select(x => $"Code: {x.Code}, Name: {x.Name}, Age: {x.Age}"));
}
Step 3 Implement the SLP in the console app.
var basicResponibility = new BasicResponibility();
basicResponibility.AddEntity(new EntityDemo {
Code = "000",
Name = "Andy",
Age = 18
});
basicResponibility.AddEntity(new EntityDemo
{
Code = "001",
Name = "Sam",
Age = 81
});
Console.WriteLine(basicResponibility.ToString());
basicResponibility.RemoveEntity(1);
Console.WriteLine("Remove index = 1.");
Console.WriteLine(basicResponibility.ToString());
Advanced part:
Step 1 Creating the interface object, then statement a few functions. It has the generic type statement.
public interface IResponsibility<T> where T : class {
void AddEntity(T entity);
void RemoveEntity(int index);
List<T> Entities { get; }
}
Step 2 Create the responsibility class, which inherits the interface object and follow the generic type mechanism to implement.
public class BasicResponibility<T> : IResponsibility<T> where T : class {
private readonly List<T> _entities;
public BasicResponibility()
{
_entities = new List<T>();
}
public void AddEntity(T entity) => _entities.Add(entity);
public void RemoveEntity(int index) => _entities.RemoveAt(index);
public List<T> Entities => _entities;
public override string ToString() {
var tmp = _entities;
StringBuilder result = new StringBuilder();
foreach (var item in _entities)
{
var _o = item.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)
.ToDictionary(prop => prop.Name, prop => prop.GetValue(item, null));
result.Append(string.Join(Environment.NewLine, _o.Select(x => x.Key +":"+x.Value).ToArray()));
}
return result.ToString();
}
}
Step 3 Implement the SLP in the web project.
var basicResponibility = new BasicResponibility<EntityDemo>();
basicResponibility.AddEntity(new EntityDemo {
Code = "000",
Name = "Andy",
Age = 18
});
basicResponibility.AddEntity(new EntityDemo
{
Code = "001",
Name = "Sam",
Age = 81
});
Console.WriteLine(basicResponibility.ToString());
basicResponibility.RemoveEntity(1);
Console.WriteLine("Remove index = 1.");
Console.WriteLine(basicResponibility.ToString());
Reference
- SOLID Principles in C# – Single Responsibility Principle
- Single-responsibility principle – Wiki
- Single Responsibility Principle (SRP) – Principles Wiki