diff --git a/container.go b/container.go index d97ba2d..a7439ed 100644 --- a/container.go +++ b/container.go @@ -138,6 +138,35 @@ func (c Container) Reset() { } } +// Close Calls the close method on all singleton instances within the container. +func (c Container) Close() error { + for _, abstraction := range c { + for _, concrete := range abstraction { + if !concrete.isSingleton { + continue + } + instance, err := concrete.make(c) + if err != nil { + return err + } + + v := reflect.ValueOf(instance) + mm := v.MethodByName("Close") + if mm.IsValid() { + res := mm.Call(nil) + if len(res) > 0 { + errVal := res[0] + if errVal.Type().Name() == "error" && errVal.Interface() != nil { + err, _ := errVal.Interface().(error) + return err + } + } + } + } + } + return nil +} + // Singleton binds an abstraction to concrete in singleton mode. // It takes a resolver function that returns the concrete, and its return type matches the abstraction (interface). // The resolver function can have arguments of abstraction that have been declared in the Container already. diff --git a/global.go b/global.go index 4b8d4fe..81958fc 100644 --- a/global.go +++ b/global.go @@ -48,6 +48,11 @@ func Reset() { Global.Reset() } +// Close calls the same method of the global concrete. +func Close() error { + return Global.Close() +} + // Call calls the same method of the global concrete. func Call(receiver interface{}) error { return Global.Call(receiver)