Documentation ¶
Overview ¶
Package ioc is Inversion of Control (IoC). Support singleton and transient.
The MIT License (MIT)
Copyright (c) 2016 Jerry Bai ¶
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Index ¶
- Constants
- func AddSingleton[TService any](instance TService)
- func AddSingletonToC[TService any](container Container, instance TService)
- func AddTransient[TService any](instanceFactory func() TService)
- func AddTransientToC[TService any](container Container, instanceFactory func() TService)
- func GetService[TService any]() TService
- func GetServiceFromC[TService any](container Container) TService
- func Inject(target any)
- func InjectFromC(container Container, target any)
- func SetParent(parent Resolver)
- type Container
- type CustomInitializer
- type Resolver
Constants ¶
const DefaultInitializeMethodName string = "Initialize"
Variables ¶
This section is empty.
Functions ¶
func AddSingleton ¶ added in v1.0.0
func AddSingleton[TService any](instance TService)
AddSingleton to add singleton instance.
It will panic if 'TService' or 'instance' is invalid.
// service type Service1 interface { Method1() } // implementation of service type ServiceImplementation1 struct { Field1 string resolver ioc.Resolver } func(si *ServiceImplementation1) Method1() {} func(si *ServiceImplementation1) Initialize(resolver ioc.Resolver) { si.resolver = resolver } // interface as service ioc.AddSingleton[Service1](&ServiceImplementation1{Field1: "abc"}) // or *struct as service ioc.AddSingleton[*ServiceImplementation1](&ServiceImplementation1{Field1: "abc"})
func AddSingletonToC ¶ added in v1.1.0
AddSingletonToC to add singleton instance to container.
It will panic if 'TService' or 'instance' is invalid.
func AddTransient ¶ added in v1.0.0
func AddTransient[TService any](instanceFactory func() TService)
AddTransient to add transient service instance factory.
It will panic if 'TService' or 'instance' is invalid.
// service type Service1 interface { Method1() } // implementation of service type ServiceImplementation1 struct { Field1 string } func(si *ServiceImplementation1) Method1() {} // interface as service ioc.AddTransient[Service1](func() Service1 { return &ServiceImplementation1{Field1: "abc"} }) // or *struct as service ioc.AddTransient[*ServiceImplementation1](func() *ServiceImplementation1 { return &ServiceImplementation1{Field1: "abc"} })
func AddTransientToC ¶ added in v1.1.0
AddTransientToC to add transient service instance factory to container.
It will panic if 'TService' or 'instance' is invalid.
func GetService ¶ added in v1.0.0
func GetService[TService any]() TService
GetService to get service.
// service type Service1 interface { Method1() } // implementation of service type ServiceImplementation1 struct { Field1 string } func(si *ServiceImplementation1) Method1() {} // interface as service service1 := ioc.GetService[Service1]() // or *struct as service service2 := ioc.GetService[*ServiceImplementation1]()
func GetServiceFromC ¶ added in v1.1.0
GetServiceFromC to get service from container.
func Inject ¶ added in v1.0.0
func Inject(target any)
Inject to func or *struct with service. Field with type 'ioc.Resolver', will always been injected.
It will panic if param type in func not registered in container.
// service type Service1 interface { Method1() } // implementation of service type ServiceImplementation1 struct { Field1 string } func(si *ServiceImplementation1) Method1() {} // client type Client struct { Field1 Service1 `ioc-inject:"true"` Field2 *ServiceImplementation1 `ioc-inject:"true"` } func(c *Client) Method1(p1 Service1, p2 *ServiceImplementation1) { c.Field1 = p1 c.Field2 = p2 } var c client // inject to func ioc.Inject(c.Method1) // inject to *struct ioc.Inject(&c)
func InjectFromC ¶ added in v1.1.0
InjectFromC to inject to func or *struct or their's reflect.Value with service from container. Field with type 'ioc.Resolver', will always been injected.
It will panic if param type in func not registered in container.
Types ¶
type Container ¶
type Container interface { Resolver // AddSingleton to add singleton instance. // // // service // type Service1 interface { // Method1() // } // // implementation of service // type ServiceImplementation1 struct { // Field1 string // } // func(si *ServiceImplementation1) Method1() {} // func(si *ServiceImplementation1) Initialize(resolver ioc.Resolver) { // si.resolver = resolver // } // // var container ioc.Container // // interface as service, register as singleton // err := container.AddSingleton(reflect.TypeOf((*Service1)(nil)).Elem(), &ServiceImplementation1{Field1: "abc"}) // // or *struct as service, register as singleton // err = container.AddSingleton(reflect.TypeOf((*ServiceImplementation1)(nil)), &ServiceImplementation1{Field1: "abc"}) AddSingleton(serviceType reflect.Type, instance any) error // AddTransient to add transient by instance factory. // // // service // type Service1 interface { // Method1() // } // // implementation of service // type ServiceImplementation1 struct { // Field1 string // } // func(si *ServiceImplementation1) Method1() {} // // var container ioc.Container // // interface as service, register as transient // err = container.AddTransient(reflect.TypeOf((*Service1)(nil)).Elem(), func() any { // return &ServiceImplementation1{Field1: "abc"} // }) // // or *struct as service, register as transient // err = container.AddTransient(reflect.TypeOf((*ServiceImplementation1)(nil)), func() any { // return &ServiceImplementation1{Field1: "abc"} // }) AddTransient(serviceType reflect.Type, instanceFactory func() any) error }
Inversion of Control container.
type CustomInitializer ¶ added in v1.2.0
type CustomInitializer interface { // InitializeMethodName indicate the initialize method name. // Won't be invoked if the method returns is not exists. InitializeMethodName() string }
CustomInitializer that use customize initialize method instead of default method 'Initialize'
type Resolver ¶ added in v1.0.0
type Resolver interface { // Set parent resolver, for resolving from parent if service not found in current. SetParent(parent Resolver) // Resolve to get service. // // // service // type Service1 interface { // Method1() // } // // implementation of service // type ServiceImplementation1 struct { // Field1 string // } // func(si *ServiceImplementation1) Method1() {} // // var container ioc.Container // // interface as service // service1 := container.Resolve(reflect.TypeOf((*Service1)(nil)).Elem()) // // or *struct as service // service2 := container.Resolve(reflect.TypeOf((*ServiceImplementation1)(nil))) Resolve(serviceType reflect.Type) reflect.Value }
Resolver can resolve service.