Specification

The specification pattern implementation in RCommon.

Once again, Eric Evans and Martin Fowler have much to say on the topic of specifications so we'll leave most of the talking to them.

The specification pattern is implemented very simply in RCommon:

public class AllocationExistsSpec : Specification<LeaveAllocation>
{
    public AllocationExistsSpec(string userId, int leaveTypeId, int period) : 
        base(q => q.EmployeeId == userId
                && q.LeaveTypeId == leaveTypeId
                && q.Period == period)
    {
    }
}

Once your specification is built, it can be used with other specifications to simplify business logic in an object oriented, and reusable way. Specifications are also natively supported by RCommon repository interfaces:

var allocationCount = await _leaveAllocationRepository
    .GetCountAsync(new AllocationExistsSpec(emp.Id, leaveType.Id, period));

Last updated