Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Text;
- namespace Test
- {
- class Program
- {
- static void Main(string[] args)
- {
- var spec1 = new AdHocSpecification<string>(s => s.StartsWith("a"));
- var spec2 = new AdHocSpecification<string>(s => s.EndsWith("a"));
- bool resultado = (spec1 & spec2).Spec().Compile()("adsb");
- Console.WriteLine(resultado);
- bool resultado2 = (spec1 | spec2).Spec().Compile()("adsb");
- Console.WriteLine(resultado2);
- bool resultado3 = (!spec1 & spec2).Spec().Compile()("bcdefa");
- Console.WriteLine(resultado3);
- Console.ReadLine();
- }
- }
- public abstract class Specification<T>
- {
- public abstract Expression<Func<T, bool>> Spec();
- public static Specification<T> operator &(Specification<T> spec1, Specification<T> spec2)
- {
- return new AndSpecification<T>(spec1.Spec(), spec2.Spec());
- }
- public static Specification<T> operator |(Specification<T> spec1, Specification<T> spec2)
- {
- return new OrSpecification<T>(spec1.Spec(), spec2.Spec());
- }
- public static Specification<T> operator !(Specification<T> spec1)
- {
- return new NegateSpecification<T>(spec1.Spec());
- }
- }
- public class AdHocSpecification<T> :Specification<T>
- {
- private readonly Expression<Func<T, bool>> _specification;
- public AdHocSpecification(Expression<Func<T, bool>> specification)
- {
- _specification = specification;
- }
- public override Expression<Func<T, bool>> Spec()
- {
- return _specification;
- }
- }
- public class AndSpecification<T> : Specification<T>
- {
- private readonly Expression<Func<T, bool>> _spec1;
- private readonly Expression<Func<T, bool>> _spec2;
- public AndSpecification(Expression<Func<T, bool>> spec1, Expression<Func<T, bool>> spec2)
- {
- _spec1 = spec1;
- _spec2 = spec2;
- }
- public override Expression<Func<T, bool>> Spec()
- {
- ParameterExpression param = Expression.Parameter(typeof(T),"x");
- return Expression.Lambda<Func<T, bool>>(
- Expression.AndAlso(
- Expression.Invoke(_spec1, param),
- Expression.Invoke(_spec2, param)), param);
- }
- }
- public class OrSpecification<T> : Specification<T>
- {
- private readonly Expression<Func<T, bool>> _spec1;
- private readonly Expression<Func<T, bool>> _spec2;
- public OrSpecification(Expression<Func<T, bool>> spec1, Expression<Func<T, bool>> spec2)
- {
- _spec1 = spec1;
- _spec2 = spec2;
- }
- public override Expression<Func<T, bool>> Spec()
- {
- ParameterExpression param = Expression.Parameter(typeof(T), "x");
- return Expression.Lambda<Func<T, bool>>(
- Expression.OrElse(
- Expression.Invoke(_spec1, param),
- Expression.Invoke(_spec2, param)), param);
- }
- }
- public class NegateSpecification<T> : Specification<T>
- {
- private readonly Expression<Func<T, bool>> _expression;
- public NegateSpecification(Expression<Func<T, bool>> expression)
- {
- _expression = expression;
- }
- public override Expression<Func<T, bool>> Spec()
- {
- ParameterExpression param = Expression.Parameter(typeof(T), "x");
- return Expression.Lambda<Func<T, bool>>(Expression.Not(Expression.Invoke(_expression, param)), param);
- }
- }
- }
Add Comment
Please, Sign In to add comment