title: Using a Strategy and Factory Pattern with Dependency Injection
body: <p>I am working on a side project to better understand Inversion of Control and Dependency Injection and different design patterns.</p>
  
  <p>I am wondering if there are <strong>best practices to using DI with the factory and strategy patterns</strong>?</p>
  
  <p><strong>My challenge comes about when a strategy (built from a factory) requires different parameters for each possible constructor and implementation</strong>.  As a result I find myself declaring all possible interfaces in the service entry point, and passing them down through the application.  As a result, the entry point must be changed for new and various strategy class implementations.</p>
  
  <p>I have put together a paired down example for illustration purposes below.  My stack for this project is .NET 4.5/C# and Unity for IoC/DI.</p>
  
  <p>In this example application, I have added a default Program class that is responsible for accepting a fictitious order, and depending on the order properties and the shipping provider selected, calculating the shipping cost.  There are different calculations for UPS, DHL, and Fedex, and each implemnentation may or may not rely on additional services (to hit a database, api, etc).</p>
  
  <pre><code>public class Order
  {
      public string ShippingMethod { get; set; }
      public int OrderTotal { get; set; }
      public int OrderWeight { get; set; }
      public int OrderZipCode { get; set; }
  }
  </code></pre>
  
  <p><strong>Fictitious program or service to calculate shipping cost</strong></p>
  
  <pre><code>public class Program
  {
      // register the interfaces with DI container in a separate config class (Unity in this case)
      private readonly IShippingStrategyFactory _shippingStrategyFactory;
  
      public Program(IShippingStrategyFactory shippingStrategyFactory)
      {
          _shippingStrategyFactory = shippingStrategyFactory;
      }
  
      public int DoTheWork(Order order)
      {
          // assign properties just as an example
          order.ShippingMethod = "Fedex";
          order.OrderTotal = 90;
          order.OrderWeight = 12;
          order.OrderZipCode = 98109;
  
          IShippingStrategy shippingStrategy = _shippingStrategyFactory.GetShippingStrategy(order);
          int shippingCost = shippingStrategy.CalculateShippingCost(order);
  
          return shippingCost;
      }
  }
  
  // Unity DI Setup
  public class UnityConfig
  {
      var container = new UnityContainer();
      container.RegisterType&lt;IShippingStrategyFactory, ShippingStrategyFactory&gt;();
      // also register  IWeightMappingService and IZipCodePriceCalculator with implementations
  }
  
  public interface IShippingStrategyFactory
  {
      IShippingStrategy GetShippingStrategy(Order order);
  }
  
  public class ShippingStrategyFactory : IShippingStrategyFactory
  {
      public IShippingStrategy GetShippingStrategy(Order order)
      {
          switch (order.ShippingMethod)
          {
              case "UPS":
                  return new UPSShippingStrategy();
  
              // The issue is that some strategies require additional parameters for the constructor
              // SHould the be resolved at the entry point (the Program class) and passed down?
              case "DHL":
                  return new DHLShippingStrategy();
  
              case "Fedex":
                  return new FedexShippingStrategy();
  
              default:
                  throw new NotImplementedException(); 
          }
      }
  }
  </code></pre>
  
  <p><strong>Now for the Strategy interface and implementations.</strong>  UPS is an easy calculation, while DHL and Fedex may require different services (and different constructor parameters).</p>
  
  <pre><code>public interface IShippingStrategy
  {
      int CalculateShippingCost(Order order);
  }
  
  public class UPSShippingStrategy : IShippingStrategy()
  {
      public int CalculateShippingCost(Order order)
      {
          if (order.OrderWeight &lt; 5)
              return 10; // flat rate of $10 for packages under 5 lbs
          else
              return 20; // flat rate of $20
      }
  }
  
  public class DHLShippingStrategy : IShippingStrategy()
  {
      private readonly IWeightMappingService _weightMappingService;
  
      public DHLShippingStrategy(IWeightMappingService weightMappingService)
      {
          _weightMappingService = weightMappingService;
      }
  
      public int CalculateShippingCost(Order order)
      {
          // some sort of database call needed to lookup pricing table and weight mappings
          return _weightMappingService.DeterminePrice(order);
      }
  }
  
  public class FedexShippingStrategy : IShippingStrategy()
  {
      private readonly IZipCodePriceCalculator _zipCodePriceCalculator;
  
      public FedexShippingStrategy(IZipCodePriceCalculator zipCodePriceCalculator)
      {
          _zipCodePriceCalculator = zipCodePriceCalculator;
      }
  
      public int CalculateShippingCost(Order order)
      {
          // some sort of dynamic pricing based on zipcode
          // api call to a Fedex service to return dynamic price
          return _zipCodePriceService.CacluateShippingCost(order.OrderZipCode);
      }
  }
  </code></pre>
  
  <p>The issue with the above is that each strategy requires additional and different services to perform the 'CalculateShippingCost' method.  Do these interfaces/implementations need to be registered with the entry point (the Program class) and passed down through the constructors?</p>
  
  <p>Are there other patterns that would be a better fit to accomplish the above scenario?  Maybe something that Unity could handle specifically (<a href="https://msdn.microsoft.com/en-us/library/dn178463(v=pandp.30).aspx" rel="noreferrer">https://msdn.microsoft.com/en-us/library/dn178463(v=pandp.30).aspx</a>)?</p>
  
  <p>I greatly appreciate any help or a nudge in the right direction.</p>
  
  <p>Thanks,
  Andy</p>