title: What is the right way to register dependencies?-"check for service registration using IsRegistered(), or use the ResolveOptional() method
body: <p>Here is my <code>factory</code> class whose responsibility is to return the <code>instance</code> based on input type.</p>
  
  <pre><code>public class ProcessFactory: IProcessFactory
  {
      public ProcessFactory(IProcess&lt;ProcessSalaryRequest&gt; processSalaryRequest,
                            IProcess&lt;ProcessLeavesRequest&gt; processLeavesRequest)
      {
           _processSalaryRequest = processSalaryRequest;
           _processLeaveRequest = processLeavesRequest;
      }
  }
  
  public IProcess GetInstance(string type)
  {
      Type atype;
      if (Enum.TryParse(type, out atype))
      {
          switch (atype)
          {
              case Type.Type1:
                  return _processSalaryRequest ;
                  break;
              case Type.Type2:
                  return _processLeaveRequest ;
                  break;
            }
      }
      return null;
   }
  }
  </code></pre>
  
  <p>This is my interface. A non-generic interface and make the generic interface inherit from it.</p>
  
  <pre><code>public interface IProcess
  {
       void Process(string id);
  }
  public interface IProcess&lt;T&gt; : IProcess where T : class
  {
  }
  </code></pre>
  
  <p>Here are my registrations </p>
  
  <pre><code>builder.RegisterType&lt;ProcessSalaryRequest&gt;().As&lt;IProcess&lt;ProcessSalaryRequest&gt;&gt;().SingleInstance();
  builder.RegisterType&lt;ProcessLeavesRequest&gt;().As&lt;IProcess&lt;ProcessLeavesRequest&gt;&gt;().SingleInstance();
  </code></pre>
  
  <p>Now, here comes the challenge while registering the <code>factory</code></p>
  
  <pre><code>  builder.Register(c =&gt; new ProcessFactory(c.Resolve&lt;ProcessSalaryRequest&gt;(),c.Resolve&lt;ProcessLeavesRequest&gt;())).As&lt;IProcessFactory&gt;().SingleInstance();
  </code></pre>
  
  <p>I am getting below error</p>
  
  <blockquote>
    <p>'MyProject.ProcessSalaryRequest' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.'</p>
  </blockquote>
  
  <p><strong>Updates</strong></p>
  
  <p>Here is <code>ProcessSalaryRequest</code></p>
  
  <pre><code>  public class ProcessSalaryRequest : IProcess&lt;ProcessSalaryRequest&gt;
    {
      private readonly IStatusClient _statusClient;
      public ProcessSalaryRequest(IStatusClient statusClient)
      {
          _statusClient = statusResponseClient;
      }
      void Process(string id)
      {
  
      }
  
    }
  </code></pre>
  
  <p><strong>Diagram of dependency</strong></p>
  
  <p><a href="https://i.stack.imgur.com/8Fahc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8Fahc.png" alt="enter image description here"></a></p>
  
  <blockquote>
    <p>DependencyResolutionException: Circular component dependency detected: MyProject.Engine -> MyProject.Broker -> MyProject.ProcessControl -> MyProject.Status -> MyProject.StatusManager -> MyProject.Prcoessor-> MyProject.ProcessSalaryRequest -> MyProject.Status</p>
  </blockquote>
  
  <p>Note: <code>Processor</code> gets the instance using <code>factory</code> based on some business conditions. </p>