AFAIK, Autofac requires that each necessary type is registered in the container, but this does not mean that you must do each separately. Almost 99% of my registrations are handled by adding this attribute to the type:
[AttributeUsage(AttributeTargets.Class)] public class AutoRegisterAttribute : Attribute { }
So, for example, you will have
[AutoRegister] class ComboActions {
And then I register them as follows:
public class AutoScanModule : Module { private readonly Assembly[] _assembliesToScan; public AutoScanModule(params Assembly[] assembliesToScan) { _assembliesToScan = assembliesToScan; } protected override void Load(ContainerBuilder builder) { builder.RegisterAssemblyTypes(_assembliesToScan) .Where(t => t.GetCustomAttributes(typeof (AutoRegisterAttribute), false).Any()) .AsSelf() .AsImplementedInterfaces() .InstancePerLifetimeScope(); } }
As I said, this covers most of my registrations, and then I usually have to worry about things like third-party manufacturers, open generics, and decorators.
EDIT: Answer the answer from Kaleb , which proves that I am wrong. A cool feature that I never knew about!
source share