While I was exploring Autofac, I found AsImplementedInterfaces quite interesting. Not only it is useful, but also, if we are not careful with its use, we may end up with unexpected application behavior. However, it was quite difficult to find an example which explains -
- What is AsImplementedInterfaces and how does it help?
- Why does code with AsImplementedInterfaces have a bad smell?
AsImplementedInterfaces
Consider a class BackupAndLogService, which implements two interfaces, namely BackupService and LoggingService. Another class, DataController, has a dependency on the BackupService for its data backup operations.
Now, there are two ways to register this dependency. Firstly, register BackupAndLogService explicitly as BackupService. However, if there comes another class which has a dependency of LoggingService, our code will fail. In order to meet the second requirement, we again need to register BackupAndLogService. explicitly as LoggingService.
Secondly, we can register "BackupAndLogService" with the container using AsImplementedInterfaces. As a result, the container automatically registers the implementation against all the interfaces it implements. Therefore, when we add a dependency of either of the interfaces implemented by BackupAndLogService, the container will be able to resolve it.
The Bad Smell
The problem occurs when we have two classes implementing a common interface and they both are registered using AsImplementedInterfaces. In such a case, the container will always provide the second implementation while we might be expecting the first one.
Assume, there is another class WindowsLoggingAndGeneralService, which provides (implements) LoggingService and GeneralService to windows users of the application. And, we register the two implementation classes as shown in the code below.
- private static void RegisterTypes(ContainerBuilder builder)
- {
- builder.RegisterType < DataController > ();
- builder.RegisterType < BackupAndLogService > ().AsImplementedInterfaces();
- builder.RegisterType < WindowsLoggingAndGeneralService > ().AsImplementedInterfaces();
- }

The image shows the result of dependency resolution based on the registrations we have in the above code sample. Such situations usually occur if we are not careful with our registrations. However, in this particular scenario, we can fix the problem by registering any one of the implementations explicitly. I would leave that for you to try and observe the changes.
Thank you!

Tejal ModiPosted Jan 5, 2021, 1:23 PM
Well explained!
Tridip BhattacharjeePosted Sep 14, 2017, 8:34 AM
There was a big hole in article. no previous article links are mention. please mention/embed 1 to 5 prev article links.
Ketan MokariyaPosted Sep 13, 2017, 8:55 AM
This is what i was finding .Good article