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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:32:39+00:00 2026-05-29T06:32:39+00:00

I currently have a main class which might call another class which might call

  • 0

I currently have a main class which might call another class which might call 2 or 3 others. The main class might create a Windows form as well.

So at the moment we might have:

public class MainClass
{
  ...
  AnotherClass = new AnotherClass();
  fmMain myForm = new fmMain();
}

public class AnotherClass
{
  ...
  DatabaseClass dbClass = new DatabaseClass();
  FileClass fClass = new FileClass();
}

As I see it, after refactoring and putting in constructor interface dependencies I can use an IOC container.

The problem is my entry point is the main class so all I can see to do is have all the dependencies in the main class constructor and then pass these down into the other classes and forms.

The issue is this could get very messy and the main class might have 10 dependencies most of which used in other classes. Is there an alternative?

  • 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-29T06:32:40+00:00Added an answer on May 29, 2026 at 6:32 am

    Dependencies should be defined locally to where they are required i.e. the dependencies should be defined on the constructors of the types, then use an IoC container to resolve all types. In this way the IoC takes care of “passing these down into the other classes and forms”. When using an IoC container avoid using “new” whenever possible, and instead use the container to create instances. It will handle creation and injection of dependencies for you.

    For example (using Ninject), the following demonstrates a Form which defines its own dependencies. The container will supply and inject all dependencies recursively when used to resolve an instance of the ProductForm.

    Dependencies do not need to be injected into the entry-point class, but are specified as bindings in a module and then injected by the container when types such as Forms are resolved.

    public class Program
    {
        public Program()
        {
            IKernel kernel = new StandardKernel(new MainModule());
    
            Form form = kernel.Get<ProductForm>();
    
            form.Show();
        }
    }
    
    public class MainModule : NinjectModule
    {
        public override void Load()
        {
            Bind<ProductForm>().ToSelf();
            Bind<IProductService>().To<ProductService>();
            Bind<IProductDb>().To<IProductDb>();
        }
    }
    
    internal class ProductForm : Form
    {
        private readonly IProductService _productService;
    
        public ProductForm(IProductService productService)
        {
            _productService = productService;
        }
    }
    
    internal interface IProductService {}
    
    internal class ProductService : IProductService
    {
        private readonly IProductDb _productDb;
    
        public ProductService(IProductDb productDb)
        {
            _productDb = productDb;
        }
    }
    
    internal interface IProductDb {}
    
    internal class ProductDb : IProductDb {}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a class and I'm trying to create an easy GUI to
I currently have a functioning in-house Windows Forms application which extensively uses the DataGridView
I currently have 3 classes, a main class containing a GUI, and the fnameTxtField
i am currently creating a shooting game , currently i have a main class
i have this html menu: <ul id='main'> <li class='active current' id='lighty'> <a href='/'>Lighty</a> <ul>
this is my Main class which belongs to the server application! but it is
I am using .net 1.1. I have a session class in which I have
I currently have a small Java program which I would like to run both
I currently have a few unit tests which share a common set of tests.
It's the first time I use java Rmi*. I have a custom class which

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.