All Projects → ljun20160606 → di

ljun20160606 / di

Licence: MIT license
Dependency Injection

Programming Languages

go
31211 projects - #10 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to di

tsed
📐 Ts.ED is a Node.js and TypeScript framework on top of Express to write your application with TypeScript (or ES6). It provides a lot of decorators and guideline to make your code more readable and less error-prone.
Stars: ✭ 2,350 (+21263.64%)
Mutual labels:  dependency-injection
AvengersChat
💙 Android sample Avengers chat application using Stream Chat SDK based on MVVM (ViewModel, Coroutines, Room, Hilt, Repository) architecture.
Stars: ✭ 350 (+3081.82%)
Mutual labels:  dependency-injection
AnnotationInject
Compile-time Swift dependency injection annotations
Stars: ✭ 40 (+263.64%)
Mutual labels:  dependency-injection
iocgo
A lightweight Inversion of Control (IoC) (Dependency Injection) container for Golang
Stars: ✭ 36 (+227.27%)
Mutual labels:  dependency-injection
last fm
A simple app to demonstrate a testable, maintainable, and scalable architecture for flutter. flutter_bloc, get_it, hive, and REST API are some of the tech stacks used in this project.
Stars: ✭ 134 (+1118.18%)
Mutual labels:  dependency-injection
ioc-ts
Inversion of control container on TypeScript
Stars: ✭ 14 (+27.27%)
Mutual labels:  dependency-injection
Instant-Weather
An Android weather application implemented using the MVVM pattern, Retrofit2, Dagger Hilt, LiveData, ViewModel, Coroutines, Room, Navigation Components, Data Binding and some other libraries from the Android Jetpack.
Stars: ✭ 677 (+6054.55%)
Mutual labels:  dependency-injection
dargo
Dependency Injection for GO
Stars: ✭ 26 (+136.36%)
Mutual labels:  dependency-injection
vesselize
⛵ A JavaScript IoC container that works seamlessly with Vue.js and React.
Stars: ✭ 22 (+100%)
Mutual labels:  dependency-injection
microcore
.NET Core framework for inter-service communication
Stars: ✭ 24 (+118.18%)
Mutual labels:  dependency-injection
trew
A fast and very lightweight dependency injection library for Java 8+
Stars: ✭ 34 (+209.09%)
Mutual labels:  dependency-injection
awilix-express
Awilix helpers/middleware for Express
Stars: ✭ 100 (+809.09%)
Mutual labels:  dependency-injection
swift-di-explorations
Functional DI explorations in Swift
Stars: ✭ 28 (+154.55%)
Mutual labels:  dependency-injection
Kata-Dagger2-Android
Kata to practice Dependency injection using Dagger 2.
Stars: ✭ 21 (+90.91%)
Mutual labels:  dependency-injection
sanic-ext
Extended Sanic functionality
Stars: ✭ 26 (+136.36%)
Mutual labels:  dependency-injection
ashley
Ashley is a dependency injection container for JavaScript.
Stars: ✭ 23 (+109.09%)
Mutual labels:  dependency-injection
Container
🚀 PHP Service Container with fast and cachable dependency injection.
Stars: ✭ 28 (+154.55%)
Mutual labels:  dependency-injection
kaop
Advanced OOP Library with createClass, inheritance, providers, injectors, advices which enables handy Inversion of Control techniques
Stars: ✭ 40 (+263.64%)
Mutual labels:  dependency-injection
Components.js
🧩 A semantic dependency injection framework
Stars: ✭ 34 (+209.09%)
Mutual labels:  dependency-injection
NoMansWallpaperApp
Looking for your next No Man's Sky wallpaper?
Stars: ✭ 35 (+218.18%)
Mutual labels:  dependency-injection

DI(依赖注入)Golang 实现 😋

🔥 快速入门 内容目录


功能特性

  • 根据注解依赖注入
  • 根据名称获取实体
  • 自定义生命周期

快速开始

下载

$ go get github.com/ljun20160606/di

创建一个main文件

package main

import (
	"github.com/ljun20160606/di"
)

func init() {
	ducks := new(Ducks)
	di.Put(ducks)
}

type Duck interface {
	Quack()
}

type Ducks struct {
	Duck Duck `di:"*"`
}

type DarkDuck struct{}

func (*DarkDuck) Quack() {
	panic("implement me")
}

func main() {
	duck := new(DarkDuck)
	di.Put(duck)

	di.Start()

	ducks := di.GetWithName("main.Ducks").(*Ducks)
	if ducks.Duck.(*DarkDuck) != duck {
		panic("error")
	}
}

内容

DI

放入容器

package main

import "github.com/ljun20160606/di"

func main() {
	name := "duck"
	// 只支持指针类型
	di.Put(&name)
}

自动注入

di:"*"代表根据类型自动注入

package main

import (
	"github.com/ljun20160606/di"
)

type Duck struct {
	Name *string `di:"*"`
}

func main() {
	name := "duck"
	duck := Duck{}
	di.Put(&name)
	di.Put(&duck)
	// 开始依赖注入
	di.Start()
}

主动从容器中获取

name可以根据日志获得,规则为package+typeName

package main

import (
	"fmt"
	"github.com/ljun20160606/di"
)

func main() {
	name := "duck"
	// 只支持指针类型
	di.Put(&name)
	withName := *(di.GetWithName("string").(*string))
	fmt.Println(name == withName)
}

加载配置

config插件的关键字为#,可以自定义插件详细看文件plugin_config.go,共支持di.JSON | di.YAML | di.TOML

import (
	"fmt"
	"github.com/ljun20160606/di"
)

type Duck struct {
	Name string `di:"#.name"`
}

func main() {
	di.ConfigLoad(`name: duck`, di.YAML)
	//di.ConfigLoadFile("path", di.YAML)
	//di.ConfigLoadReader(reader, di.YAML)
	duck := Duck{}
	di.Put(&duck)
	di.Start()

	fmt.Println(duck.Name == "duck")
}

还支持根据前缀映射配置,如下取前缀为test.properties内的配置映射到对应的PrefixProperties结构体中

import (
	"fmt"
	"github.com/ljun20160606/di"
)

type Prefix struct {
	// 如果希望使用相同的PrefixProperties可以写为*PrefixProperties
	// 否则只会拷贝结构体本身
	// 如果想映射整个配置可以写`di:"#.{}"`
	Properties PrefixProperties `di:"#.{test.properties}"`
}

type PrefixProperties struct {
	Type        string
	Value       string
	SnakeFormat string `yaml:"snake_format"`
}

func main() {
    di.ConfigLoad(`
test:
  properties:
    type: prefix
    value: test
    snake_format: snake`, YAML)

    prefix := Prefix{}
    di.Put(&prefix)

    fmt.Println(prefix.Properties)
}
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].