framework

package
v0.0.0-...-bae97f3 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Container

type Container interface {
	// Bind 绑定一个服务提供者,如果关键字凭证已经存在,会进行替换操作,不返回error
	Bind(provider ServiceProvider) error

	// IsBind 关键字凭证是否已经绑定服务提供者
	IsBind(key string) bool

	// Make 根据关键字凭证获取一个服务的实例
	Make(key string) (interface{}, error)

	// MustMake 根据关键字凭证获取一个服务的实例,如果实例不存在,则会panic
	// 所以在使用这个接口的时候请保证服务容器已经为这个关键字凭证绑定了服务提供者
	MustMake(key string) interface{}

	// MakeNew 根据关键字凭证获取一个服务的实例,只是这个服务的实例是新的实例,不是单例
	// 它是根据服务提供者注册时的启动函数和传递的 params参数实例化出来的
	// 这个函数在需要为不同参数启动不同实例的时候非常有用
	MakeNew(key string, params ...interface{}) (interface{}, error)
}

Container 服务容器,提供绑定服务和获取服务的功能

type MyContainer

type MyContainer struct {
	// Container 表示MyContainer必须显示实现Container接口
	Container
	// contains filtered or unexported fields
}

MyContainer 是服务容器的具体实现

func NewContainer

func NewContainer() *MyContainer

func (*MyContainer) Bind

func (c *MyContainer) Bind(provider ServiceProvider) error

func (*MyContainer) IsBind

func (c *MyContainer) IsBind(key string) bool

func (*MyContainer) Make

func (c *MyContainer) Make(key string) (interface{}, error)

func (*MyContainer) MakeNew

func (c *MyContainer) MakeNew(key string, params ...interface{}) (interface{}, error)

func (*MyContainer) MustMake

func (c *MyContainer) MustMake(key string) interface{}

type NewInstance

type NewInstance func(...interface{}) (interface{}, error)

NewInstance 定义了如何创建一个新实例,这是所有服务容器的创建方法

type ServiceProvider

type ServiceProvider interface {
	// Register 在服务容器中注册了一个实例化服务的方法,是否在注册时就实例化这个服务,需要由IsDefer()方法来决定
	// 参数Container是服务容器
	// 返回值 NewInstance 是实例化服务的方法,会在服务需要使用时调用这个方法
	Register(Container) NewInstance

	// IsDefer 决定是否在注册的时候实例化这个服务,如果不是注册的时候实例化,那就是在第一次make的时候进行实例化操作
	// false表示不需要延迟实例化,在注册的时候就实例化。true表示延迟实例化
	IsDefer() bool

	// Params 定义传递给NewInstance方法的参数,可以自定义多个,但建议将Container作为第一个参数
	Params(Container) []interface{}

	// Name 定义了这个服务提供者的名称
	Name() string

	// Boot 在调用服务实例化的时候会调用这个方法,可以把一些准备工作:基础配置、初始化参数的操作放在这里
	// 如果 Boot 返回error,整个服务实例化就会实例化失败
	Boot(Container) error
}

ServiceProvider 定义一个服务提供者需要实现的接口

Directories

Path Synopsis
gin
Package gin implements a HTTP web framework called gin.
Package gin implements a HTTP web framework called gin.

Jump to

Keyboard shortcuts

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