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 8256291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:49:00+00:00 2026-06-08T01:49:00+00:00

What would the following example look like re-written to use Ninject? Specifically how would

  • 0

What would the following example look like re-written to use Ninject?

Specifically how would you bind a Samurai to both Shuriken and Sword?

(From https://github.com/ninject/ninject/wiki/Dependency-Injection-By-Hand)

interface IWeapon
{
    void Hit(string target);
}

class Sword : IWeapon
{
    public void Hit(string target) 
    {
        Console.WriteLine("Chopped {0} clean in half", target);
    }
}

class Shuriken : IWeapon
{
    public void Hit(string target)
    {
        Console.WriteLine("Pierced {0}'s armor", target);
    }
}

class Program
{
    public static void Main() 
    {
        var warrior1 = new Samurai(new Shuriken());
        var warrior2 = new Samurai(new Sword());
        warrior1.Attack("the evildoers");
        warrior2.Attack("the evildoers");    
       /* Output...
        * Piereced the evildoers armor.
        * Chopped the evildoers clean in half.
        */
    }
}
  • 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-08T01:49:02+00:00Added an answer on June 8, 2026 at 1:49 am

    You could simply rebind IWeapon after resolving first Samurai:

    StandardKernel kernel = new StandardKernel();
    
    kernel.Bind<IWeapon>().To<Sword>().Named();
    Samurai samurai1 = kernel.Get<Samurai>();
    samurai1.Attack("enemy");
    
    kernel.Rebind<IWeapon>().To<Shuriken>();
    Samurai samurai2 = kernel.Get<Samurai>();
    samurai2.Attack("enemy");
    

    Alternatively, you could use named bindings. It would be first necessary to redefine Samurai‘s constructor a bit, adding a Named attribute to its dependency:

    public Samurai([Named("Melee")]IWeapon weapon)
    {
        this.weapon = weapon;
    }
    

    Then, you would need to give names to your bindings as well:

    StandardKernel kernel = new StandardKernel();
    
    kernel.Bind<IWeapon>().To<Sword>().Named("Melee");
    kernel.Bind<IWeapon>().To<Shuriken>().Named("Throwable");
    
    Samurai samurai = kernel.Get<Samurai>();
    samurai.Attack("enemy"); // will use Sword
    

    There are really many alternatives – I would recommend browsing over the link provided by @scottm and checking them all out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What would the following cURL command look like as a generic (without cURL) http
In the following example I would like to retrieve the text between pMAINp and
I have a big list, but micro example would be like the following: A
The following code is an example of what I think would qualify as pseudocode,
For example a string contains the following (the string is variable): http://www.google.comhttp://www.google.com What would
What would the basic C# code look like to model a many-to-many relationship, where
I have several strings which look like the following: <some_text> TAG[<some_text>@11.22.33.44] <some_text> I want
Expert, I would like to create a custom control with following feature and this
I have an XML File with Elements that look like the following: <level> <name>Name
I have the following example for which I'd like to calculate totals at each

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.