RegisterSingleton registers a singleton instance with the given name. Returns an error if a singleton with the same name already exists.
(name string, instance any)
| 193 | // RegisterSingleton registers a singleton instance with the given name. |
| 194 | // Returns an error if a singleton with the same name already exists. |
| 195 | func (d *DefaultContainer) RegisterSingleton(name string, instance any) error { |
| 196 | if name == "" { |
| 197 | return errors.New("empty name") |
| 198 | } |
| 199 | |
| 200 | if instance == nil { |
| 201 | return errors.New("nil instance") |
| 202 | } |
| 203 | |
| 204 | d.muSingletons.Lock() |
| 205 | defer d.muSingletons.Unlock() |
| 206 | |
| 207 | if _, exists := d.singletons[name]; exists { |
| 208 | return ErrInstanceAlreadyExists |
| 209 | } |
| 210 | |
| 211 | d.singletons[name] = instance |
| 212 | d.typesOfSingletons[name] = reflect.TypeOf(instance) |
| 213 | return nil |
| 214 | } |
| 215 | |
| 216 | // ContainsSingleton checks whether a singleton with the specified name exists. |
| 217 | func (d *DefaultContainer) ContainsSingleton(name string) bool { |
no outgoing calls