title: Dependency Registrar for other assembly
body: <p>I have a simple question about dependecy registration. </p>
  
  <p>I'm developing a brand new web application that use Engine Context paradigm with Autofac container. For any library on the solution I have one class implementing <code>IDependencyRegistrar</code> that implement a common <code>Register</code> method, due to add one the container some specific implementation of some interfaces and components.</p>
  
  <p>In this way, a base Core library (running at application startup) provide a <code>RegisterDependencies</code> method that lookup on every Executing Assembly to discover all the DDL's used by the application and registering them on Autofac Container. </p>
  
  <p>The code that provide this behavior is:</p>
  
  <pre><code> builder = new ContainerBuilder();
          var drTypes = typeFinder.FindClassesOfType&lt;IDependencyRegistrar&gt;();
          var drInstances = new List&lt;IDependencyRegistrar&gt;();
          foreach (var drType in drTypes)
              drInstances.Add((IDependencyRegistrar) Activator.CreateInstance(drType));
          //sort
          drInstances = drInstances.AsQueryable().OrderBy(t =&gt; t.Order).ToList();
          foreach (var dependencyRegistrar in drInstances)
              dependencyRegistrar.Register(builder, typeFinder, config);
          builder.Update(container);
  </code></pre>
  
  <p>Where the <code>FindClassOfType&lt;IDependencyRegistrar&gt;</code> works thanks to a Method implementation like that:</p>
  
  <pre><code>        public virtual IList&lt;Assembly&gt; GetAssemblies()
      {
          var addedAssemblyNames = new List&lt;string&gt;();
          var assemblies = new List&lt;Assembly&gt;();
  
          if (LoadAppDomainAssemblies)
              AddAssembliesInAppDomain(addedAssemblyNames, assemblies);
          AddConfiguredAssemblies(addedAssemblyNames, assemblies);
  
          return assemblies;
      }
  </code></pre>
  
  <p>And, <code>AddAssemblyInAppDomain</code> is:</p>
  
  <pre><code>private void AddAssembliesInAppDomain(List&lt;string&gt; addedAssemblyNames, List&lt;Assembly&gt; assemblies)
      {
          foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
          {
              if (Matches(assembly.FullName))
              {
                  if (!addedAssemblyNames.Contains(assembly.FullName))
                  {
                      assemblies.Add(assembly);
                      addedAssemblyNames.Add(assembly.FullName);
                  }
              }
          }
      }
  </code></pre>
  
  <p><strong>The problem is:</strong> when I end up on adding in mysolution the MVC project (the front-end), I've referenced on it only direct accessing library (service layer and some infrastructure components) but no DataLayer components and some other DLL. Due to the fact that MVC not referencing directly some libraries of deep layers, my Engine Context doesn't see the others sub-components and not registering them on the Autofac container, causing a </p>
  
  <blockquote>
    <p>'no registered services'</p>
  </blockquote>
  
  <p>exception when execution make explicit request on them. </p>
  
  <p>The whole system just works if I add reference to any library from the MVC project but, for layered architectured application, this is not a best practice: my MVC need to know nothing about DataLayer or others low-layered services. </p>
  
  <p>However, in this way, no ExecutingAssembly are discovered, so, not dependency are registered anymore.</p>
  
  <p>Wich is the best approch to resolve this situation without referencing all assemblies directly from main MVC project?</p>