This project is read-only.

Do I need to create multiple interfaces

Jul 28, 2012 at 3:21 AM


it should be simple but I I have a quick question :

Scenario : I am returning a string value from web service and from some other source

Lets say Interface has a function like

string Getstring();
Now I have 2 classes that will implement this interface, lets say Class A And B .
While registering in Global.asax.cs 
How would I register both class :

The 2 lines above will return error but both classes implement same interface. Do I need to create seperate interface for each class to register them ?


Jul 28, 2012 at 9:27 AM
In general, you should prevent having this situation, since there should be no ambiquity in what to resolve. You have two implementations, but which implementation should the application use? If the implementations are not related and are important to the application, you should define two separate interfaces. It doesn't matter that they look the same; they both represent a different service.

However, it could be that you need multiple inplementations of the same service. This will generally be the case when you have a set of those services and iterate over them, and calling their GetString method. In that case you can register them as a set using RegisterAll<Interface1>() and let application code depend on an IEnumerable<Interface1>. Even better would it be to wrap those implementations in a composite (an implementation of Interface1 that wraps multiple Interface1s or an IEnumerable<Interface1>). This would allow the application to simply depend upon Interface1 (instead of IEnumerable<Interface1>) and it would move the iteration logic out of the application and into the composite.

Since your question is very general, I can't be any more specific about what solution would be best for you. I hope this gives you some ideas about how to solve your problem.

DeveloperNet <> wrote:

Jul 28, 2012 at 10:08 PM

Thank you for clarifying on that . But imagine a situation where we have a getemployee function and the functions are like :

1) GetEmployeeSQL

2) GetEmployeeMemory

3) GetEmployeeMock

Now in this situation you will have one Interface or 3 different interfaces . if you one interface how will you resolve them ?

The way I thought of resoving my problem stated above is like this :

I am getting the source value from config file :


classToCreate = System.Configuration.ConfigurationManager.AppSettings["Source" ];








Is there any better way ?



Jul 29, 2012 at 7:38 AM
If I understand correctly, you are talking about a repository interface, such as IEmployeeRepository. It is very natural to have multiple implementations of such interface when having automated tests. You are talking about a memory and mock implementation, which you probably need in your unit tests.

When writing unit tests, you typically don't use a DI container at all. You simply create a class under test manually and inject the fake implementations of its dependencies in its constructor. Using the container within your unit tests would make your tests much harder to read, and maintain. Make sure you design your objects around constructor injection, and you wont need to configure the container in your tests.

Since you only should configure the container for the application, you loose the need to configure the MemoryEmployeeRepository and the MockEmployeeRepository. What's left is your SqlEmployeeRepository and you can simply register it using container.Register<IEmployeeRepository, SqlEmployeeRepository>().

Does this make sense? If I misunderstood please supply me with more context about your design and how your application is set up.

DeveloperNet <> wrote:

Jul 29, 2012 at 10:09 PM

Thank you . Is there any simple example which I can follow to understand the concept you have written above ?


Jul 30, 2012 at 6:26 PM

It depends on what you need to know, but here are a few pointers:

Marked as answer by dot_NET_Junkie on 11/4/2013 at 1:58 AM