title: Using factory for everything?
body: <p>My software shall control a device or multiple of them. The program flow is like that it will search for devices on all available interfaces and instantiate an object of the type <code>IDevice</code> for each device found. To state it here, all these objects must be set up during runtime.</p>
  <p>Using Autofac to gain dependency inversion (DI) everything has to be set up at the beginning. If something shall be used during runtime &quot;factories&quot; are the thing to use. So lets dig in into an example:</p>
  <p>The Program start would look like this:</p>
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          var container = ContainerConfig.Configure();
  
          using (var scope = container.BeginLifetimeScope())
          {
              var app = scope.Resolve&lt;IApplication&gt;();
              app.Run();
          }
      }
  }
  </code></pre>
  <p>As soon as &quot;Application&quot; has been resolved everything is set up that is known at start time. The Application then gets started (<code>app.Run()</code>), searching devices and then setting up a list of all devices found. Setting up a device now requires a bunch of &quot;Factories&quot;, basically (and here's the problem) &quot;one for each class&quot; that is instantiated through &quot;Device&quot;.</p>
  <pre><code>class Application : IApplication
  {
      readonly Device.Factory _deviceFactory;
      readonly Log.Factory _logFactory;
      readonly DateTime.Factory _dateTimeFactory;
      readonly Incident.Factory _incidentFactory;
  
      public Application(Device.Factory deviceFactory, Log.Factory logFactory, DateTime.Factory dateTimeFactory, Incident.Factory incidentFactory)
      {
          _deviceFactory = deviceFactory;
          _logFactory = logFactory;
          _dateTimeFactory = dateTimeFactory;
          _incidentFactory = incidentFactory;
      }
  
      public void Run()
      {
          List&lt;string&gt; listOfDeviceIds = SearchAllDevices();
          List&lt;IDevice&gt; listOfDevices = new List&lt;IDevice&gt;;
          
          foreach(string deviceId in listOfDeviceIds)
          {
              listOfDevices.Add(deviceFactory(_logFactory, _dateTimeFactory, incidentFactory);
          }
      }
  }
  </code></pre>
  <p>A &quot;Device&quot; contains a logger tracking something, in this example it will log a time and an incident. So as soon as a new log is set up, it will require a few factories injected.</p>
  <pre><code>public class Device : IDevice
  {
      public delegate Device Factory(Log.Factory logFactory, DateTime.Factory dateTimeFactory, Incident.Factory incidentFactory);
      
      readonly DateTime.Factory _dateTimeFactory;
      readonly Incident.Factory _incidentFactory;
      readonly Log.Factory _logFactory;
      
      List&lt;Log&gt; ListOfLogs = new List&lt;Log&gt;();
      
      public Device(Log.Factory logFactory, DateTime.Factory dateTimeFactory, Incident.Factory incidentFactory)
      {
          _dateTimeFactory = dateTimeFactory;
          _incidentFactory = incidentFactory;
          _logFactory = logFactory;
      }
      
      public AddLog()
      {
          ListOfLogs().Add(_logFactory(_dateTimeFactory(), _incidentFactory() ));
      }
  }
  
  
  public class Log()
  {
      public delegate Log Factory();
      
      public IDateTime DateTime() { get; }
      public IIncident Incident() { get; }
      
      public Log(IDateTime dateTime, IIncident incident)
      {
          DateTime = dateTime;
          Indicent = incident;
      }
  }
  
  
  public DateTime : IDateTime
  {
      public delegate DateTime Factory();
      
      //...
  }
  
  public Indicent : IIndicent
  {
      public delegate Indicent Factory();
      
      //...
  }
  </code></pre>
  <p>Now, this is only a little insight of the device class which in real integrates a lot more stuff. And that's were it's now getting messy again.</p>
  <p>I have about 30 subclasses or even more that are set up when instantiating a &quot;Device&quot;. So because they are set up during runtime they will all need a &quot;Factory&quot; that has to be provided through the &quot;Device&quot; its constructor. Well I guess you see the point.</p>
  <p>How do you deal with that? Or am I on the wrong track again? Do I misunderstand something by trying to inverse dependency on everything?</p>
  <p>The other point is, that the entire story is about to make it testable, where interface and autofac are the key words. But when using a factory I always have to use the class it self and can't use its interface as reference, at which point DI isn't implemented anymore. ... ?</p>
  <pre><code>public class MyClass : IMyClass
  {
      public delegate MyClass Factory();
      // ...
  }
  </code></pre>
  <p>using it will require <code>MyClass.Factory</code> instead of <code>IMyClass.Factory</code>:</p>
  <pre><code>class Main()
  {
      MyClass.Factory _myClassFactory;
      Main(MyClass.Factory myClassFactory)
      {
          _myClassFactory = myClassFactory;
      }
      
      foo ()
      {
          IMyClass myInstance = myClassFactory();
      }
  }
  </code></pre>