How to register a new <IChecker> list for Autofac ContainerBuilder
var servers = new List<IChecker> { //Server1 new DatabaseSystem { ServerName = "ANTIVIRUS" }, new DatabaseSizes { ServerName = "ANTIVIRUS"}, new DiskBackup { ServerName = "ANTIVIRUS"}, new SqlServerEventLog { ServerName="ANTIVIRUS"}, new DiskSystem { ServerName="ANTIVIRUS"}, //Server1 new DatabaseSystem { ServerName="SEJKG-S-DB01" }, new DatabaseSizes { ServerName = "SEJKG-S-DB01"}, new DiskBackup { ServerName = "SEJKG-S-DB01"}, new SqlServerEventLog { ServerName="SEJKG-S-DB01"}, new DiskSystem { ServerName="SEJKG-S-DB01"}, }; var builder = new ContainerBuilder(); builder.RegisterInstance(notifiers).As<IList<INotifier>>(); builder.RegisterInstance(servers).As<IList<IChecker>>(); builder.Register(c => new ServerChecker(c.Resolve<IList<IChecker>>(), c.Resolve<IList<INotifier>>())); return builder.Build(); I am having a problem with how I should register my "where server = new List {..}" in the container Builder. My iChecker list is passed as a ServerChecker parameter. So much I could decide, but not the list itself, but I have to be outside. The usual list of servers is much larger.
Autofac has support for implicit collection so that you can register several individual elements and when you enable IEnumerable of those, you will receive all registrations:
var builder = new ContainerBuilder(); builder.RegisterType<FirstType>().As<IDependency>(); builder.RegisterType<SecondType>().As<IDependency>(); builder.RegisterType<ThirdType>().As<IDependency>(); var container = builder.Build(); using(var scope = container.BeginLifetimeScope()) { var allDependencies = scope.Resolve<IEnumerable<IDependency>>(); // allDependencies will contain all three of the registered types. } This will allow you to greatly simplify registration, since you would not need to create a list beforehand - you can simply register each instance of "IChecker" or "INotifier" directly and enable IEnumerable of them.
If you have the option to modify the "ServerChecker" constructor to accept IEnumerable, not IList, then you are done - you no longer need to do c.Resolve (), This is probably the best design because IList suggests that the collection can be modified later, which is probably not what you want. (Perhaps ServerChecker copies the IEnumerable content to its own list, so you can change the local copy, but you do not want people to think that they can or should change the contents of the central dependency like this.)
If it should be an IList, you can add another registry that creates the list on the fly:
builder .Register(c => new List<IDependency>(c.Resolve<IEnumerable<IDependency>>())) .As<IList<IDependency>>(); That way, you can still avoid calling c.Resolve <...> () in the permission for "ServerChecker" - Autofac will suddenly learn how to create a specific list type.
If you really want to get an idea about this, you can add a custom Autofac RegistrationSource so that if anyone tries to enable IList Autofac, automatically enable IEnumerable and create a list ... but this is a little more than you probably need.