21. ManagerPresenterTests [TestMethod] public void CanAddEmployee() { var view = new ManagerViewFake(); var presenter = new ManagerPresenter(view); var manager = Persist.Manager(); view.SelectedManager = manager; view.Add(); Assert.AreEqual(1, view.Employees.Count); }
35. Тестируем код [TestMethod] public void CanConvertMoney() { var service = new ExchangeRatesServiceFake() .UsdCosts(1, "USD") .UsdCosts(29M, "RUR"); var money = new Money(5, "RUR"); Assert.AreEqual( new Money(5/29M, "USD"), money.Convert("USD", service)); }
45. DSL new DateTime(2009, 11, 12) 12.11.of2009(); public static DateTime of2009(this double ddMM) { var day = (int) Math.Round(ddMM, 0); var month = (int) Math.Round(ddMM*100, 0) - day * 100; return new DateTime(2009, month, day); }
46. DSL var month = new CalendarMonth(2009, 10); var month = Oct.of2009(); internal static class Oct { public static CalendarMonth of2009 { get { return new CalendarMonth(2009, 10); } } }
47. DSL var customer = new Customer(); customer.Name = “VasyaPupkin”; var order = new Order(); order.Customer = customer; order.Date = new DateTime(2009, 10, 11); varorderItem = new orderItem(); orderItem.Product = new Product(“Chips”); orderItem.Amount = 3; orderItem.Price = 45.30; order.Items.Add(orderItem); orderItem.Product = new Product(“Beer”); orderItem.Amount = 3; orderItam.Price = 20.50; order.Items.Add(orderItem);
48. DSL var order = new OrderBuilder() .WithCustomer(“VasyaPupkin”) .WithDate(11.10.of2009()) .WithLineItem() .WithProduct(“Chips”) .WithAmount(3) .WithPrice(45.30) .WithLineItem() .WithProduct(“Beer”) .WithAmount(3) .WithPrice(20.50) .GetResult();
49. GoF / PoEAAGang of Four / Patterns of Enterprise Application Architecture
52. Спецификация // if order is critical if (order.Amount >= 1000000) { … }
53. Спецификация public class CriticalOrder { public boolIsSatisfiedBy(Order order) { return order.Amount >= 1000000; } }
54. Спецификация public class CriticalOrder { private readonlyintminimalAmount; public CriticalOrder(intminimalAmount) { this.minimalAmount = minimalAmount; } public boolIsSatisfiedBy(Order order) { return order.Customer.Type == CustomerType.KeyCustomer && order.Amount >= minimalAmount; } }
55. Композитные спецификации var specification = new CriticalOrder( CustomerType.KeyCustomer, 200000) & new SoldTo(new Country("Russia")) & new AsOf(2009);
56. Что дают шаблоны Повторное использование Расширяемость Гибкость SOLID