framework

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2022 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Copyright 2022 cralack. All rights reserved. Use of this source code is governed by a MIT style license that can be found in the LICENSE file.

Copyright 2022 cralack. All rights reserved. Use of this source code is governed by a MIT style license that can be found in the LICENSE file.

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 NewInstance

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

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

type PBContainer

type PBContainer struct {
	Container // 强制要求 PBContainer 实现 Container 接口
	// contains filtered or unexported fields
}

func NewPBContainer

func NewPBContainer() *PBContainer

func (*PBContainer) Bind

func (cont *PBContainer) Bind(provider ServiceProvider) error

Bind 将服务容器和关键字做了绑定

func (*PBContainer) IsBind

func (cont *PBContainer) IsBind(key string) bool

func (*PBContainer) Make

func (cont *PBContainer) Make(key string) (interface{}, error)

func (*PBContainer) MakeNew

func (cont *PBContainer) MakeNew(key string, params []interface{}) (interface{}, error)

func (*PBContainer) MustMake

func (cont *PBContainer) MustMake(key string) interface{}

func (*PBContainer) NameList

func (cont *PBContainer) NameList() (ret []string)

NameList 列出容器中所有服务提供者的字符串凭证

func (*PBContainer) PrintProviders

func (cont *PBContainer) PrintProviders() []string

PrintProviders 输出服务容器中注册的关键字

type ServiceProvider

type ServiceProvider interface {
	// Register 在服务容器中注册了一个实例化服务的方法, 是否在注册的时候就实例化这个服务,需要参考 IsDefer 接口。
	Register(Container) NewInstance
	// Boot 在调用实例化服务的时候会调用,可以把一些准备工作:基础配置,初始化参数的操作放在这个里面。
	// 如果 Boot 返回 error,整个服务实例化就会实例化失败,返回错误
	Boot(Container) error
	// IsDefer 决定是否在注册的时候实例化这个服务,如果不是注册的时候实例化,那就是在第一次 make 的时候进行实例化操作
	// false 表示不需要延迟实例化,在注册的时候就实例化。true 表示延迟实例化
	IsDefer() bool
	// Params params 定义传递给 NewInstance 的参数,可以自定义多个,建议将 container 作为第一个参数
	Params(Container) []interface{}
	// Name 代表了这个服务提供者的凭证
	Name() string
}

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

Directories

Path Synopsis
Package cobra is a commander providing a simple interface to create powerful modern CLI interfaces.
Package cobra is a commander providing a simple interface to create powerful modern CLI interfaces.
doc
gin
Package gin implements a HTTP web framework called gin.
Package gin implements a HTTP web framework called gin.
provider
app
env
id
log
orm
ssh

Jump to

Keyboard shortcuts

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