GoDoc | http://godoc.org/github.com/99designs/goodies/depinject |
Code | https://github.com/99designs/goodies/tree/master/depinject |
License | The MIT License (MIT) |
This library is part of a much larger library created, and open sourced by the guys over at 99designs, a provider of website design and hosting, as well as a major contributor to the open source community.
The style of dependency injection that depinject uses is reflection.
The style of dependency injection that depinject uses is reflection.
Example
The example below is based on the same imaginary car sales system as used in the previous posts. The idea is that when ordering a car the request must go to a car factory that builds that particular model.
The code is broken down into it's three key areas, these being the interfaces, the concrete implementations, and a method that defines the mapping of the objects and later uses those objects to get details of the car that has been 'built'.
As with the previous post a quick look at the code, and the main function, and it should be obvious that if you wanted to substitute the make and model of car, you can change the factory functions registered against the types. As with go.inject this package is that it's not as flexible as the first two packages and to truly achieve the functionality of the other examples a larger level of refactoring would be required.
The code is broken down into it's three key areas, these being the interfaces, the concrete implementations, and a method that defines the mapping of the objects and later uses those objects to get details of the car that has been 'built'.
As with the previous post a quick look at the code, and the main function, and it should be obvious that if you wanted to substitute the make and model of car, you can change the factory functions registered against the types. As with go.inject this package is that it's not as flexible as the first two packages and to truly achieve the functionality of the other examples a larger level of refactoring would be required.
Loading ....
Summary
In summary, as with go.inject, the example above does provide an option for DIP, but it is again neither straightforward, or perfect. As before we would ideally want to setup object mappings at runtime and not build time, in the case of depinject, achieving this is a not inconsiderable task, and with other options out there I don't see a reason for extending this library to include a configurable approach.
As for a recommendation, my personal view is that there are better options out there.
<< Previous: go.inject Next: Summary >>
As for a recommendation, my personal view is that there are better options out there.
<< Previous: go.inject Next: Summary >>
Links
Dependency Inversion in Go - IntroductionDependency Inversion in Go - Simple IoC
Dependency Inversion in Go - go-inject
Dependency Inversion in Go - go.inject
Dependency Inversion in Go - depinject
Dependency Inversion in Go - Summary
No comments:
Post a Comment