All Projects → magic003 → alice

magic003 / alice

Licence: MIT license
An additive dependency injection container for Golang.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to alice

Swinjectstoryboard
Swinject extension for automatic dependency injection via Storyboard
Stars: ✭ 211 (+313.73%)
Mutual labels:  dependency-injection, ioc-container
Disco
PSR-11 compatible Dependency Injection Container for PHP.
Stars: ✭ 135 (+164.71%)
Mutual labels:  dependency-injection, ioc-container
Singularity
A extremely fast ioc container for high performance applications
Stars: ✭ 63 (+23.53%)
Mutual labels:  dependency-injection, ioc-container
Vue.js With Asp.net Core Sample
This provides a sample code using vue.js running on ASP.NET Core
Stars: ✭ 44 (-13.73%)
Mutual labels:  dependency-injection, ioc-container
Typhoon
Powerful dependency injection for Objective-C ✨✨ (https://PILGRIM.PH is the pure Swift successor to Typhoon!!)✨✨
Stars: ✭ 2,711 (+5215.69%)
Mutual labels:  dependency-injection, ioc-container
Simplify
Simplify is a set of .NET libraries that provide infrastructure for your applications. DI and mocking friendly.
Stars: ✭ 52 (+1.96%)
Mutual labels:  dependency-injection, ioc-container
React Ioc
Hierarchical Dependency Injection with new React 16 Context API
Stars: ✭ 133 (+160.78%)
Mutual labels:  dependency-injection, ioc-container
Weaver
Dependency Injection framework for Swift (iOS/macOS/Linux)
Stars: ✭ 546 (+970.59%)
Mutual labels:  dependency-injection, ioc-container
Node Dependency Injection
The NodeDependencyInjection component allows you to standarize and centralize the way objects are constructed in your application.
Stars: ✭ 140 (+174.51%)
Mutual labels:  dependency-injection, ioc-container
Autofac.annotation
Autofac extras library for component registration via attributes 用注解来load autofac 摆脱代码或者xml配置和java的spring的注解注入一样的体验
Stars: ✭ 140 (+174.51%)
Mutual labels:  dependency-injection, ioc-container
Simpleinjector
An easy, flexible, and fast Dependency Injection library that promotes best practice to steer developers towards the pit of success.
Stars: ✭ 966 (+1794.12%)
Mutual labels:  dependency-injection, ioc-container
Ioc
🦄 lightweight (<1kb) inversion of control javascript library for dependency injection written in typescript
Stars: ✭ 171 (+235.29%)
Mutual labels:  dependency-injection, ioc-container
Servicemanager
🔌 Most basic implementation of dependency injection container for JavaScript
Stars: ✭ 29 (-43.14%)
Mutual labels:  dependency-injection, ioc-container
Poodinis
A dependency injection framework for D with support for autowiring.
Stars: ✭ 57 (+11.76%)
Mutual labels:  dependency-injection, ioc-container
Di
Dependency Injection and IoC framework for PHP
Stars: ✭ 5 (-90.2%)
Mutual labels:  dependency-injection, ioc-container
Python Dependency Injector
Dependency injection framework for Python
Stars: ✭ 1,203 (+2258.82%)
Mutual labels:  dependency-injection, ioc-container
Typescript Ioc
A Lightweight annotation-based dependency injection container for typescript.
Stars: ✭ 427 (+737.25%)
Mutual labels:  dependency-injection, ioc-container
Swinject
Dependency injection framework for Swift with iOS/macOS/Linux
Stars: ✭ 4,958 (+9621.57%)
Mutual labels:  dependency-injection, ioc-container
Microresolver
Extremely Fast Dependency Injection Library.
Stars: ✭ 138 (+170.59%)
Mutual labels:  dependency-injection, ioc-container
Container
A lightweight yet powerful IoC container for Go projects
Stars: ✭ 160 (+213.73%)
Mutual labels:  dependency-injection, ioc-container

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].