public void PoorMansDiTest() { IOrderProcessor orderProcessor = new OrderProcessor( new TaxStrategyFactory( new ITaxStrategy[] {new CanadaTaxStrategy(), new USTaxStrategy()})); var order = new Order { Country = "US" }; orderProcessor.Process(order); Assert.AreEqual(10, order.Tax); }
public void InjectUsingCollectionResolver() { var container = new WindsorContainer(); container.Kernel.Resolver.AddSubResolver(new CollectionResolver(container.Kernel)); container.Register(Component.For<ITaxStrategy>().ImplementedBy<USTaxStrategy>()); container.Register(Component.For<ITaxStrategy>().ImplementedBy<CanadaTaxStrategy>()); container.Register(Component.For<IOrderProcessor>().ImplementedBy<OrderProcessor>()); container.Register(Component.For<ITaxStrategyFactory>().ImplementedBy<TaxStrategyFactory>()); var order = new Order { Country = "US" }; container.Resolve<IOrderProcessor>().Process(order); Assert.AreEqual(10, order.Tax); }
public void InjectWithFactoryMethod() { var container = new WindsorContainer(); container.Register(Component.For<ITaxStrategy>().ImplementedBy<USTaxStrategy>().Named("USTaxStrategy")); container.Register(Component.For<ITaxStrategy>().ImplementedBy<CanadaTaxStrategy>().Named("CanadaTaxStrategy")); container.Register(Component.For<IOrderProcessor>().ImplementedBy<OrderProcessor>()); container.Register(Component.For<ITaxStrategyFactory>() .UsingFactoryMethod( kernel => new TaxStrategyFactory(new ITaxStrategy[] { kernel.Resolve<ITaxStrategy>("USTaxStrategy"), kernel.Resolve<ITaxStrategy>("CanadaTaxStrategy"), }))); var order = new Order {Country = "US"}; container.Resolve<IOrderProcessor>().Process(order); Assert.AreEqual(10, order.Tax); }
public void InjectWithSpecificRegistration() { var container = new WindsorContainer(); container.Register(Component.For<ITaxStrategy>().ImplementedBy<USTaxStrategy>()); container.Register(Component.For<ITaxStrategy>().ImplementedBy<CanadaTaxStrategy>()); container.Register(Component.For<IOrderProcessor>().ImplementedBy<OrderProcessor>()); container.Register( Component.For<ITaxStrategyFactory>() .ImplementedBy<TaxStrategyFactory>() .DependsOn(Dependency.OnValue<ITaxStrategy[]>(container.ResolveAll<ITaxStrategy>()))); var order = new Order { Country = "US" }; container.Resolve<IOrderProcessor>().Process(order); Assert.AreEqual(10, order.Tax); var order2 = new Order { Country = "CA" }; container.Resolve<IOrderProcessor>().Process(order2); Assert.AreEqual(5, order2.Tax); }
public void Process(Order order) { var strategy = strategyFactory.GetTaxStrategy(order); order.Tax = strategy.CalcTax(order); }
public bool IsApplicable(Order order) { return order.Country == "CA"; }
public decimal CalcTax(Order order) { return 5; }
public ITaxStrategy GetTaxStrategy(Order o) { try { return _taxStrategies.First(taxStrategy => taxStrategy.IsApplicable(o)); } catch (Exception ex) { throw new Exception("No suitable tax strategy was found.", ex); } }
public virtual void Process(Order order) { order.Tax = Strategy.CalcTax(order); }