An open API service indexing awesome lists of open source software.

https://github.com/iharyakimush/specification

Specification pattern implementation for filtering objects
https://github.com/iharyakimush/specification

expression-tree filter specification-pattern

Last synced: 7 months ago
JSON representation

Specification pattern implementation for filtering objects

Awesome Lists containing this project

README

          

# Specification
Filter data using specification expressions tree with following advantages:
- ability to load specification from xml. json format support comming soon.
- concept of "specification reference" and "value reference" to reuse already defined expressions in more complex ones.
- comparison between single and multiple values
- base visitors for efficien work with specification expressions tree

## Getting started
Define specification expression
```
Specification specification = new AndSpecification(
new EqualSpecification("key1", SpecificationValue.Single("value1")),
new OrSpecification(
new HasValueSpecification("key2"),
new EqualSpecification("key5", SpecificationValue.Single(new DateTime(2018, 10, 10, 1, 2, 3))),

// value of key3 should be equal to any of values 1,2,3
new EqualSpecification("key3", SpecificationValue.AnyOf(1, 2, 3))),

// key4 should be equal to value with name "currentDateTime" which will be resolved at specification evaluation
new EqualSpecification("key4", SpecificationValue.Ref("currentDateTime")));
```
Evaluate specification expression
```
Dictionary values = new Dictionary
{
{ "key1", "value1" },
{ "key3", 1 },
{ "key4", DateTime.Now.AddMinutes(1) },

// value referenced from specification
{ "currentDateTime", DateTime.UtcNow }
};

SpecificationResult result = specification.Evaluate(values);
Assert.True(result.IsSatisfied);
```
Serialize specification expression to xml `string xml = specification.ToXml();`. DateTime values will be converted to UTC for serialization and comparison.
```






1
2
3


```
Parse specification from xml `Specification sp2 = Specification.Parse.FromXml(XElement.Parse(xml));`. Parse operation assume DateTime to be UTC values.

## NuGet
https://www.nuget.org/packages/Specification.Expressions

## Contribution
Please feel free to create issues and pool requests to develop branch