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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:44:22+00:00 2026-05-16T08:44:22+00:00

I try to create aplication with Ninject. I have one MainForm and dialog form

  • 0

I try to create aplication with Ninject. I have one MainForm and dialog form for settings. How should i implement this? Should MainForm have a Kernel instance and then create SettingsForm with Get<SettingsForm>() method? Or should MainForm constructor have parameter with SettingsForm instance?

I tried find some example of WinForm application with ninject, but i found only some ASP.NET applications which are useless in Visual C# 2008 express.

  • 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-16T08:44:23+00:00Added an answer on May 16, 2026 at 8:44 am

    I would make the dependency be at the form-to-form level. You want to have something in between that.

    namespace Example
    {
        public class SettingsRepository
        {
            public SettingsRepository()
            {
    
            }
        }
        public class SettingsForm
        {
            private SettingsRepository _settingsRepository;
            public SettingsForm( SettingsRepository settingsRepository )
            {
                _settingsRepository = settingsRepository;
    
            }
        }
        public class MainForm
        {
            private SettingsRepository _settingsRepository;
            private Func<SettingsForm> _createSettingsForm;
            public MainForm( Func<SettingsForm> createSettingsForm, SettingsRepository settingsRepository )
            {
                _createSettingsForm = createSettingsForm;
                _settingsRepository = settingsRepository;
    
            }
        }
    }
    

    Then you inject a Func<SettingsForm> into your class to remove the direct usage of the container / Kernel from your code (if you’re doing inline Get calls all over the place, you’re doing Service Location, which is a different thing to DI entirely).

        public class ExampleNinjectModule : NinjectModule
        {
            public override void Load()
            {
                Bind<Func<SettingsForm>>().ToMethod( context => () => context.Kernel.Get<SettingsForm>()  );
            }
        }
    

    Another approach is to add a Kernel to your constructor args (Ninject automatically resolves it), but that quickly becomes a mess in general.

    I tried a quick search for samples, but sadly didnt find anything quickly in the WinForms space. I’d suggest perhaps looking for WPF examples instead.

    Bottom line is you wont go far wrong if you:

    1. stick with Constructor Injection, and avoiding direct usage of Kernel or container attributes in your real code as much as possible
    2. Dont use a Global Kernel and/or Service Location

    Update Sep 12: These days one would definitely employ Ninject.Extensions.Factory to manage the factory (i.e., most of the code would above would be auto-genned behind the scenes)

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

Sidebar

Related Questions

I try to create login form in web application. in JSP page I can
I try to create junit test for my GWT application. The application uses MathJax
When I try to create pipeline that uses H264 to transmit video, I get
So I get thrown an exception right when I try to create a new
i use zend framework 1.10 and i have a script under scripts library. i
i'm trying to create an application which connects to internet and consume web services
My application is using MEF to export some classes from an external assembly. These
I'm trying to get the current running version of Outlook or start up Outlook
I'm starting to dip my toes in the world of network programming and I've

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.