alice

package module
v0.0.0-...-2087f83 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2017 License: MIT Imports: 3 Imported by: 0

README

Alice

Release GoDoc Build Status Coverage Status Go Report Card

Alice is an additive dependency injection container for Golang.

Philosophy

Design philosophy behind Alice:

  • The application components should not be aware of the existence of a DI container.
  • Use static Go files to define the object graph.
  • Developer has the freedom to choose the way to initialize objects.

Install

$ go get github.com/magic003/alice

Usage

Alice is inspired by the design of Spring JavaConfig.

It usually takes 3 steps to use Alice.

Define modules

The instances to be managed by the container are defined in modules. There could be multiple modules organized by the functionality of the instances. Modules are usually placed in a separate package.

A typical module looks like this:

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}
}

A module struct must embed the alice.BaseModule struct. It allows 3 types of fields:

  • Field tagged by alice:"". It will be associated with the same or assignable type of instance defined in other modules.
  • Field tagged by alice:"Bar". It will be associated with the instance named Bar defined in other modules.
  • Field without alice tag. It will not be associated with any instance defined in other modules. It is expected to be provided when initializing the module. It is not managed by the container and could not be retrieved.

It is also common that no field is defined in a module struct.

Any public method of the module struct defines one instance to be intialized and maintained by the container. It is required to use a pointer receiver. The method name will be used as the instance name. The return type will be used as the instance type. Inside the method, it could use any field of the module struct to create new instances.

Create container

During the bootstrap of the application, create a container by providing instances of modules.

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

It will panic if any module is invalid.

Retreive instances

The container provides 2 ways to retrieve instances: by name and by type.

instanceX := container.InstanceByName("InstanceX")

instanceY := container.Instance(reflect.TypeOf((Y)(nil)))

It will panic either if no instance is found or if multiple matched types are found.

Example

A dummy example using Alice.

Documentation

Overview

Package alice provides an additive dependency injection container.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseModule

type BaseModule struct{}

BaseModule is an implementation of Module interface. It should be embedded into each module defined in the application.

A typical module is defined as follows:

type ExampleModule struct {
	alice.BaseModule
	Foo Foo `alice:""`      // associated by type
	Bar Bar `alice:"Bar"`   // associated by name
	URL string              // not associated. Provided by creating the module.
}

func (m *ExampleModule) Baz() Baz {
	return Baz{}
}

func (*BaseModule) IsModule

func (b *BaseModule) IsModule() bool

IsModule indicates it is a module.

type Container

type Container interface {
	// Instance returns an instance by type. It panics when no instance is found,
	// or multiple instances are found for the same type.
	Instance(t reflect.Type) interface{}
	// InstanceByName returns an instance by name. It panics when no instance is found.
	InstanceByName(name string) interface{}
}

Container defines the interface of an instance container. It initializes instances based on dependencies, and provides APIs to retrieve instances by type or name.

func CreateContainer

func CreateContainer(modules ...Module) Container

CreateContainer creates a new instance of container with specified modules. It panics if any of the module is invalid. This is the only way to create a container. Most applications call it only once during bootstrap.

type Module

type Module interface {
	// IsModule indicates if this is a module.
	IsModule() bool
}

Module is a marker interface for structs that defines how to initialize instances.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL