mercredi 21 juin 2017

Is this code fragmentation normal when using DI

we're in the process of trying to use DI in our application. We have a Docking/MDI application (think Visual Studio). To explain the 'problem' I'm seeing I'll try to simplify, the real application obviously has a lot more logic involved.

  • We have a DockingItemService which manages all the tools and documents. Since tools (solution explorer, toolbox, search,...) can only exist once they are created in the DockingItemService and only made public by properties.
  • We also have a NavigationService which allows navigating around. Comparable to VS where double clicking a source file would open it, or just focus it when already open, etc.

We now start to doubt the design because we start getting chicken/egg problems. Two examples:

  • The NavigationService needs the DockingItemService to be able to get a document an focus it. The solution explorer needs the NavigationService to be able to open/focus a document. To create the solution explorer the DockingItemService again needs the Navigation service.
  • The search tool item will create a bunch of search results. Each search result should allow to navigate to the corresponding line in the corresponding document. Again, search result needs NavigationService, thus search tool item needs it, ...

From reading around I think I know how to solve this using two simple factories (simple factory pattern). ToolItemFactory on which DockingItemService depends, and SearchResultFactory on which search tool item depends. ToolItemFactory will depende on SearchResultFactory since search tool item is created in ToolItemFactory.

I have many questions because I'm pretty new to all this but I'll try to focus on:

  • Would this be a better design or am I still not getting it?
  • All SOLID examples seem to make sense until reality hits. I am now introducing two extra factories that make it to the composition root. None of them will or can be used elsewhere. It feels like I am creating code just to get the dependency injection to work and not pass through the dependencies. This feels like cluttering logic all over which makes it again somehow hard to follow/maintain. Is this a logic consequence? Necessary evil?

Note: I am aware that factories help with 'open/closed' principle. But that doesn't make any sense here. DockingItemService will need to know about every toolitem. And SearchResultFactory will only make one specific type of instances.

Jef

Aucun commentaire:

Enregistrer un commentaire