Search code examples
architecturedependency-injectioninversion-of-controlautofac

Injecting an internal helper class while resolving a public class


I have the following architecture where a public Service class referencing an internal Helper class exists in another assembly:

ApplicationAssembly {
  public class Widget {
    public Widget(ReferencedAssembly.Service service) { ... }
  }
}

ReferencedAssembly {
  public class Service {
    public Service(Helper helper) { ... }
  }
  class Helper { ... }
}

(I realize that I can't put an internal class in the arguments of a public class's constructor--I'm just trying to illustrate the IoC pattern I'm going after.)

The problem is that ApplicationAssembly can't see ReferencedAssembly.Helper, so it can't be registered in my IoC container (Autofac in this case). As a result, Helper cannot be resolved when I try to resolve Service. What is my best option here?

  • Option 1: Remove Helper from Service's constructor and new it up in the constructor explicitly. I don't like this option because it kind of breaks the IoC paradigm.

  • Option 2: Make Helper implement a public IHelper interface, then add a public module within ReferencedAssembly that registers Helper as an IHelper. I don't like this option because it requires ApplicationAssembly to know too many implementation details about Service, and if the user forgets to register that module at startup everything breaks.

  • Option 3: Create a public static constructor on Service that builds a 2nd IoC container specifically for ReferencedAssembly and registers Helper in it. Remove Helper from Service's constructor and resolve it within the constructor using the 2nd IoC container. This seems like my best option but requires more "plumbing" code than the others. I'm also not a big fan of public static constructors.

  • Option 4. Change my architecture to something else altogether.


Solution

  • The way to register internal types with Autofac is to include an Autofac Module inside the assembly with the internal types, and register the module with the container. Because the module is in the same assembly it can configure the ContainerBuilder with the internal types.

    I think the best option is to make Service implement a public interface, then make the Service class that you have currently internal. Autofac will happily instantiate an internal class to provide a public interface.

    The other option (to allow Service to take a constructor dependency on an internal type) is to make Service's constructor internal, and then use a constructor finder in the Service type registration:

    builder.RegisterType<Service>()
      .FindConstructorsWith(new BindingFlagsConstructorFinder(BindingFlags.NonPublic));