Welcome to CuttingEdge.ServiceLayerHelpers

CuttingEdge.ServiceLayerHelpers library contains useful helper classes for Service Layer that are build on top of .NET 3.5 or higher.

The project currently contains two concepts: EntitySorter<TEntity> and EntityFilter<TEntity>.

EntitySorter<TEntity>

The EntitySorter<TEntity> allows the Presentation Layer to delegate sorting to the Service Layer. Service Layer methods can expose an IEntitySorter<TEntity> interface as method argument, as follows:

public static Person[] GetAllPersons(IEntitySorter<Person> sorter)
{
    using (var db = ContextFactory.CreateContext())
    {
        IOrderedQueryable<Person> sortedList = sorter.Sort(db.Persons);

        return sortedList.ToArray();
    }
}
This method can be called from the Presentation Layer by supplying an implementation of this interface:
IEntitySorter<Person> sorter =
    from person in EntitySorter<Person>.AsQueryable()
    orderby person.Name descending, person.Id
    select person;

var persons = PersonServices.GetAllPersons(sorter);
The following code shows other ways of creating IEntitySorter<T> implementations, all through the EntitySorter<T> facade:
// Using lambda expressions.
var sorter1 = EntitySorter<Person>.OrderBy(p => p.Id);
var sorter2 = EntitySorter<Person>.OrderByDescending(p => p.Id);
var sorter3 = EntitySorter<Person>
    .OrderBy(p => p.Name)
    .ThenBy(p => p.Id);

// Using strings
var sorter4 = EntitySorter<Person>.OrderBy("Name");
// Specifying chains of properties.
var sorter5 = EntitySorter<Person>.OrderBy("Address.City");

EntityFilter<TEntity>

The EntityFilter<TEntity> allows the Presentation Layer to delegate filtering to the Service Layer. Service Layer methods can expose an IEntityFilter<TEntity> interface as method argument, as follows:
public static Person[] GetAllPersons(IEntityFilter<Person> filter)
{
    using (var db = ContextFactory.CreateContext())
    {
        IQueryable<Person> filteredList = filter.Filter(db.Persons);

        return filteredList .ToArray();
    }
}
This method can be called from the Presentation Layer by supplying an implementation of this interface:
IEntityFilter<Person> filter =
    from person in EntityFilter<Person>.AsQueryable()
    where person.Name.StartsWith("a")
    where person.Id < 100
    select person;

var persons = PersonServices.GetAllPersons(filter);
The following code shows other ways of creating IEntityFilter<TEntity> implementations, all through the EntityFilter<TEntity> facade:
// Using lambda expressions.
var filter1 = EntityFilter<Person>.Where(p => p.Id < 10);
var filter2 = EntityFilter<Person>
    .Where(p => p.Id < 10)
    .Where(p => p.Name.StartsWith("a"));

Last edited Oct 25, 2009 at 2:54 PM by dot_NET_Junkie, version 3