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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:51:21+00:00 2026-05-28T14:51:21+00:00

Okay, so recently I’ve been reading into ninject but I am having trouble understanding

  • 0

Okay, so recently I’ve been reading into ninject but I am having trouble understanding what makes it better over why they referred do as ‘poor man’s’ DI on the wiki page. The sad thing is I went over all their pages on the wiki and still don’t get it =(.

Typically I will wrap my service classes in a factory pattern that handles the DI like so:

public static class SomeTypeServiceFactory
{
    public static SomeTypeService GetService()
    {
        SomeTypeRepository someTypeRepository = new SomeTypeRepository();
        return = new SomeTypeService(someTypeRepository);

    }

}

Which to me seems a lot like the modules:

public class WarriorModule : NinjectModule {
    public override void Load() {
      Bind<IWeapon>().To<Sword>();
      Bind<Samurai>().ToSelf().InSingletonScope();
    }
}

Where each class would have it’s associated module and you Bind it’s constructor to a concrete implementation. While the ninject code is 1 less line I am just not seeing the advantage, anytime you add/remove constructors or change the implementation of an interface constructor, you’d have to change the module pretty much the same way as you would in the factory no? So not seeing the advantage here.

Then I thought I could come up with a generic convention based factory like so:

 public static TServiceClass GetService<TServiceClass>()
        where TServiceClass : class
    {
        TServiceClass serviceClass = null;

        string repositoryName = typeof(TServiceClass).ToString().Replace("Service", "Repository");
        Type repositoryType = Type.GetType(repositoryName);

        if (repositoryType != null)
        {
            object repository = Activator.CreateInstance(repositoryType);
            serviceClass =  (TServiceClass)Activator.CreateInstance(typeof (TServiceClass), new[]{repository});
        }

        return serviceClass;
    }

However, this is crappy for 2 reasons: 1) Its tightly dependent on the naming convention, 2) It assumed the repository will never have any constructors (not true) and the service’s only constructor will be it’s corresponding repo (also not true). I was told “hey this is where you should use an IoC container, it would be great here!” And thus my research began…but I am just not seeing it and am having trouble understanding it…

Is there some way ninject can automatically resolve constructors of a class without a specific declaration such that it would be great to use in my generic factory (I also realize I could just do this manually using reflection but that’s a performance hit and ninject says right on their page they don’t use reflection).

Enlightment on this issue and/or showing how it could be used in my generic factory would be much appreciated!

EDIT: Answer

So thanks to the explanation below I was ably to fully understand the awesomeness of ninject and my generic factory looks like this:

public static class EntityServiceFactory
{
    public static TServiceClass GetService<TServiceClass>()
        where TServiceClass : class
    {
        IKernel kernel = new StandardKernel();

        return kernel.Get<TServiceClass>();
    }
}

Pretty awesome. Everything is handled automatically since concrete classes have implicit binding.

  • 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-05-28T14:51:22+00:00Added an answer on May 28, 2026 at 2:51 pm

    The benefit of IoC containers grows with the size of the project. For small projects their benefit compared to “Poor Man’s DI” like your factory is minimal. Imagine a large project which has thousands of classes and some services are used in many classes. In this case you only have to say once how these services are resolved. In a factory you have to do it again and again for every class.

    Example: If you have a service MyService : IMyService and a class A that requires IMyService you have to tell Ninject how it shall resolve these types like in your factory. Here the benefit is minimal. But as soon as you project grows and you add a class B which also depends on IMyService you just have to tell Ninject how to resolve B. Ninject knows already how to get the IMyService. In the factory on the other hand you have to define again how B gets its IMyService.

    To take it one step further. You shouldn’t define bindings one by one in most cases. Instead use convention based configuration (Ninject.Extension.Conventions). With this you can group classes together (Services, Repositories, Controllers, Presenters, Views, ….) and configure them in the same way. E.g. tell Ninject that all classes which end with Service shall be singletons and publish all their interfaces. That way you have one single configuration and no change is required when you add another service.

    Also IoC containers aren’t just factories. There is much more. E.g. Lifecycle managment, Interception, ….

    kernel.Bind(
        x => x.FromThisAssembly()
              .SelectAllClasses()
              .InNamespace("Services")
              .BindToAllInterfaces()
              .Configure(b => b.InSingletonScope()));
    kernel.Bind(
        x => x.FromThisAssembly()
              .SelectAllClasses()
              .InNamespace("Repositories")
              .BindToAllInterfaces());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay so ive been reading up on and working with SQLite recently (someone on
I've recently been lifted out of the .Net world into the Java world and
Okay, I think I'm just making a stupid mistake here, but I want to
okay so this is probably a soft pitch question for sombody, but I want
Okay, I have done a bit of searching online and found this thread, but
Okay I might be entirely off track now but here goes: Our webshop offers
Recently I've been thinking about performance difference between class field members and method variables.
I've been teaching myself python recently an came across and example where str.endswith took
I programmed a CMS that has a log of who has recently logged into
Okay this is frustrating me to no end. I recently coded a page in

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.