diff --git a/registry/registry.go b/registry/registry.go index 676adff..0272d78 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -20,6 +20,7 @@ var registry ComponentRegistry type ComponentRegistry interface { RegisterType(t reflect.Type, ca *cda.ComponentAnnotation) + RegisterResource(name string, resource interface{}) error GetInstance(t reflect.Type) (interface{}, error) GetInstanceByName(name string) (interface{}, error) @@ -29,6 +30,7 @@ func newRegistry() ComponentRegistry { r := &defaultComponentRegistry{} r.definitionByType = make(map[reflect.Type]*TypeDefinition, 0) r.definitionByName = make(map[string]*TypeDefinition, 0) + r.resourceByName = make(map[string]interface{}, 0) return r } @@ -36,6 +38,7 @@ func newRegistry() ComponentRegistry { type defaultComponentRegistry struct { definitionByType map[reflect.Type]*TypeDefinition definitionByName map[string]*TypeDefinition + resourceByName map[string]interface{} } func RegisterType(t reflect.Type, ca *cda.ComponentAnnotation) { @@ -78,6 +81,18 @@ func (cr *defaultComponentRegistry) RegisterType(t reflect.Type, ca *cda.Compone cr.definitionByName[name] = td } +func RegisterResource(name string, resource interface{}) error { + return registry.RegisterResource(name, resource) +} +func (cr *defaultComponentRegistry) RegisterResource(name string, resource interface{}) error { + if _, ok := cr.resourceByName[name]; ok { + return fmt.Errorf("DI: Resource[%s] is already exist", name) + } + cr.resourceByName[name] = resource + + return nil +} + func GetInstance(t reflect.Type) (interface{}, error) { return registry.GetInstance(t) }