mercredi 16 août 2017

Best Practices around Dependency Injection, using a Container

I have a question regarding architecture and dependency injection. I have been reading questions around SO but I did not manage to find the information needed to clarify best practices and how to use a DI framework and container to then test the code.

It is a general architecture question, but I would add some context to better explain my issue understanding it, under iOS:

Context

In iOS platform the application works by showing to the user a ViewController.

Each ViewController can react to user inputs, instantiate a child ViewController and do a segue to it to show it

With a bit of architecture, you can setup some data binding and ad adapter can return ViewController to display in particular sections of the screen.

DI Container

To my understanding the idea of a DI container is to create the graph of dependencies in the app in the same place and forget about it.

The container will later on be used to get the various needed objects without the need to worry about its child dependencies.

Main question

The main question is about what is the intended idea of use of a container, in order to create better isolated modules that are testable

Should a Container:

  • be passed around the application as a dependency itself, to be reused
  • should it be accessed directly from the "main" of the application as a singleton and access directly
  • should it define the whole graph, so that it is only used once when setting the first "controller" in the "main" of the application

Clarifications

1

Should we split the dependencies between controllers? As a consequence: should a controller have the Container itself as a dependency? Or should a controller access it directly as a singleton and resolve the child controller that it is trying to show

3

Should the graph in the container at the root of the app define every single dependency of the app, and the Container should only be used to resolve things once (the main controller)? So as a consequence: Should a controller contain as a dependency also all the child controllers that it will segue to?

Data Binding

What about adapters that are using for databinding? They need to instanciate controllers. Does the adapter have the Container as a dependency, or access it directly? Then how can we test it? Should we instead create a Factory and delegate to the factory the creation of controllers? Then how do we test the factory 😭 ?

Aucun commentaire:

Enregistrer un commentaire