Skip to content

Instantly share code, notes, and snippets.

@panesofglass
Created October 26, 2009 16:17
Show Gist options
  • Select an option

  • Save panesofglass/218767 to your computer and use it in GitHub Desktop.

Select an option

Save panesofglass/218767 to your computer and use it in GitHub Desktop.
A factory for creating specifications inline using Expressions.
using System;
using System.Linq.Expressions;
namespace Foundation.Specifications
{
/// <summary>
/// Defines a factory for creating specifications inline.
/// </summary>
public static class SpecificationFactory
{
#region methods
/// <summary>
/// Factory method to creates an <see cref="InlineSpecification{T}" /> specifaction using the
/// specified <paramref name="expression"/>.
/// </summary>
/// <typeparam name="T">The type of the entity used by the specification.</typeparam>
/// <param name="expression">The expression.</param>
/// <returns>The <see cref="Specification{T}" />.</returns>
public static ISpecification<T> Create<T>(Expression<Func<T, bool>> expression)
{
return new InlineSpecification<T>(expression);
}
#endregion
#region nested type: InlineSpecification
/// <summary>
/// Defines an internal implemenation for creating inline specifications.
/// </summary>
/// <typeparam name="T"></typeparam>
internal class InlineSpecification<T> : Specification<T>
{
private readonly Expression<Func<T, bool>> _condition;
/// <summary>
/// Gets the condition.
/// </summary>
/// <value>The condition.</value>
public override Expression<Func<T, bool>> Condition
{
get { return _condition; }
}
/// <summary>
/// Initializes a new instance of the <see cref="InlineSpecification&lt;T&gt;"/> class.
/// </summary>
/// <param name="condition">The condition.</param>
public InlineSpecification(Expression<Func<T, bool>> condition)
{
_condition = condition;
}
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment