Dependency Injection

etienne_marais

Honorary Master
Joined
Mar 16, 2008
Messages
15,093
I can't quite distinguish Dependency Injection (DI) from the Visitor design pattern. Is DI simply a form of the Visitor pattern, if so what qualifies it as DI ?
 

Sysem

Expert Member
Joined
Mar 26, 2009
Messages
1,891
I'm not familiar with the Visitor Pattern, but as far as I can tell its not related at all. Dependancy Injection is for managing object dependancies. It provides objects with objects it needs, instead of constructor the objects itself. Look at Google's Guava library for how it works. The Visitor Pattern is more for providing an easy way of adding a method to a class/interface without having to modify any subclasses implementations.

I may a tad bit off on these (I've only just started wrapping my head around the Dagger2 DI library), but here are some StackOverflow posts on each of the patterns:

Visitor Pattern
Dependancy Injection
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
Both fall under the greater concept of Inversion of Control (IOC) meaning that the problems addressed are similar, but they differ on the specifics.

In much the same way as building an application around a specific vendor's web services can be bad:
  • "what happens if they discontinue the service"
  • "increase their prices".
To circumvent this, you would design a generic implementation that makes it easy to switch services providers (specifically avoiding an lock-in features), that way if they increase their prices, you have the option to move your services to another more affordable provider.

Dependency injection is a technique to accommodate this within your object graph i.e. to avoid building something that is brittle and difficult to test. Designing for flexibility means you are accommodating change in the design i.e. substitution of one related services for another. DI is great for things like framework flexibility, plugins, or to even help to simplify unit testing... here's a simple example http://typhoonframework.org

The Really Short Version
Dependency injection means giving an object its instance variables as parameters.

Here's more specific detail:
 
Last edited:

etienne_marais

Honorary Master
Joined
Mar 16, 2008
Messages
15,093
Thanks guys.

As I have it then DI will typically assign a 'service' object/instance to a member field of the 'consumer', the consumer will then call a method on the service allowing for simplistic change of service. With visitor on the other hand, an object will Accept() a visitor object and call a method (Visit()) on this visitor allowing for extension / adding of operations of/on the object without having to modify the original object.
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
Thanks guys.

As I have it then DI will typically assign a 'service' object/instance to a member field of the 'consumer', the consumer will then call a method on the service allowing for simplistic change of service. With visitor on the other hand, an object will Accept() a visitor object and call a method (Visit()) on this visitor allowing for extension / adding of operations of/on the object without having to modify the original object.
Be careful about a trying to summarize it too rigidly, because you can replicate the behavior of the Visitor pattern without the Visit / Accept terminology.

You should rather try to understand the problems that these patterns help to solve:
  • Visitor - Related set of elements, that share a common set of algorithms, for example: useful to avoid code duplication and /or the need to modify the elements when adding more features.
  • Dependency Injection - Separates the code for use of services from the construction of services, for example: useful to avoid massive code changes when services are changed; helpful when you have similar services with dissimilar APIs (e.g. weather), the use objects would all support a common DI framework, expecting compliant instances as parameters. This is usually achieved by creating a wrapper class to make the new service API comply with what your DI framework expects. The wrapper class in this case deals with build up and tear down processes (construction) specific to that API, translating this into a common set of actions for the DI object.
 
Last edited:

Spacerat

Expert Member
Joined
Jul 29, 2015
Messages
1,328
I addition to decoupling the creation of a service from the consumer of the service, the consumer advertises its dependencies via the ctor (you usually inject through the ctor). It means that when constructing a consumer, you know exactly what services it requires. I cannot recommend reading the book

https://www.amazon.com/Dependency-Injection-NET-Mark-Seemann/dp/1935182501

enough. It is a must-read. You will have so many a-ha moments througout the book.
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
Oh gawd, I was wondering when his name would show up.
They just can't help themselves. :D
Personally prefer GoF, re it was the first book I read on subject, and where much of it started.
 
Top