# Part 13 - Injecting Service Arrays So this time we're going to look at injecting service arrays ... if you haven't read part 12, it might pay to have a look at that first - this part follows on from the examples presented there.

So this should be reasonably quick, we're just going to rework the Part 12 solution so we don't need decorators... why would we do this, well in this case I feel that rewiring decorators (ie. changing orders, or disabling one of the decorators temporarily) isn't completely intuitive, especially when you have a long chain, it's easy to make mistakes.

So what's the plan, well first off let's implement an abstract class for our calculators:

```public abstract class AbstractCalculator
{
public abstract decimal Calculate(decimal currentTotal, Order order);
}
```

Now we can rework the existing decorators and default calculator to be implementations of the abstract caculator, so here goes:

First off the the logic in the DefaultCostCalculator will be moved into a class called TotalCalculator :

```public class TotalCalculator : AbstractCalculator
{
private decimal CalculateTotal(Order order)
{
decimal total = 0;
foreach (OrderItem item in order.Items)
{
total += (item.Quantity*item.CostPerItem);
}
}
public override decimal Calculate(decimal currentTotal, Order order)
{
return currentTotal + CalculateTotal(order);
}
}
```

And then for the gst calculator, we do much the same thing:

```public class GstCalculator : AbstractCalculator
{
private decimal _gstRate = 1.125m;
public decimal GstRate
{
get { return _gstRate; }
set { _gstRate = value; }
}
private bool IsNewZealand(Order order)
{
return (order.CountryCode == "NZ");
}
public override decimal Calculate(decimal currentTotal, Order order)
{
if (IsNewZealand(order))
{
return (currentTotal*_gstRate);
}
return currentTotal;
}
}
```

And finally the shipping calculator:

```public class ShippingCalculator : AbstractCalculator
{
private decimal _shippingCost = 5.0m;
public decimal ShippingCost
{
get { return _shippingCost; }
set { _shippingCost = value; }
}
{
set { _fragileShippingPremium = value; }
}
private decimal GetShippingTotal(Order order)
{
decimal shippingTotal = 0;
foreach (OrderItem item in order.Items)
{
decimal itemShippingCost = ShippingCost*item.Quantity;
shippingTotal += itemShippingCost;
}
return shippingTotal;
}
public override decimal Calculate(decimal currentTotal, Order order)
{
return currentTotal + GetShippingTotal(order);
}
}
```

Now, our original DefaultCostCalculator is reworked to use an array of AbstractCalculators to compute the total cost for an order:

```public class DefaultCostCalculator : ICostCalculator
{
private AbstractCalculator[] _calculators;
public DefaultCostCalculator(AbstractCalculator[] calculators)
{
_calculators = calculators;
}
public decimal CalculateTotal(Order order)
{
decimal currentTotal = 0;
foreach (AbstractCalculator calculator in _calculators)
{
currentTotal = calculator.Calculate(currentTotal, order);
}
return currentTotal;
}
}
```

Notice it takes an array of AbstractCalculator's as a constructor dependency... just like we did for configuration parameters in the early parts of this container tutorial series, we can actually pass arrays of dependencies to a component as well, so last of all let's see the container configuration:

```

type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />

service="IoC.Tutorials.Part13.AbstractCalculator, IoC.Tutorials.Part13"
type="IoC.Tutorials.Part13.GstCalculator, IoC.Tutorials.Part13">

1.20

service="IoC.Tutorials.Part13.AbstractCalculator, IoC.Tutorials.Part13"
type="IoC.Tutorials.Part13.ShippingCalculator, IoC.Tutorials.Part13">

0.0

service="IoC.Tutorials.Part13.AbstractCalculator, IoC.Tutorials.Part13"
type="IoC.Tutorials.Part13.TotalCalculator, IoC.Tutorials.Part13" />

service="IoC.Tutorials.Part13.ICostCalculator, IoC.Tutorials.Part13"
type="IoC.Tutorials.Part13.DefaultCostCalculator, IoC.Tutorials.Part13">

\${calc.total}
\${calc.shipping}
\${calc.gst}

```

Notice the default caculator now takes our 3 calculators as members of an array, and if we want to change the order in which they are evaluated, it's trivial to just move them around within the node, or to comment certain ones out.

And just to demonstrate that nothing has changed, the code for the program in part 13 is identical to part 12:

```private static void Main(string[] args)
{
WindsorContainer container = new WindsorContainer(new XmlInterpreter());
Order order1 = new Order();
order1.CountryCode = "NZ";
Order order2 = new Order();
order2.CountryCode = "US";
ICostCalculator costCalculator = container.Resolve();
Console.WriteLine("Cost to deliver Order 1: {0}", costCalculator.CalculateTotal(order1));
Console.WriteLine("Cost to deliver Order 2: {0}", costCalculator.CalculateTotal(order2));
}
```

And the results are the same too:

Cost to deliver Order 1: 192.0000

Cost to deliver Order 2: 260.0

Beautiful :)

The next part will have a quick look at the "Startable" facility, enjoy.
Written on April 28, 2007
{"ft_lang_label":"__label__en","ft_lang_prob":0.6740283,"math_prob":0.95789784,"size":5313,"snap":"2023-40-2023-50","text_gpt3_token_len":1230,"char_repetition_ratio":0.19363345,"word_repetition_ratio":0.05757576,"special_character_ratio":0.22830792,"punctuation_ratio":0.21744055,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9642015,"pos_list":[0,1,2],"im_url_duplicate_count":[null,4,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-10-04T03:20:04Z\",\"WARC-Record-ID\":\"<urn:uuid:50527520-b2b0-415f-aec2-11fe7d6b99a1>\",\"Content-Length\":\"24185\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:bfdd0658-902d-4e5b-b590-6f791d269e4c>\",\"WARC-Concurrent-To\":\"<urn:uuid:ded2a242-67ce-4730-8433-e9cc50d2411a>\",\"WARC-IP-Address\":\"185.199.108.153\",\"WARC-Target-URI\":\"https://blog.bittercoder.com/part-13-injecting-service-arrays/\",\"WARC-Payload-Digest\":\"sha1:HMYY476Z7BPKOFQVQF7AUDEA7YC7MLNQ\",\"WARC-Block-Digest\":\"sha1:X77TVSTGSDG6ZDALWPSGCRNMMHUIQDZ3\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233511351.18_warc_CC-MAIN-20231004020329-20231004050329-00405.warc.gz\"}"}