Skip to content

Instantly share code, notes, and snippets.

@panesofglass
Created October 26, 2009 16:16
Show Gist options
  • Save panesofglass/218766 to your computer and use it in GitHub Desktop.
Save panesofglass/218766 to your computer and use it in GitHub Desktop.
A generic specification base class.
using System;
using System.Linq.Expressions;
namespace Foundation.Specifications
{
/// <summary>
/// Defines an abstract class from which to create conditional specifications.
/// </summary>
/// <typeparam name="T">The type of entity used in the specification.</typeparam>
/// <seealso href="http://ubik.com.au/article/named/implementing_the_specification_pattern_with_linq" />
/// <seealso href="http://www.lostechies.com/blogs/chrismissal/archive/2009/09/10/using-the-specification-pattern-for-querying.aspx" />
public abstract class Specification<T> : ISpecification<T>
{
#region fields
private Func<T, bool> _func;
#endregion
#region properties
/// <summary>
/// Gets the condition expression.
/// </summary>
/// <value>The <see cref="Expression{TDelegate}" />.</value>
public abstract Expression<Func<T, bool>> Condition { get; }
/// <summary>
/// Gets the is satisfied by.
/// </summary>
/// <value>The <see cref="Func{T,TResult}" />.</value>
public virtual bool IsSatisfiedBy(T candidate)
{
if (_func == null)
_func = Condition.Compile();
return _func(candidate);
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment