// Copyright (c) Shenyang Leading Edge Intelligent Technology Co., Ltd. All rights reserved.
|
|
|
|
package container
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
// 组件句柄管理器
|
|
type HandlerManager struct {
|
|
// 组件句柄映射: map[接口类型]组件句柄
|
|
handlerMapping map[Interface]ComponentHandler
|
|
}
|
|
|
|
// 创建组件句柄管理器
|
|
// 参数
|
|
// 1.会话标识
|
|
// 2.用户信息
|
|
// 3.服务映射: map[接口类型]组件句柄
|
|
// 返回值:
|
|
// 1.会话
|
|
// 2.错误
|
|
func NewHandlerManager(handlerMapping map[Interface]ComponentHandler) (*HandlerManager, error) {
|
|
if len(handlerMapping) > 0 {
|
|
for interfaceType, handler := range handlerMapping {
|
|
if interfaceType == nil {
|
|
return nil, errors.New(fmt.Sprintf("组件接口不能为空!"))
|
|
}
|
|
if handler == nil {
|
|
return nil, errors.New(fmt.Sprintf("组件句柄不能为空!"))
|
|
}
|
|
}
|
|
}
|
|
return &HandlerManager{handlerMapping}, nil
|
|
}
|
|
|
|
// 获取指定接口的组件句柄
|
|
// 参数
|
|
// 1.服务接口类型
|
|
// 返回值:
|
|
// 1.组件句柄
|
|
func (session *HandlerManager) Handler(interfaceType Interface) ComponentHandler {
|
|
if interfaceType == nil {
|
|
return nil
|
|
}
|
|
componentHandler, ok := session.handlerMapping[interfaceType]
|
|
if !ok {
|
|
return nil
|
|
}
|
|
return componentHandler
|
|
}
|