Design – Desktop application, dependency injection

dependency-injectiondesignservice-locatoruser interface

I am thinking of applying a real dependency injection library to my toy C#/GTK# desktop application. I chose NInject, but I think this is irrelevant to my question.

There is a database object, a main window and several utility window classes. It's clear that I can inject the database into every window object, so here DI is useful. But does it make sense to inject utility window classes into other window classes?

Example: I have classes such as:

class MainWindow {…}
class AddItemWindow {…}
class AddAttachmentWindow {…}
class BrowseItemsWindow {…}
class QueryBuilderWindow {…}
class QueryBrowserWindow {…}
class PreferencesWindow {…}
…

Each of the utility classes can be opened from MainWindow. Some utility windows can also be opened from other utility windows. Generally, there might be a really complex graph of who can open whom. So each of those classes might need quite a lot of other window classes injected. I'm worried that such usage will go against the suggestion not to inject too many classes at once and become a code smell. Should I use some kind of a service locator object here?

Best Answer

I'd suggest a UtilityWindowOpener class. Its methods will open the various utility windows. Then you can just inject a UtilityWindowOpener anywhere you may need to open a utility window. The UtilityWindowOpener will then have the various utility windows injected (or possibly create them on demand, that's hidden behind the interface). It'll also bring all navigation logic into fewer locations.