Alice: an additive dependency injection container for Golang

magic003
magic003
Apr 22, 2017 · 5 min read

What is a preferable way?

No code pollution

No partial object

Easy to navigate the dependency graph


Why a new DI container?


How it works?

Define modules

type ExampleModule struct {
alice.BaseModule
Foo Foo `alice:""`
Bar Bar `alice:"Bar"`
Baz Baz
}

func (m *ExampleModule) InstanceX() X {
return X{m.Foo}
}

func (m *ExampleModule) InstanceY() Y {
return Y{m.Baz}
}

Create container

m1 := &ExampleModule1{}
m2 := &ExampleModule2{...}
container := alice.CreateContainer(m1, m2)

Retrieve instances

instanceX := container.InstanceByName("InstanceX")instanceY := container.Instance(reflect.TypeOf((Y)(nil)))

Limitations


Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade