Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8173911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:19:05+00:00 2026-06-06T22:19:05+00:00

I am getting confused with the scenario of 2 classes implementing the same interface

  • 0

I am getting confused with the scenario of 2 classes implementing the same interface and Dependency Injection.

public interface ISomething
{
  void DoSomething();
}

public class SomethingA : ISomething
{
  public void DoSomething()
  {

  }
}

public class SomethingAB : ISomething
{
  public void DoSomething()
  {

  }
}

public class Different
{
  private ISomething ThisSomething;

  public Different(ISomething Something)
  {  
    ThisSomething = Something;
  }
}

I have seen online examples say that this is valid but you would only use one class at a time. So if the app is running at SiteA you tell your IOC to use SomethingA but if its at SiteB you tell it to use SomethingAB.

Is it considered bad practice therefore to have one app that has 2 classes that implement 1 interface and for it to try to use both classes? If its not how do you tell the IOC which class to use in the relevant circumstance?

UPDATE: To explain it better I will use Ninject’s example:

public class Samurai 
{
    private IWeapon Weapon;

    public Samurai(IWeapon weapon) 
    {
        this.Weapon = weapon;
    }
}

public class Sword : IWeapon
{
...
}

public class Gun : IWeapon
{
...
}

public class WarriorModule : NinjectModule
{
    public override void Load() 
    {
        this.Bind<IWeapon>().To<Sword>();
        this.Bind<IWeapon>().To<Gun>();  //Just an example
    }
}

So now you have 2 classes that use IWeapon. Depending on something or a context in your app you want Samurai to have a Sword sometimes or a Gun at other points. How do you make this happen? How do you handle that “if” scenario??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T22:19:07+00:00Added an answer on June 6, 2026 at 10:19 pm

    I don’t think that this is a bad practice in the general case. There are situations where you could need different implementations of the same interface inside the same application and based on the context use one or another implementation

    As far as how to configure your DI to enable this scenario, well, it will depend on your DI of course 🙂 Some might not support it, others might not, others might partially support it, etc..

    For example with Ninject, you could have the following classes:

    public interface ISomething
    {
    }
    
    public class SomethingA : ISomething
    {
    }
    
    public class SomethingB : ISomething
    {
    }
    
    public class Foo
    {
        public Foo(ISomething something)
        {
            Console.WriteLine(something);
        }
    }
    
    public class Bar
    {
        public Bar(ISomething something)
        {
            Console.WriteLine(something);
        }
    }
    

    and then use named bindings when configuring the kernel:

    // We create the kernel that will be used to provide instances when required
    var kernel = new StandardKernel();
    
    // Declare 2 named implementations of the same interface
    kernel.Bind<ISomething>().To<SomethingA>().Named("somethingA");
    kernel.Bind<ISomething>().To<SomethingB>().Named("somethingB");
    
    // inject SomethingA into Foo's constructor
    kernel.Bind<Foo>().ToSelf().WithConstructorArgument(
        "something", ctx => ctx.Kernel.Get<ISomething>("somethingA")
    );
    
    // inject SomethingB into Bar's constructor
    kernel.Bind<Bar>().ToSelf().WithConstructorArgument(
        "something", ctx => ctx.Kernel.Get<ISomething>("somethingB")
    );
    

    Now when you request an instance of Foo it will inject SomethingA into it its constructor and when you request an instance of Bar it will inject SomethingB into it:

    var foo = kernel.Get<Foo>();
    var bar = kernel.Get<Bar>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting confused on why the compiler is not recognizing my classes. So
I often getting confused with bash and shell. Are they same? If I want
I'm getting confused with my C++ class structure below: A.h: class A{ }; A.cpp
I'm getting confused by bank switching in PIC assembler... This works for putting a
I'm getting confused re the range of options for development & deploying some simple
I am getting confused between TCP being Connection oriented and UDP being connectionless so
I'm getting confused in the doco how I should be setting up Ninject. I'm
I'm getting confused by pointers in objective-c. Basically I have a bunch of static
I am getting confused with size_t in C. I know that it is returned
I'm getting confused working with character string arrays. I'm trying to fill 2 arrays

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.