This project demonstrates an advanced implementation of the Specification Pattern in C#, showcasing both a basic implementation and an enhanced version with enterprise-grade features.
The Specification Pattern is a behavioral design pattern that allows you to define business rules or criteria that can be combined using boolean operators (AND, OR, NOT) to create complex business logic. This implementation provides both a simple version and an advanced version with additional features.
- Core interfaces and base classes
- AND, OR, and NOT composition
- Expression-based specifications
- Extension methods for fluent API
- Performance Optimizations: Parameter rebinding and expression caching
- Asynchronous Evaluation: Non-blocking specification evaluation
- Specification Builder: Fluent API for creating complex specifications
- Specification Visitor: Pattern for analyzing and processing specifications
- Validation Framework: Built-in validation with error collection
- Caching Mechanism: Performance improvement through result caching
- Operation Analysis: Count and analyze operations in complex specifications
ISpecification<T>: Base interface for all specifications
SpecificationBase<T>: Abstract base class for specificationsAndSpecification<T>: Combines two specifications with AND logicOrSpecification<T>: Combines two specifications with OR logicNotSpecification<T>: Negates a specification
SpecificationBase<T>: Enhanced base class with caching and async supportAndSpecification<T>: Optimized AND implementation with parameter rebindingOrSpecification<T>: Optimized OR implementation with parameter rebindingNotSpecification<T>: NOT implementationCachedSpecification<T>: Wrapper for caching specification resultsSpecificationBuilder<T>: Fluent API for building complex specificationsSpecificationVisitor: Base class for visiting and analyzing specificationsOperationCounterVisitor: Example visitor that counts operationsValidationSpecification<T>: Specification with validation capabilitiesCompositeValidationSpecification<T>: Validates multiple specifications
var activeAccountSpec = new ActiveAccountSpecification();
var highBalanceSpec = new AccountAmountSpecification(1000);
var compositeSpec = activeAccountSpec.And(highBalanceSpec);
if (compositeSpec.IsSatisfiedBy(account))
{
// Account is active and has high balance
}// Using the specification builder
var spec = SpecificationBuilder<Account>
.Create(a => a.IsActive)
.And(a => a.Amount > 1000)
.Build();
// Async evaluation
var result = await spec.IsSatisfiedByAsync(account);
// Operation analysis
var visitor = new OperationCounterVisitor();
visitor.Visit(spec);
Console.WriteLine($"AND operations: {visitor.AndCount}");To run the demonstration:
dotnet runThis will show examples of both the basic and advanced specification patterns in action.
To run the unit tests:
dotnet test- Performance: Optimized expression building and caching mechanisms
- Flexibility: Rich API for composing complex business rules
- Maintainability: Clean separation of concerns and reusable components
- Extensibility: Easy to add new operations and analysis tools
- Enterprise Ready: Includes validation, async support, and diagnostic capabilities
This project is open source and available under the MIT License.