GithubHelp home page GithubHelp logo

xdi's Introduction

OpenMix 出品:https://openmix.org

Mix XDI

DI、IoC 容器

DI, IoC container

Overview

一个创建对象以及处理对象依赖关系的库,该库可以实现统一管理依赖,全局对象管理,动态配置刷新等。

Installation

go get github.com/mix-go/xdi

Quick start

通过依赖配置实例化一个单例

package main

import (
    "github.com/mix-go/xdi"
)

type Foo struct {
}

func init() {
    obj := &xdi.Object{
        Name: "foo",
        New: func() (interface{}, error) {
            i := &Foo{}
            return i, nil
        },
    }
    if err := xdi.Provide(obj); err != nil {
        panic(err)
    }
}

func main() {
    var foo *Foo
    if err := xdi.Populate("foo", &foo); err != nil {
        panic(err)
    }
    // use foo
}

Reference

依赖配置中引用另一个依赖配置的实例

package main

import (
    "github.com/mix-go/xdi"
)

type Foo struct {
    Bar *Bar
}

type Bar struct {
}

func init() {
    objs := []*xdi.Object{
        {
            Name: "foo",
            New: func() (interface{}, error) {
                // reference bar
                var bar *Bar
                if err := xdi.Populate("bar", &bar); err != nil {
                    return nil, err
                }

                i := &Foo{
                    Bar: bar,
                }
                return i, nil
            },
        },
        {
            Name: "bar",
            New: func() (interface{}, error) {
                i := &Bar{}
                return i, nil
            },
            NewEverytime: true,
        },
    }
    if err := xdi.Provide(objs...); err != nil {
        panic(err)
    }
}

func main() {
    var foo *Foo
    if err := xdi.Populate("foo", &foo); err != nil {
        panic(err)
    }
    // use foo
}

Refresh singleton

程序执行中配置信息发生变化时,可以刷新单例的实例

obj, err := xdi.Container().Object("foo")
if err != nil {
    panic(err)
}
if err := obj.Refresh(); err != nil {
    panic(err)
}

License

Apache License Version 2.0, http://www.apache.org/licenses/

xdi's People

Contributors

onanying avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.