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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:07:00+00:00 2026-06-12T19:07:00+00:00

I’m trying to create a generic factory I can call to instantiate a class

  • 0

I’m trying to create a generic factory I can call to instantiate a class and its dependencies using Ninject constructor injection. It seems to work great, but its not sitting well with me, I don’t know if that’s because its the first time I’ve used generics and an IoC container, but I think my approach is flawed. Rather than explain ill just dump my simple test console app.

Farm.cs

class Farm
{
    private readonly IAnimal _animal;
    private readonly IVehicle _vehicle;

    public Farm(IAnimal animal, IVehicle vehicle)
    {
        _animal = animal;
        _vehicle = vehicle;
    }

    public void Listen()
    {
        _animal.Speak();
        _vehicle.Run();
    }
}

program.cs

class Program
{
    static void Main(string[] args)
    {
        var farm = new NinjectFactory<Farm>().GetInstance();

        farm.Listen();

        Console.Read();
    }
}

NinjectFactory.cs

class NinjectFactory<T>
{
    public T GetInstance()
    {
        var kernel = new StandardKernel(new IoCModule());

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

NinjectModule.cs

class IoCModule : NinjectModule 
{
    public override void Load()
    {
        Bind<IAnimal>().To<Dog>();
        Bind<IVehicle>().To<Tractor>();
    }
}

Any ideas/feedback would be greatly appreciated, thanks.

  • 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-12T19:07:01+00:00Added an answer on June 12, 2026 at 7:07 pm

    I’m trying to create a generic factory I can call to instantiate a
    class and its dependencies using Ninject constructor injection

    The Ninject Kernel (or any container for that matter) IS a generic factory. You are hiding that generic factory behind a generic factory. You can simply do this:

    private static StandardKernel kernel;
    
    static void Main(string[] args)
    {
        Bootstrap();
    
        // Resolve the application's root type
        // by using the container directly.
        var farm = kernel.Get<Farm>();
    
        // Operate on the root type
        farm.Listen();
    
        Console.Read();
    }
    
    private static Kernel Bootstrap()
    {
        kernel = new StandardKernel();
    
        kernel.Bind<IAnimal>().To<Dog>();
        kernel.Bind<IVehicle>().To<Tractor>();
        kernel.Bind<Farm>().ToSelf();
    }
    

    If your idea is to use your generic factory to hide the container from the application, this means that application code depends on that static factory. This is a big no-no. All types should be designed around constructor injection, and injecting a generic factory is the same as injecting the kernel itself into a type. This leads to code that is hard to maintain, hard to test, and hard to be verified.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm trying to select an H1 element which is the second-child in its group
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the

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.