Pages

Labels

Monday, 9 December 2013

Dependency Inversion in Go - go-inject

GoDoc http://godoc.org/code.google.com/p/go-inject
Code https://code.google.com/p/go-inject/
License Apache License, Version 2.0

This library has been created, and open sourced by Jason Kinner, who although he does work for Google, is not associated with the go project.

The style of dependency injection that go.inject uses is reflection.

Example

The example below is based on the same imaginary car sales system as used in the previous post. 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 four key areas, these being the interfaces, the concrete implementations, injector tags, 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 object returned by the GetCarFactory struct to a new factory instead of the FordFactory used.

Loading ....

Summary

In summary, the go-inject example above does provide a relatively straight forward option to DIP, but like the IoC option previous, it isn't perfect, and beyond that is not as easy to extend and improve. As before we would ideally want to setup object mappings at runtime and not build time, unlike the IoC post it wouldn't be as simple to achieve, however time permitting I may look at branching the library and include a configuration driven approach.

Would I recommend this as a path to implementing SOLID principles? Although not as simple to do, it be achieved, though I may be tempted to use it on smaller projects only. 

Links

Dependency Inversion in Go - Introduction
Dependency 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

1 comment:

 
 
Blogger Templates ReadABlog.com