Pages

Labels

Monday, 9 December 2013

Dependency Inversion in Go - Introduction

Introduction

The fact that you have landed on this page is a strong indication that you are aware of the existence of the go language.

The language itself was originally created by three Google developers back in 2007 (though officially released to the world in 2009) in their spare time, but it has since gone on to be one of the hottest new languages on the market and is even used by Google to power some of their own services.

Although a relatively young language, there is a big community of developers using it, and some of these are approaching the language in very much a craftsman like manner, and these developers are looking for ways to use principles like SOLID with it.

This is not a post about the merits of SOLID in go, what I want to cover here is one aspect of SOLID, Dependency Inversion Principle, and how you can apply DIP to go code.

Requirements of Dependency Inversion

The principle of Dependency Inversion, is to remove hardwired dependencies from any code that you write, further more functions, structs, and even packages should only depend on abstractions of other modules, and not the module itself.

This can be achieved in many ways, and some of the standard methods are:
  • Separated Interface Pattern
  • Dependency Injection
  • Inversion of Control
  • Adapter Pattern
In this group of articles I will demonstrate a simple method of implementing Inversion of Control, before moving on to Dependency Injection. The focus on these two methods is a result of the 'relative ease' of implementing IoC, and the 'availability' of DI in go when compared with the other options available.

DI Options

So what are my options as far as Dependency Injection is concerned in the go language. Well this is not an exhaustive review of all of the libraries, and if you know of others that you think are worth exploring please feel free to let me know, but at the time of writing this post the three packages below stood out as promising.
  • go-inject
  • go.inject
  • depinject
When searching for Dependency Injection packages the first two come up time and time again, and unless you pay close attention you might be forgiven for thinking that they are one in the same package.

Over the next couple of posts I will go through each option detailed above, give some background about each, as well as, with the use of a simple example discuss the good and bad points of each option.

Next: Simple IoC >>

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

No comments:

Post a Comment

 
 
Blogger Templates ReadABlog.com