jeudi 10 septembre 2015

Removing a Service Locator by a better mechanism

I'm building a GUI framework and I currently have a lot of lines like

var renderer = locator.GetService<IRenderer>();
var input = locator.GetService<IMouseInput>();

Most of the services I'm retrieving using the Service Locator are core services, I mean, services that are inherently part of what is considered the basics.

I have thought about using constructor injection, but some annoyances arise.

  • I will have to modify almost every constructor to have its dependencies injected, and that means it will receive even the most basic "services". I'm afraid of having 4 or 5 arguments in each constructor.
  • But another thing that is even worse is that end users of the framework will not have parameterless constructors. Do you imagine that creating a TextBox required you you pass all the dependencies it really needs?

The end user could be overwhelmed with new TextBox(dep1, dep2, dep3).

So, is there a good way to remove the Service Locator completely without making it too complex?

Aucun commentaire:

Enregistrer un commentaire