container icon indicating copy to clipboard operation
container copied to clipboard

add close method

Open leijux opened this issue 2 years ago • 5 comments

close Calls the close method on all singleton instances within the container.

leijux avatar Dec 14 '23 03:12 leijux

Do you believe it's truly the responsibility of the IOC container to handle the closure of services?

miladrahimi avatar Dec 16 '23 14:12 miladrahimi

Do you believe it's truly the responsibility of the IOC container to handle the closure of services?

I tried to close all instances with Close(c container.Container), but concrete.isSingleton and concrete.make are unexported fields and methods.

Thanks for your help!

leijux avatar Dec 16 '23 17:12 leijux

I think it is necessary to condition the close method.

  1. have no impact on existing functions and methods
  2. Convenient to close db like services

leijux avatar Dec 16 '23 17:12 leijux

I know that close is not a special method, and I don't understand why all objects should have such a method. Could you explain more about it?

miladrahimi avatar Dec 19 '23 09:12 miladrahimi

I know that close is not a special method, and I don't understand why all objects should have such a method. Could you explain more about it?

Although 'close' is not a special method, some objects conventionally use it to release resources such as databases or files.

Therefore, if the Singleton has a 'close' method, it should be called to release the resource.

v := reflect.ValueOf(instance)
mm := v.MethodByName("Close")
if mm.IsValid() {}

leijux avatar Dec 22 '23 04:12 leijux