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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:20:58+00:00 2026-05-31T03:20:58+00:00

I recently learned about the DI frameworks Guice and Ninject and wanted to use

  • 0

I recently learned about the DI frameworks Guice and Ninject and wanted to use them in some of my new projects.

While I am familiar with general dependency injection concepts and know how to use those frameworks to construct object graphs, I struggle to apply IoC when it comes to dynamic application behavior.

Consider this example:

  • When the application starts, the main window will be shown.
  • When the user clicks the main panel, a context menu opens.
  • Depending on the user’ selection, a new user control will be created and shown at the mouse position.
  • Should the user eventually decide to close the application, a confirmation box will be shown and – upon confirmation – the main window will be closed.

While it is easy to wire the main window’s View to a Presenter/ViewModel and then bind that to the domain logic, I don’t understand how to cleanly (in the sense of IoC) achieve the following tasks:

  • Dynamically instantiate a concrete UI control (e.g. IGreenBoxView, IRedImageView <– JConcreteGreenBoxView, JConcreteRedImageView) without using any kind of service locator pattern (e.g. requesting from the IoC again)
    • Depending on that, create a new model, presenter and view instance
  • Similary, instanciate a new concrete dialog box, e.g. JOptionPane at runtime

I’ve seen some solutions using abstract factories but honestly didn’t fully understand them. It seems that such a solution would lead to exposing some of the (view domain’s, presenter domain’s, …) internal types to the construction root and, with that, to the whole world.

So – how do I do it right?

  • 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-31T03:20:59+00:00Added an answer on May 31, 2026 at 3:20 am

    If you can reuse the controls then you can do constructor injection where you use them. Otherwise you have to inject a factory:

    public interface IControlFactory 
    {
         IGreenBoxView CreateGreenBoxView();
         IRedImageView CreateRedImageView();
    }
    

    and inject it where you need to create this controls.

    The implementation goes to the container configuration. There you can inject the container to the implementation. Some containers provide to implement this factory automatically. e.g., in Ninject:

    Bind<IControlFactory>().ToFactory();
    

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

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

Sidebar

Related Questions

I recently learned about CTE's in SQL Server and am attempting to use it
Well, I recently learned about closures in Javascript. While i find it's concept truly
I just recently learned about self calling anonymous functions. Some snippets of code that
I recently learned about Recursive Common Table Expressions (CTEs) while looking for a way
I recently learned about the basic structure of the .docx file (it's a specially
I quite recently learned about the C++ classes friend keyword and the uses in
I've just recently learned about friend class concept in C++ (I've googled around for
Recently I learned about Werkzeug's interactive debugger in combination of django_extensions to debug Django
I recently asked about functional programs having no side effects, and learned what this
I learned HTML and CSS about 8 months ago, and recently, about 2 months

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.