Created
May 3, 2011 17:06
-
-
Save jfromaniello/953725 to your computer and use it in GitHub Desktop.
This is with the specification pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public interface IDao<T> | |
| { | |
| IEnumerable<T> Retrieve(Specification<T> predicate); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class LastWeekSpecification : Specification<Country> | |
| { | |
| public override Expression<Func<Order, bool>> IsSatisfiedBy() | |
| { | |
| return c => c.Date < firstDayOfCurrentWeek; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [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