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 (eg IGreenBoxView , IRedImageView <-- JConcreteGreenBoxView , JConcreteRedImageView ) without using any kind of service locator pattern (eg requesting from the IoC again)
  • Depending on that, create a new model, presenter and view instance
  • Similary, instanciate a new concrete dialog box, eg 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?


    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. eg, in Ninject:

    Bind<IControlFactory>().ToFactory();
    

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


    For those looking to do similar things with Unity (instead of Ninject), I have created an extension that allows you to create factories without even having to declare the interfaces: UnityMappingFactory@GitHub

    You just add the mappings right where you register the classes during the normal bootstrapping process...

    //make sure to register the output...
    container.RegisterType<IImageWidgetViewModel, ImageWidgetViewModel>();
    container.RegisterType<ITextWidgetViewModel, TextWidgetViewModel>();
    
    //define the mapping between different class hierarchies...
    container.RegisterFactory<IWidget, IWidgetViewModel>()
    .AddMap<IImageWidget, IImageWidgetViewModel>()
    .AddMap<ITextWidget, ITextWidgetViewModel>();
    

    Then you just declare the mapping factory interface in the constructor for CI and use its Create() method...

    public ImageWidgetViewModel(IImageWidget widget, IAnotherDependency d) { }
    
    public TextWidgetViewModel(ITextWidget widget) { }
    
    public ContainerViewModel(object data, IFactory<IWidget, IWidgetViewModel> factory)
    {
        IList<IWidgetViewModel> children = new List<IWidgetViewModel>();
        foreach (IWidget w in data.Widgets)
            children.Add(factory.Create(w));
    }
    

    As an added bonus, any additional dependencies in the constructor of the mapped classes will also get resolved during object creation. (Also shared as an answer to this Stackoverflow post)

    链接地址: http://www.djcxy.com/p/58090.html

    上一篇: 在jinja2中为包含父文件的文件使用块

    下一篇: IoC / DI容器,工厂和运行时类型创建