Specification Pattern with C#

Reading Time: 6 minutes

What is specification pattern?

Specification pattern is a software designing pattern where business logic is maintained in single unit which can then be used to either validate business logic against domain model or can be used to chain business rules together using Boolean logic operands like AND,OR,NOT to retrieve expected results.

In this article, we will discuss about specification pattern. We will cover brief overview of specification pattern, its use in domain-driven development and its usefulness to keep DRY (Don’t Repeat Yourself) principle intact while implementing business logic. We will also discuss implementation of specification pattern together with repository pattern and c#’s generics to obtain generic abstract specification which will provide base of pattern.

Example

Let’s consider famous Employee as an entity with following property.

 

Now suppose there is need to get the detail of Employees which are team leaders, here we will create EmployeeRepository which interacts with Employee data source.

So, we will create EmployeeRepository.cs along with IEmployeeRepository.cs

OK, this looks simple. It could be complicated as more and more requirement increases.it creates problem when there is need to validate the logic as well as need to get list of employees from the repository method.

For example if at some point of time there is a need to check that Employee’s Designation is Team leader. So controller method can be written as

In above code there is problem, as logic of deciding team leader is spread across two parts of application, one in GetTeamMembers method in controller and other in GetTeamLeaders method of repository. Which violates DRY principle and increases overhead to maintain code.

It becomes severe as size of code increases. So, to solve this problem specification pattern helps in keeping business logic in one single piece of unit.

So let’s add specification for Employee.

We will create specification for employee to decide team leader,

In above we have created class which contains specification to get expression which gives list of team leader and IsSatisfiedBy method provide way to validate the logic in controller.

Now we can use this specification to validate team leader criteria as follows,

And repository method can be refactored as,

Ok, so we have removed redundancy to decide team leader. But what if we also need to create specification for Company entity?

This is where Generics helps to provide specification for other entity.

Let’s create generic specification class.

we have created this class as abstract, it make sense as it has method defined without implementation, that method will be overridden by the other specifications and they will decide how to implement ToExpression method. So let’s inherit this class to TeamLeaderSpecification class first.

Now TeamLeaderSpecification will looks like below,

Notice that ToExpression is overridden here and IsSatisfiedBy method is removed from TeamLeaderSpecification, actually it is moved to Specification class.

Again we are ready to create CompanySpecification as per company requirements and move on to expand Specification pattern. Like if we want to get the specification to get the company based on Id we would create,

Here we have created specification using constructor call as id would be dynamic, so whenever we need to create specification we will created specification using constructor initialization.

So we are ready to use specification for different entities.

However we can also chain the specifications Like AND, OR, NOT

 

For example if we want to combine two specifications like we want to have employees with name Mark and Also he should be team leader then we can create AND specification in abstract class,

We can now create Specification to generate query to get team leaders by name as follows,

 

We have now two methods to get the list of employees,

In both of the above methods we are creating Specifications in Repository, we can combine two methods in one to get the list of employees and give the responsibility of creating business rules to controller and give single responsibility to get employee list to Repository. We can do it by simply creating method in employee repository which takes specification of employees and give list of employees back,

that’s it, we can now call this method from controller and force the controller to create business rule.

Conclusion

  • Specification pattern helps to implement DRY principle along with repository pattern and Generics.
  • It also helps in implementing SRP (Single Responsibility Principle) and writing loosely coupled code.
  • Specification pattern also helps to maintain entity related business logic in single unit of code.

 

Software developer driven by passion and love of coding.Lucky enough to have colleagues with great skills and i am able to have hands-on experience with C#.Net, Entity-Framework and AngularJs with the help of them.