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
- Host: GitHub
- URL: https://github.com/iharyakimush/specification
- Owner: IharYakimush
- License: mit
- Created: 2018-10-09T09:07:39.000Z (over 7 years ago)
- Default Branch: develop
- Last Pushed: 2018-11-27T11:42:03.000Z (about 7 years ago)
- Last Synced: 2025-03-01T00:37:46.906Z (11 months ago)
- Topics: expression-tree, filter, specification-pattern
- Language: C#
- Homepage:
- Size: 218 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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