The Bitter Coder Tutorials, Binsor Style: Injecting Service Arrays
Mon, Oct 6, 2008 3-minute read
Previous posts in the series:here. We are going to rework the last post and remove the decorator pattern. Instead, we’ll create a calculator that manages any number of other calculators, which are injected as an array.
So, in keeping with my shameless plagirizing of Alex’s code, here is the abstract class for our new calculators:And now our total calculator: And the GST calculator: The shipping calculator: Lastly, our calculator of calculators, the reworked DefaultCalculator: So, we'll inject an array of calculators (total, shipping, and gst) into our default calculator. Binsor, away!
component "default.calculator", ICostCalculator,DefaultCalculatorNoDecorator: _calculators=[@total.calculatornodec,@shipping.calculatornodec,@gst.calculatornodec] component "total.calculatornodec", AbstractCalculator,TotalCalculator component "gst.calculatornodec", AbstractCalculator, GSTCostCalculatorNoDecorator: GstRate=Convert.ToDecimal(1.20) component "shipping.calculatornodec", AbstractCalculator, ShippingCalculatorNoDecorator: FragileShippingPremium=Convert.ToDecimal(0.0)Just like we did in our post on arrays, we inject an array of dependencies. Changing the order is just a matter of switching the order in the binsor. All good. Running the program gives the same output as the last post. Next post, which is the last in the series until Alex writes more (whew), is on the "Startable" facility.