Specification.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System;
  2. using System.Linq.Expressions;
  3. namespace Abp.Specifications
  4. {
  5. /// <summary>
  6. /// Represents the base class for specifications.
  7. /// </summary>
  8. /// <typeparam name="T">The type of the object to which the specification is applied.</typeparam>
  9. public abstract class Specification<T> : ISpecification<T>
  10. {
  11. /// <summary>
  12. /// Returns a <see cref="bool"/> value which indicates whether the specification
  13. /// is satisfied by the given object.
  14. /// </summary>
  15. /// <param name="obj">The object to which the specification is applied.</param>
  16. /// <returns>True if the specification is satisfied, otherwise false.</returns>
  17. public virtual bool IsSatisfiedBy(T obj)
  18. {
  19. return ToExpression().Compile()(obj);
  20. }
  21. /// <summary>
  22. /// Gets the LINQ expression which represents the current specification.
  23. /// </summary>
  24. /// <returns>The LINQ expression.</returns>
  25. public abstract Expression<Func<T, bool>> ToExpression();
  26. /// <summary>
  27. /// Implicitly converts a specification to expression.
  28. /// </summary>
  29. /// <param name="specification"></param>
  30. public static implicit operator Expression<Func<T, bool>>(Specification<T> specification)
  31. {
  32. return specification.ToExpression();
  33. }
  34. }
  35. }