Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IoC / DI containers, factories and runtime type creation

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?

like image 458
sunside Avatar asked Mar 09 '12 00:03

sunside


People also ask

What are the types of IoC containers explain them?

There are basically two types of IOC Containers in Spring: BeanFactory: BeanFactory is like a factory class that contains a collection of beans. It instantiates the bean whenever asked for by clients. ApplicationContext: The ApplicationContext interface is built on top of the BeanFactory interface.

What is IoC container?

IoC container is a framework for implementing automated dependency injection. It contains object creation for the longer ways to use and injects dependencies within the class.

What is IoC and DI in Spring?

Spring IoC (Inversion of Control) Container is the core of Spring Framework. It creates the objects, configures and assembles their dependencies, manages their entire life cycle. The Container uses Dependency Injection(DI) to manage the components that make up the application.

Which IoC container is best?

You can waste days evaluating IOC containers. The top ones are quite similar. There is not much in this, but the best ones are StructureMap and AutoFac. At SSW we use Autofac on most projects.


1 Answers

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

like image 141
Remo Gloor Avatar answered Sep 29 '22 20:09

Remo Gloor