Skip to content

Latest commit

 

History

History
67 lines (48 loc) · 1.7 KB

File metadata and controls

67 lines (48 loc) · 1.7 KB

Shuttle.Core.Specification

Provides a simple implementation of the specification pattern.

Installation

dotnet add package Shuttle.Core.Specification

Synchronous Specification

ISpecification

A custom specification can be implemented by inheriting from the ISpecification<T> interface:

public class MySpecification : ISpecification<MyCandidate>
{
    public bool IsSatisfiedBy(MyCandidate candidate)
    {
        return candidate.IsActive && !candidate.IsDeleted;
    }
}

Specification

A default Specification<T> wrapper class is available that accepts a function as a callback for simple scenarios where an explicit ISpecification<T> implementation may not be warranted:

var specification = new Specification<MyCandidate>(candidate => candidate.IsActive && !candidate.IsDeleted);

if (specification.IsSatisfiedBy(myCandidate))
{
    // ...
}

Asynchronous Specification

IAsyncSpecification

An asynchronous specification can be implemented by inheriting from the IAsyncSpecification<T> interface:

public class MyAsyncSpecification : IAsyncSpecification<MyCandidate>
{
    public async Task<bool> IsSatisfiedByAsync(MyCandidate candidate)
    {
        return await _myService.IsActiveAsync(candidate.Id);
    }
}

AsyncSpecification

A default AsyncSpecification<T> wrapper class is available that accepts a function as a callback for simple scenarios where an explicit IAsyncSpecification<T> implementation may not be warranted:

var specification = new AsyncSpecification<MyCandidate>(async candidate => await _myService.IsActiveAsync(candidate.Id));

if (await specification.IsSatisfiedByAsync(myCandidate))
{
    // ...
}