Types of Dependency Injection #513
StevenACoffman
started this conversation in
Ideas
Replies: 1 comment
-
Hi, I didn't know about github discussion and didn't get any notifications 😂 I'll give this a proper ready later |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I found the dependency injection section rather sparse and hard to apply in practice, which made it hard for me to test drive code. I wonder if more information about different dependency injection types or techniques and when to use them could be provided?
I found an article that described the different types to be helpful. I've paraphrased it a bit below:
Dependency injection
Dependency injection is a form of inversion of control, but is probably better known as a solution to the dependency inversion principle which is the letter “D” in SOLID. Both principles touch on writing well-architected, modularized programs.
Dependency injection helps through modularization, by injecting dependencies into dependents (clients) instead of letting them ask for or create dependencies themselves.
Dependencies are stateful : The only reason to consider treating something as a dependency is if it had some sort of a state. For example, a database has its connection pool as its state. This also means there should be some kind of initialisation involved before you can use a dependency
A dependency is characterized by its contract. The module using it should not know about its implementation, or internal state.
Constructor Injection
A simple dependency injection in Go looks like this:
In this case, the repository, instead of creating the dependency itself, receives a database connection through a “constructor” function. This is called a constructor injection and it’s probably the purest form of dependency injection. Constructor injection should be the main way that you do dependency injection, whenever a dependency is required in order to work properly.
Property Injection (aka Setter Injection)
One is the so-called property injection:
In this case, the dependency is the HTTP handler, which is passed to a struct member field (“property”).
Use property injection when a dependency is optional and/or when a dependency can be changed after the class is instantiated. In a language like Go, this is usually not the safest dependency injection, because there is nothing that prevents concurrent access during subsequent writes, both of which can lead to unexpected behavior. In languages like C#, class members/properties can be made write-once/read-only which makes this kind of injection safer.
Method Injection
The other type of injection in the http.Server struct is method injection:
The dependency here is the listener which, again, instead of being created by the server itself (unlike in ListenAndServe) is passed via a method argument.
Method injection should be used when the dependency could change with every use, or at least when you can’t be sure which dependency will be needed at the point of use.
Beta Was this translation helpful? Give feedback.
All reactions