On Saturday, 1 November 2014 at 16:36:02 UTC, JN wrote:
I may be ignorant, can someone explain what's the difference between:

Container container;
container.register!(IGreeter, Greeter);
auto greeter = container.get!IGreeter();


auto greeter = new Greeter();


It's the fundamental way dependency injection containers are used. Instead of having complex dependencies on other resources you can just rely on the container to serve resources as needed.

For example if you have a class which needs the Greeter service you will either have to embed that service using composition or pass it into the class via a constructor or setter. A container is a single place where this and many other services can be retrieved. This also means that anything wanting to use the container is also dependent on it but this is sometimes less complicated than dealing with all the other dependencies individually. Also some dependencies have complicated rules for creating them which some dependency injection containers deal with, again removing this code from where the resource is actually being used.

Reply via email to