Coder Social home page Coder Social logo

jotatoledo / specimen Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 28 KB

Basic contract and abstract implementation of the specification pattern.

License: Other

C# 72.00% PowerShell 20.07% Shell 7.93%
csharp net-standard net-framework csharp-library specification-pattern

specimen's Introduction

Specimen

Build status NuGet

Basic contract and abstract implementation of the specification pattern.

Installation

Specimen can be installed using the NuGet command line or the NuGet Package Manager in Visual Studio.

PM> Install-Package Specimen

Example

First, you will need to add the following using statement:

using Specimen;

Creating specification classes

To create your own specification classes, there are 2 options:

  • Implement the ISpecification<T> interface:
public class PositiveIntegerSpecification : ISpecification<int> 
{
    public Expression<Func<int, bool>> Predicate => value => value > 0;
    
    public bool IsSatisfiedBy(int value)
    {
        // Ideally the compiled expression should be cached
        var compiled = this.Predicate.Compile();
        return compiled(value);
    }
}

public class GreaterThanSpecification : ISpecification<int> 
{
    private readonly int threshold int;
    
    public LargerThanSpecification(int threshold)
    {
        this.threshold = threshold;
    }

    public Expression<Func<int, bool>> Predicate => value => value > this.threshold;
    
    public bool IsSatisfiedBy(int value)
    {
        // Ideally the compiled expression should be cached
        var compiled = this.Predicate.Compile();
        return compiled(value);
    }
}

public class MultipleOfSpecification : ISpecification<int> 
{
    private readonly int baseNumber int;
    
    public MultipleOfSpecification(int baseNumber)
    {
        this.baseNumber = baseNumber;
    }

    public Expression<Func<int, bool>> Predicate => value => value % this.baseNumber == 0;
    
    public bool IsSatisfiedBy(int value)
    {
        // Ideally the compiled expression should be cached
        var compiled = this.Predicate.Compile();
        return compiled(value);
    }
}
  • Extend the SpecificationBase<T> abstract class:
public class PositiveIntegerSpecification : SpecificationBase<int> 
{
    public override Expression<Func<int, bool>> Predicate => value => value > 0;
}

public class GreaterThanSpecification : SpecificationBase<int> 
{
    private readonly int threshold int;
    
    public LargerThanSpecification(int threshold)
    {
        this.threshold = threshold;
    }

    public override Expression<Func<int, bool>> Predicate => value => value > this.threshold;
}

public class MultipleOfSpecification : ISpecification<int> 
{
    private readonly int baseNumber int;
    
    public MultipleOfSpecification(int baseNumber)
    {
        this.baseNumber = baseNumber;
    }

    public override Expression<Func<int, bool>> Predicate => value => value % this.baseNumber == 0;
}

The SpecificationBase<T> class already implements the ISpecification<T>#IsSatisfiedBy method in a cache-aware manner.

Logical operators

Logical operations like negation, conjunction (AND) and disjunction (OR) can be applied on ISpecification<T> instances as follows:

Negation

var positive = new PositiveIntegerSpecification();

var negativeOrZero = positive.Negate();

Assert(negativeOrZero.IsSatisfiedBy(-10));
Assert(negativeOrZero.IsSatisfiedBy(-5));
Assert(negativeOrZero.IsSatisfiedBy(0));
Assert(negativeOrZero.IsSatisfiedBy(5)); // Raises error

Conjunction

var positive = new PositiveIntegerSpecification();
var multipleOfTwo = new MultipleOfSpecification(2);
var largerThanFive = new GreaterThanSpecification(5);

var positiveMultipleOfTwoLargerThanFive = positive.And(multipleOfTwo, largerThanFive);

Assert(positiveMultipleOfTwoLargerThanFive.IsSatisfiedBy(-10)); // Raises error
Assert(positiveMultipleOfTwoLargerThanFive.IsSatisfiedBy(0)); // Raises error
Assert(positiveMultipleOfTwoLargerThanFive.IsSatisfiedBy(15)); // Raises error
Assert(positiveMultipleOfTwoLargerThanFive.IsSatisfiedBy(20));
Assert(positiveMultipleOfTwoLargerThanFive.IsSatisfiedBy(25)); // Raises error

Disjunction

var positive = new PositiveIntegerSpecification();
var multipleOfTwo = new MultipleOfSpecification(2);
var largerThanFive = new GreaterThanSpecification(5);

var positiveMultipleOfTwoLargerThanFive = positive.Or(multipleOfTwo, largerThanFive);

Assert(positiveMultipleOfTwoLargerThanFive.IsSatisfiedBy(-10)); // Raises error
Assert(positiveMultipleOfTwoLargerThanFive.IsSatisfiedBy(0)); // Raises error
Assert(positiveMultipleOfTwoLargerThanFive.IsSatisfiedBy(15));
Assert(positiveMultipleOfTwoLargerThanFive.IsSatisfiedBy(20));
Assert(positiveMultipleOfTwoLargerThanFive.IsSatisfiedBy(25));

Working with LINQ

Support for LINQ expressions/functions is provided by the library, allowing to use ISpecification<T> instances as inpuf of Where invocations:

var source = new []{-10, 5, 0, -5, 20 };
var positive = new PositiveIntegerSpecification();

var filtered = source.Where(positive); // Yields [5, 20]

Assert(positiveMultipleOfTwoLargerThanFive.Count() == 2);

Other projects

Check out some of my other C# projects:

specimen's People

Contributors

jotatoledo avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.