Feature | Is supported |
---|---|
Null safe | |
Validation | |
EF Core | |
EF 6 | |
External Comparer |
Validation result
Error
"Object is not equal to [{Expected}]".
"Object is equal to [{Expected}]" - for negation.
Parameters
Parameter | Description |
---|---|
Expected | Expected object passed in constructor. |
Info
Checks if candidate object is equal to expected object.
Specification for comparison use (in order):
IEqualityComparer<T>
- if available.- == operator - if defined for T.
CompareTo()
method - if T implementsIComparable<T>
.CompareTo(Object)
method - if T implementsIComparable
.Equals()
method - if T implementsIEquatable<T>
.Equals(Object)
.
Usage
var spec = Specification.Equal("lorem ipsum");
spec.IsSatisfiedBy("lorem ipsum"); // true
spec.IsSatisfiedBy("ipsum"); // false
spec.IsSatisfiedBy(null); // false
As property
var customerSpec = Specification.Equal<Customer, string>(
c => c.Comments, null);
customerSpec.IsSatisfiedBy(new Customer { Comments = null }); // true
customerSpec.IsSatisfiedBy(new Customer { Comments = "VIP" }); // false
Not Equal
var spec = Specification.NotEqual("lorem ipsum");
spec.IsSatisfiedBy("lorem ipsum"); // false
spec.IsSatisfiedBy("ipsum"); // true
spec.IsSatisfiedBy(null); // true
As property
var customerSpec = Specification.NotEqual<Customer, string>(
c => c.Comments, null);
customerSpec.IsSatisfiedBy(new Customer { Comments = null }); // false
customerSpec.IsSatisfiedBy(new Customer { Comments = "VIP" }); // true
Equality Comparer
EqualSpecification<T>
supports IEqualityComparer<T>
.
var spec = Specification.Equal<double>(
0.0045, EqualityComparer<double>.Default);
EF 6 support
EqualSpecification<T>
works correctly in EF 6 solution, when T is primitive or enum type.
Non primitive types, may generate NotSupportedException
.