Component with multiple interfaces

Feb 26, 2008 at 2:24 AM
When we get to configuration, it would be nice being able to support that a given component might implement multiple interfaces.

public interface IFoo { ... }
public interface IDatabase { ... }
public class FooDatabase : IFoo, IDatabase {...}

Using code, I could do the following:

Foo foo = container.BuildUp(new Foo());

Ideally, I'd like to see:

  <add type="Foo, MyAssembly" ...>
      <add type="IFoo, ..."/>
      <add type="IDatabase, ..."/>
Feb 26, 2008 at 9:05 AM
This sounds like an good idea to me.

I have some concerns about registration of several interfaces on one instance. I sounds like a good design breaker. But that is another discution.
Mar 5, 2008 at 6:39 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Mar 6, 2008 at 6:48 PM
This is a bad idea in general.
It violates SRP
Mar 6, 2008 at 8:32 PM
This is not always a bad idea as it depends on the situation.

When the interface registered is in turn an extension of another interface it is often desired to have either key be satisfied by a single registration.
i.e. IServiceContainer is an extension of IServiceProvider. In most implementations of IServiceContainer (including the one baked into .Net itself) a request to IServiceContainer.GetService(typeof(IServiceProvider)) will return the service container instance itself. Afterall, IServiceContainer IS a IServiceProvider. This becomes important in many frameworks when components in different levels need access to facilities that may be extended in other levels to another level of specification. In that case the decision making is delegated to the person(s) making the choice of configuration.

I do agree that having two seperate and distinct business responsibilities satisfied by a single type is violation of SRP, that is absolutely NOT the case for specializations of a business responsibility.


PS I am in know way advocating that container anti-pattern to end all anti-patterns of requesting from Unity for an IUnityContainer. I am just using the above real life concrete interface specialization in .Net that I am sure we've all used before. in our own code
Mar 6, 2008 at 9:29 PM
Actually, if you follow SIP then you will often need this, as a single object will end up implementing multiple interfaces.

Mar 7, 2008 at 1:10 AM
What you are talking about is actually pretty rare in most production code that I have seen.
In general, when you have those varying levels of the same component, it is generally a sign that you are overusing inheritance.

One interface, one responsability.