Get easily started using the ChainOfResponsibility Design Pattern
public class Employee
{
public string Name { get; set; }
public string Position { get; set; }
public int Salary { get; set; }
}
We want to increase the salary of managers by 200 and those of engineers by 500
For each chain , we need to inherit from AbstractChain<TRequest>
.
Below is an example of 2 chains. In each chain, we need to override the 2 functions, IsChainResponsible
and RequestHandler
.
A RequestHandler
is only invoked when IsChainResponsible
returns True
.
public class ManagerSalaryChanger : AbstractChain<Employee>
{
public override bool IsChainResponsible(Employee request)
=> request.Position == "Manager";
public override void RequestHandler(Employee request)
=> request.Salary += 200;
}
public class EngineerSalaryChanger : AbstractChain<Employee>
{
public override bool IsChainResponsible(Employee request)
=> request.Position.Contains("Engineer");
public override void RequestHandler(Employee request)
=> request.Salary += 500;
}
private static void Main(string[] args)
{
var employees = new List<Employee>() {
new Employee(){ Name = "Razali", Position = "Software Engineer", Salary = 8000},
new Employee(){ Name = "Damian", Position = "UX Engineer", Salary = 6500},
new Employee(){ Name = "Zana", Position = "Manager", Salary = 13000},
};
IChainGroup<Employee> salaryChainGroup = new ChainGroup<Employee>()
.AddChainGroupLink(new EngineerSalaryChanger())
.AddChainGroupLink(new ManagerSalaryChanger());
employees.ForEach(employee => salaryChainGroup.Process(employee));
employees.ForEach(employee => Console.WriteLine($"Position: {employee.Position}, Salary: {employee.Salary}"));
}