ioc

package
v0.0.0-...-8e0b234 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2024 License: OSL-3.0 Imports: 2 Imported by: 0

README

ioc 简单实现

ioc设计

功能:

  1. 对象托管:
    • 对象的注册
    • 对象获取
  2. 对象统一管理:
    • 配置(不做实现)
    • 初始化
    • 销毁

基于Map来实现Ioc容器

单容器设计

基于Map来实现一个这样的 对象托管容器

container = map[string]any

注册的对象必须进行接口约束, 要求他必须实现 对象统一管理的方法:

// 对象统一管理
type IocObject interface {
    Init() error
    Destory() error
}


type TestStruct struct {

}

func (t *TestStruct) Init() error {

}

func (t *TestStruct) Destory() error {

}

func (t *TestStruct) XXX() error {

}

// container = map[string]IocObject
ioc.Registry("service name", &TestStruct{})

// 启动的时候, 完成对象的统一管理, 循环容器里面的所有对象, 调用他们的Init方法
ioc.Init()

一个map, 不允许重名的, 有一个模块叫token:

  • TokenServiceImpl: Controller
  • TOkenApiHandler: Api
// 注册控制器
ioc.Controller.Registry("module name", &TokenServiceImpl{})
ioc.Api.Registry("module name", &TOkenApiHandler{})

根据程序设计,对这些对象的 职责约束, 将容器进行分区:

  • Api: 负责注册 Api实现类型的对象
  • Controller: 负责注册服务实现类的对象
  • Config: 配置对象, db,kafak,redis
  • Default: 预留区域
多容器
api_contailer = map[string]IocObject
controller_container = map[string]IocObject
实现ioc

封装Container

func TestContainerGetAndRegistry(t *testing.T) {
	c := ioc.NewContainer()
	c.Registry("TestStruct", &TestStruct{})
	t.Log(c.Get("TestStruct"))

	// 断言使用
	c.Get("TestStruct").(*TestStruct).XXX()
}

封装Manager

func TestManageGetAndRegistry(t *testing.T) {
	ioc.Controller().Registry("TestStruct", &TestStruct{})
	t.Log(ioc.Controller().Get("TestStruct"))

	// 断言使用
	ioc.Controller().Get("TestStruct").(*TestStruct).XXX()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Destory

func Destory() error

func Init

func Init() error

func RegistryGinApi

func RegistryGinApi(rr gin.IRouter)

Types

type Container

type Container struct {
	// contains filtered or unexported fields
}

ioc 容器具体实现

func Api

func Api() *Container

ioc.Api().Restiry ioc.Api().Get()

func Config

func Config() *Container

ioc.Config().Restiry ioc.Config().Get()

func Controller

func Controller() *Container

ioc.Controller().Restiry() ioc.Controller().Get()

func NewContainer

func NewContainer() *Container

func (*Container) Get

func (c *Container) Get(name string) any

获取的值, 由使用者进行约束,或者断言 ioc.Get("moduel name").(*TestService)

func (*Container) Registry

func (c *Container) Registry(name string, obj Object)

对象注册功能

type GinApi

type GinApi interface {
	// 基础约束
	Object
	// 额外约束
	// ioc.Api().Get(token.AppName).(*api.TokenApiHandler).Registry(rr)
	Registry(rr gin.IRouter)
}

type NamespaceContainer

type NamespaceContainer struct {
	// contains filtered or unexported fields
}

基于这个构建多空间的container

func (*NamespaceContainer) Destory

func (c *NamespaceContainer) Destory() error

func (*NamespaceContainer) Init

func (c *NamespaceContainer) Init() error

func (*NamespaceContainer) RegistryGinApi

func (c *NamespaceContainer) RegistryGinApi(rr gin.IRouter)

遍历所有的对象, 帮忙完成Api的注册 由ioc调用对象提供的 Registry方法,来吧模块的api 注册给gin root router

type Object

type Object interface {
	Init() error
	Destory() error
}

对象方法约束

Jump to

Keyboard shortcuts

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