First, I’d like to say that I wrote this article because of the difficulty I faced when I tried to research the specification pattern. I saw a lot of confusing videos and articles online. I was eventually able to get the hang of it so I decided to take the time to document it to help others. Moreover, I’ve gained a lot from C# Corner; now, it’s my turn to give back.
This lesson has been divided into two parts. In this part, I will introduce you to the whole idea of the specification pattern, and in part two I will talk about composite specifications.
Okay, what’s the specification pattern about? To answer this question, it’s important to note that every design pattern seeks to solve a problem developers encounter frequently, as well as ensure “clean coding”. The specification pattern helps to ensure that the ‘O’ in SOLID principles is adhered to, i.e. software entities should be open to expansion and closed to modification.
Take a look at the code listing below,
- public class GetEmployee
- {
- public Employee[] GetEmployeesByName(string name)
- {
- }
-
- public Employee[] GetEmployeesByDepartment(string department)
- {
- }
-
- public Employee[] GetEmployeesByID(string ID)
- {
- }
-
- public Employee[] GetEmployeesByYear(string ID)
- {
- }
- }
Of course, we’re assuming that an Employee class has been created somewhere. In the above example, the methods in the GetEmployee class are used to filter employees based on certain criteria (Name, department etc). This is an inefficient way to handle the scenario as it directly violates the ‘O’ of SOLID principles. Implementing it this way means that if we need to create a new filter criterion tomorrow, we’d have to tamper with the GetEmployee class by adding another method.
The code listing below shows how elegantly the specification pattern handles the same scenario.
- public class Employee
- {
- public string EmployeeID { get; set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public string Department { get; set; }
- private int _yearOfResumption;
- public int YearOfResumption
- {
- get { return _yearOfResumption; }
- set
- {
- if (value > DateTime.Now.Year)
- {
- _yearOfResumption= DateTime.Now.Year;
- }
- else { _yearOfResumption = value; }
- }
- }
- }
-
- public interface IEmployeeSpecification
- {
- bool IsSatisfiedBy(Employee employee);
- }
-
- public class EmployeeDepartmentSpecification : IEmployeeSpecification
- {
- private readonly string _department;
-
- public EmployeeDepartmentSpecification(string depatrment)
- {
- _department = depatrment;
- }
-
- public bool IsSatisfiedBy(Employee employee)
- {
- return employee.Department.Equals(_department);
- }
- }
-
- public class EmployeeYearSpecification : IEmployeeSpecification
- {
- private readonly int _year;
-
- public EmployeeYearSpecification(int year)
- {
- _year = year;
- }
- public bool IsSatisfiedBy(Employee employee)
- {
- return employee.YearOfResumption.Equals(_year);
- }
- }
-
- public class GetEmployee
- {
- public static List<Employee> GetEmployeeBy(IEmployeeSpecification specification, Employee[] employees)
- {
- List<Employee> NeededEmployees = new List<Employee>();
-
-
- foreach (Employee employee in employees)
- {
- if (specification.IsSatisfiedBy(employee))
- {
- NeededEmployees.Add(employee);
- }
- }
- return NeededEmployees;
- }
- }
Using this method, we simply define a boolean IsSatisfied method in an IEmployeeSpecification interface (which serves as a parameter for our GetEmploeeBy method in GetEmployee class) and have all our specifications implement that interface. That way, if we need to add a new specification in the future, we simply define it and have it implement the IEmployeeSpecification interface.
Below is a sample Program.cs file you can use to test the code,
- class Program
- {
- static void Main(string[] args)
- {
- Employee employee1 = new Employee { FirstName="Fidel",Department="Maths", YearOfResumption = 2017};
- Employee employee2 = new Employee { FirstName = "Francis", Department = "Software", YearOfResumption = 2018 };
- Employee employee3 = new Employee { FirstName = "Ahmed",Department = "Maths", YearOfResumption = 2018 };
- Employee employee4 = new Employee { FirstName = "Ebuka",Department = "Software", YearOfResumption = 2017 };
-
- Employee[] employees = new Employee[] { employee1,employee2,employee3,employee4};
-
- Console.WriteLine("Software Department");
- List<Employee> SoftwareEmployees = GetEmployee.GetEmployeeBy(new EmployeeDepartmentSpecification("Software"), employees);
- foreach (var employee in SoftwareEmployees)
- {
- Console.WriteLine(employee.FirstName);
- }
-
- Console.WriteLine();
-
- Console.WriteLine("Employed in 2017");
- List<Employee> EmployedIn2017 = GetEmployee.GetEmployeeBy(new EmployeeYearSpecification(2017), employees);
- foreach (var employee in EmployedIn2017)
- {
- Console.WriteLine(employee.FirstName);
- }
-
- Console.ReadKey();
- }
- }
The picture below shows the expected output.
I hope this article has been helpful. You can also read part two of this publication where I discussed composite specifications. Follow me on C# Corner to know when the article is posted. Kindly drop feedback and comments in the Comments section below.