title: dependency injection or factory pattern c#
body: <p>I have to write a Complex calculator logic which has 4 different components to be calculated brokerage, stockprice, admin charges &amp; other charges. Each having a different logic and formulas.</p>
  
  <p>So I decided to use Unity DI. I have a ContainerFactoryClass which resolves all classes which implements  IChargeCalculator interface as shown below in the TotalAnnualCostCalculator constructor. </p>
  
  <pre><code>public class TotalAnnualCostCalculator
  {
      private readonly IUnityContainer container;
  
     //Constructor
      public TotalAnnualCostCalculator()
      {
          container = ContainerFactory.InitializeContainer();
          ContainerFactory.SetupContainer(container);
      }
      public AnnualCostCharges CalculateTotalAnnualCost(Parameters product)
      {
            var calculators = container.ResolveAll&lt;ICalculator&gt;().ToList();
            // Invoke calcualtion method
  
             Parallel.ForEach(calculators, c =&gt;
          {
              return c.CalculateAnnualCost(product);
  
          });
      }
  
  }
  </code></pre>
  
  <p><strong>Container Factory class:-</strong></p>
  
  <pre><code>public static class ContainerFactory
  {
      public static IUnityContainer Container { get; private set; }
      public static IUnityContainer InitializeContainer()
      {
          var container = new UnityContainer();
  
          RegisterDependencies(container);
  
          return container;
      }
  
      private static void RegisterDependencies(UnityContainer container)
      {
          container.RegisterType&lt;ICalculatorStrategyFactory, CalculatorStrategyFactory&gt;("Factory");
  
          container.RegisterType&lt;IEffectiveAnnualCostCalculator, InvestmentManagementChargeCalculator&gt;("IMCChargeCalculator",
              new InjectionConstructor(new ResolvedParameter&lt;ICalculatorStrategyFactory&gt;("Factory")));
          //container.RegisterType&lt;IEffectiveAnnualCostCalculator, AdministrationChargeCalculator&gt;("AdministrationChargeCalculator");
          container.RegisterType&lt;IEffectiveAnnualCostCalculator, AdviceChargeCalculator&gt;("AdviceChargeCalculator");
          container.RegisterType&lt;IEffectiveAnnualCostCalculator, OtherChargeCalculator&gt;("OtherChargeCalculator");
  
          container.RegisterType&lt;IInvestmentManagementChargeCalculator, LumpSumIMCCalculator&gt;("LumpSumIMCCalculator");
          container.RegisterType&lt;IInvestmentManagementChargeCalculator, DebitOrderIMCCalculator&gt;("DebitOrderIMCCalculator");
      }
  
      public static void SetupContainer(IUnityContainer container)
      {
          Container = container;
      }
  }
  </code></pre>
  
  <p>Then any API consumes my Calculator.dll by just creating an instance of TotalAnnualCostCalculator  and call a method like this.</p>
  
  <pre><code>var totalCharges = calc.CalculateTotalAnnualCost(prod);
  </code></pre>
  
  <p><strong>My code reviewer says its better to use Factory Pattern ,as this tightly coupled to Unity Framework.</strong> </p>
  
  <p>Please advise</p>