A lightweight, reflection-based dependency-injection and lifecycle management framework.
Housekeeper manager resolve values follow this steps:
- Retrieve value by type from cache, return it when found.
- When not found, follow this steps to make it:
- Call provider for type when present, or simply new it.
- Wire value to fields which has
autowiretag. - Call
Initmethod on target value when defined. - Put made value to cache.
- Repeat these steps for parameters of provider function or
Initmethod, and values forautowirefield.
You can customize
Initmethod name throughInitMethodOptionwhen making manager.
During manager closing, all managed values that implements io.Closer will be
closed, developer can release resources in its Close method.
Provider is a function that returns a pointer value to target type. It should follow these specifications:
- Provider function should have at least one result.
- The type of first result should be a pointer type, which will be treated as target type.
- The type of second or later result can be an error type, which will be treated as providing error.
- All parameters of provider function, should be pointer type, except for
context.Contextparameter. - Provider function can NOT have variadic parameter.
Init method is the method to initialize value, It should follow these specifications:
- Init method should be exportable.
- The receiver of init method should be the pointer to value.
- Init method can have a result of error type, which will be treated as initializing error.
- All parameters of init method, should be pointer type, except for
context.Contextparameter. - Init method can NOT have variadic parameter.
- All values managed by housekeeper will be cached, in other words, they are SINGLETON.
- You SHOULD declare dependent value in pointer type.
MIT