Skip to content

Instantly share code, notes, and snippets.

@jfromaniello
Created May 3, 2011 17:06
Show Gist options
  • Select an option

  • Save jfromaniello/953725 to your computer and use it in GitHub Desktop.

Select an option

Save jfromaniello/953725 to your computer and use it in GitHub Desktop.
This is with the specification pattern
public class CancelOrderService
{
private IDao<Order> daoOrder;
public CancelOrderService(IDao<Order> daoOrder)
{
this.daoOrder = daoOrder;
}
public void CancelLastWeekOrders()
{
//note that in this case the query is part of this code
var orders = daoOrder.Retrieve(new LastWeekSpecification());
orders.ForEach(o => o.Cancel());
}
}
public interface IDao<T>
{
IEnumerable<T> Retrieve(Specification<T> predicate);
}
public class LastWeekSpecification : Specification<Country>
{
public override Expression<Func<Order, bool>> IsSatisfiedBy()
{
return c => c.Date < firstDayOfCurrentWeek;
}
}
[Test]
public void CancelAllOrdersFromLastWeek_ShouldWork()
{
var order1 = new Order();
var handler = new CancelOrderService(Moc.Of<IDao<Order>>(dao => dao.Retrieve(It.IsAny<LastWeekSpecification>()) == new[]{order});
handler.CancelAllOrdersFromLastWeek();
order1.Status.Should().Be.EqualTo(OrderStatus.Canceled);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment