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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:24:45+00:00 2026-06-03T01:24:45+00:00

If I have the following code: public class RobotNavigationService : IRobotNavigationService { public RobotNavigationService(IRobotFactory

  • 0

If I have the following code:

public class RobotNavigationService : IRobotNavigationService {
  public RobotNavigationService(IRobotFactory robotFactory) {
    //...
  }
}
public class RobotFactory : IRobotFactory {
  public IRobot Create(string nameOfRobot) {
    if (name == "Maximilian") {
      return new KillerRobot(); 
    } else {
      return new StandardRobot();
    }
  }
}

My question is what is the proper way to do Inversion of Control here? I don’t want to add the KillerRobot and StandardRobot concretes to the Factory class do I? And I don’t want to bring them in via a IoC.Get<> right? bc that would be Service Location not true IoC right? Is there a better way to approach the problem of switching the concrete at runtime?

  • 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-03T01:24:46+00:00Added an answer on June 3, 2026 at 1:24 am

    For your sample, you have a perfectly fine factory implementation and I wouldn’t change anything.

    However, I suspect that your KillerRobot and StandardRobot classes actually have dependencies of their own. I agree that you don’t want to expose your IoC container to the RobotFactory.

    One option is to use the ninject factory extension:

    https://github.com/ninject/ninject.extensions.factory/wiki

    It gives you two ways to inject factories – by interface, and by injecting a Func which returns an IRobot (or whatever).

    Sample for interface based factory creation: https://github.com/ninject/ninject.extensions.factory/wiki/Factory-interface

    Sample for func based: https://github.com/ninject/ninject.extensions.factory/wiki/Func

    If you wanted, you could also do it by binding a func in your IoC Initialization code. Something like:

    var factoryMethod = new Func<string, IRobot>(nameOfRobot =>
                            {
                                if (nameOfRobot == "Maximilian")
                                {
                                    return _ninjectKernel.Get<KillerRobot>();
                                }
                                else
                                {
                                    return _ninjectKernel.Get<StandardRobot>();
                                }
    
                            });
    _ninjectKernel.Bind<Func<string, IRobot>>().ToConstant(factoryMethod);
    

    Your navigation service could then look like:

        public class RobotNavigationService
        {
            public RobotNavigationService(Func<string, IRobot> robotFactory)
            {
                var killer = robotFactory("Maximilian");
                var standard = robotFactory("");
            }
        }
    

    Of course, the problem with this approach is that you’re writing factory methods right inside your IoC Initialization – perhaps not the best tradeoff…

    The factory extension attempts to solve this by giving you several convention-based approaches – thus allowing you to retain normal DI chaining with the addition of context-sensitive dependencies.

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

Sidebar

Related Questions

I have the following code: public class ClassExample { void DoSomthing<T>(string name, T value)
I have the following code: public class MyElement { String name; String type; MyElement(String
I have the following code : public class MyClass { private readonly string name;
I have following code public class TEST { public static void main(String arg[]){ try
I have the following code: public class AppDomainArgs : MarshalByRefObject { public string myString;
If I have the following code: public class MyClass { public string myName {
Hi I have following code block public class Driver { static String x =
I have following code: public class LCS1 { public static String lcs(String a, String
I have the following code: public class Parent { public string MyField { get;
I have the following code: public class Tests { public static void main(String[] args)

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.