strategy pattern

Nov 10, 2009 at 1:46 AM
Edited Nov 10, 2009 at 2:17 AM

NachaPaymentProcessor(ITaxStrategy taxcalculation) : IPaymentProcessor

ITaxStrategy - the concrete type to return is based on some context type say "State".

IPaymentProcessor - the concrete type to return is based on PaymentType enumeration.


lets assume we had a "Payment" available with properties: State, PaymentType.


here is how some PSEUDO code on how i would implement something like that:


var taxstrategylocator = CommonServiceLocator.Resolve<ITaxStrategyLocator>();
var processorstrategylocator = CommonServiceLocator.Resolve<IPaymentProcessorLocator>();

var processor = processorstrategylocator.GetBy(paymenttypecontext, strategylocator.GetBy(statecontext));

inside service locator for both of these we would have to "new" the concrete type.  that is one approach.

does unity have a policy system to enable strategy resolving?



Dec 18, 2009 at 12:56 AM

Not directly. You could hook in some custom container extensions to do this, but with the amount of code needed, you're probably better off writing a TaxStrategyFactory and injecting THAT from the container.