3

How Do You Implement Specification Pattern for querying database using NHibernate?(without LINQ to NHibernate).I read a lot about Specification Pattern but most of them was about Validation and Querying Memory collection objects.

Best method as far as I know using DetachedCriteria in Specification Interface like this.

interface ISpecification<T> {

 bool IsSatisfiedBy(T object);

 DetachedCriteria CreateCriteria();

}

Is there any alternative or better way to do this?

caltuntas
  • 10,747
  • 7
  • 34
  • 39
  • I was investigating a similar subject and found these posts interesting: [Specification Pattern Implementation](http://colinjack.blogspot.com/2007/06/specification-pattern-implementation.html) and [Extensible Query with Specification Pattern](http://hendryluk.wordpress.com/2009/03/23/extensible-query-with-specification-patterns/) – Marijn Jan 21 '11 at 09:48

2 Answers2

3

This is not nessary better, but can be an alternative

interface ISpecification<T> 
{
   bool IsSatisfiedBy(T object);

   Expression<Func<T, bool>> Predicate { get; }
}

Easy to use over linq (to nhibernate) and memory-collections.

RaYell
  • 69,610
  • 20
  • 126
  • 152
Paco
  • 8,335
  • 3
  • 30
  • 41
0

I implemented this using a simple extension method and specification pattern, works for System.Linq.IQueryable lists.

public interface IFilter<in T>
{
    bool MatchFilter(T o);
}

public static class FilterExtension
{
    public static IQueryable<T> Filter<T>(this IQueryable<T> query, IFilter<T> filter)
    {
        return query.Where(x => filter.MatchFilter(x));
    }
}

Simple example classes and IFilter implementation:

public class Organization
{
    public string Name { get; set; }
    public string Code { get; set; }
    public Address Address { get; set; }


    public Organization(string name, string code, string city, string country)
    {
        Name = name;
        Code = code;
        Address = new Address(city, country);
    }

}

public class Address
{
    public Address(string city, string country)
    {
        City = city;
        Country = country;
    }

    public string City { get; set; }
    public string Country { get; set; }
}

public class GenericOrganizationFilter : IFilter<Organization>
{
    public string FilterString { get; set; }

    public GenericOrganizationFilter(string filterString)
    {
        FilterString = filterString;
    }

    public bool MatchFilter(Organization o)
    {
        return
            (o.Name != null && o.Name.Contains(FilterString)) ||
            (o.Code != null && o.Code.Contains(FilterString)) ||
            (o.Address != null && o.Address.City != null && o.Address.City.Contains(FilterString)) || 
            (o.Address != null && o.Address.Country != null && o.Address.Country.Contains(FilterString));
    }
}

Usage:

IFilter<Organization> filter = new GenericOrganizationFilter("search string");
//Assuming queryable is an instance of IQueryable<Organization>. 
IQueryable<Organization> filtered = queryable.Filter(filter);