|
Common System 0.2.0
Common interfaces and patterns for system integration
|
Abstract interface for dependency injection containers. More...
#include <service_container_interface.h>


Public Member Functions | |
| virtual | ~IServiceContainer ()=default |
| template<concepts::ServiceInterface TInterface, concepts::ServiceImplementation< TInterface > TImpl> | |
| VoidResult | register_type (service_lifetime lifetime=service_lifetime::singleton) |
| Register a service type with its implementation. | |
| template<typename TInterface > | |
| VoidResult | register_instance (std::shared_ptr< TInterface > instance) |
| Register a pre-existing instance as a singleton. | |
| template<concepts::ServiceInterface TInterface, concepts::ServiceFactory< TInterface > TFactory> | |
| VoidResult | register_factory (TFactory &&factory, service_lifetime lifetime=service_lifetime::singleton) |
| Register a factory function for creating service instances. | |
| template<concepts::ServiceInterface TInterface, concepts::SimpleServiceFactory< TInterface > TFactory> | |
| VoidResult | register_simple_factory (TFactory &&factory, service_lifetime lifetime=service_lifetime::singleton) |
| Register a simple factory function without container access. | |
| template<typename TInterface > | |
| Result< std::shared_ptr< TInterface > > | resolve () |
| Resolve a service by its interface type. | |
| template<typename TInterface > | |
| std::shared_ptr< TInterface > | resolve_or_null () |
| Try to resolve a service, returning nullptr if not found. | |
| virtual std::unique_ptr< IServiceScope > | create_scope ()=0 |
| Create a new service scope. | |
| template<typename TInterface > | |
| bool | is_registered () const |
| Check if a service type is registered. | |
| virtual std::vector< service_descriptor > | registered_services () const =0 |
| Get list of all registered service descriptors. | |
| template<typename TInterface > | |
| VoidResult | unregister () |
| Unregister a service type. | |
| virtual VoidResult | clear ()=0 |
| Clear all registrations. | |
| virtual | ~IServiceContainer ()=default |
| template<typename Interface , typename Factory > | |
| void | register_factory (Factory &&factory) |
| Register a service with a factory function. | |
| template<typename Interface > | |
| void | register_singleton (std::shared_ptr< Interface > instance) |
| Register a singleton instance. | |
| template<typename Interface > | |
| std::shared_ptr< Interface > | resolve () |
| Resolve a service by interface type. | |
| template<typename Interface > | |
| bool | has () const |
| Check if a service is registered. | |
Protected Member Functions | |
| virtual VoidResult | register_factory_internal (std::type_index interface_type, const std::string &type_name, std::function< std::shared_ptr< void >(IServiceContainer &)> factory, service_lifetime lifetime)=0 |
| Internal factory registration (type-erased). | |
| virtual VoidResult | register_instance_internal (std::type_index interface_type, const std::string &type_name, std::shared_ptr< void > instance)=0 |
| Internal instance registration (type-erased). | |
| virtual Result< std::shared_ptr< void > > | resolve_internal (std::type_index interface_type)=0 |
| Internal service resolution (type-erased). | |
| virtual bool | is_registered_internal (std::type_index interface_type) const =0 |
| Internal registration check (type-erased). | |
| virtual VoidResult | unregister_internal (std::type_index interface_type)=0 |
| Internal unregistration (type-erased). | |
| virtual void | register_impl (std::type_index type, std::any factory)=0 |
| virtual void | register_singleton_impl (std::type_index type, std::any instance)=0 |
| virtual std::any | resolve_impl (std::type_index type)=0 |
| virtual bool | has_impl (std::type_index type) const =0 |
Abstract interface for dependency injection containers.
Interface for dependency injection containers.
IServiceContainer provides a type-safe mechanism for registering and resolving services with configurable lifetimes. It supports:
Thread Safety Requirements:
Error Handling:
Usage Example:
|
virtualdefault |
|
exportvirtualdefault |
|
pure virtual |
Clear all registrations.
Removes all service registrations. Existing resolved instances remain valid but no services can be resolved after this call.
Implemented in kcenon::common::di::service_container, and kcenon::common::di::service_scope.
|
pure virtual |
Create a new service scope.
Returns a scoped container that shares singleton registrations with the parent but maintains its own instances for scoped services.
Implemented in kcenon::common::di::service_container, and kcenon::common::di::service_scope.
|
export |
Check if a service is registered.
Definition at line 102 of file di.cppm.
References has_impl().

|
exportprotectedpure virtual |
Implemented in kcenon::common::di::ServiceContainer.
Referenced by has().

|
inline |
Check if a service type is registered.
| TInterface | The interface type to check |
Definition at line 371 of file service_container_interface.h.
References is_registered_internal().

|
protectedpure virtual |
Internal registration check (type-erased).
Implemented in kcenon::common::di::service_container, and kcenon::common::di::service_scope.
Referenced by is_registered().

|
export |
Register a service with a factory function.
Definition at line 75 of file di.cppm.
References register_impl().

|
inline |
Register a factory function for creating service instances.
The factory is called each time the service needs to be instantiated (once for singleton, each time for transient, once per scope for scoped). The factory receives a reference to the container for resolving dependencies.
| TInterface | The interface type to register (must satisfy ServiceInterface) |
| TFactory | Callable returning shared_ptr<TInterface> (must satisfy ServiceFactory) |
| factory | Factory function: (IServiceContainer&) -> shared_ptr<TInterface> |
| lifetime | Lifetime policy for the service |
Definition at line 263 of file service_container_interface.h.
References register_factory_internal().

|
protectedpure virtual |
Internal factory registration (type-erased).
Implemented in kcenon::common::di::service_container, and kcenon::common::di::service_scope.
Referenced by register_factory(), register_simple_factory(), and register_type().

|
exportprotectedpure virtual |
Implemented in kcenon::common::di::ServiceContainer.
Referenced by register_factory().

|
inline |
Register a pre-existing instance as a singleton.
The provided instance will be returned for all resolution requests. The container takes shared ownership of the instance.
| TInterface | The interface type to register |
| instance | The instance to register |
Definition at line 230 of file service_container_interface.h.
References kcenon::common::error_codes::INVALID_ARGUMENT, kcenon::common::make_error(), and register_instance_internal().

|
protectedpure virtual |
Internal instance registration (type-erased).
Implemented in kcenon::common::di::service_container, and kcenon::common::di::service_scope.
Referenced by register_instance().

|
inline |
Register a simple factory function without container access.
Convenience overload for factories that don't need to resolve dependencies.
| TInterface | The interface type to register (must satisfy ServiceInterface) |
| TFactory | Callable returning shared_ptr<TInterface> (must satisfy SimpleServiceFactory) |
| factory | Factory function: () -> shared_ptr<TInterface> |
| lifetime | Lifetime policy for the service |
Definition at line 290 of file service_container_interface.h.
References register_factory_internal().

|
export |
Register a singleton instance.
Definition at line 81 of file di.cppm.
References register_singleton_impl().

|
exportprotectedpure virtual |
Implemented in kcenon::common::di::ServiceContainer.
Referenced by register_singleton().

|
inline |
Register a service type with its implementation.
Creates a factory that constructs TImpl instances when TInterface is resolved. TImpl must be constructible with no arguments or with dependencies that are also registered in the container.
| TInterface | The interface type to register (must satisfy ServiceInterface) |
| TImpl | The implementation type (must satisfy ServiceImplementation) |
| lifetime | Lifetime policy for the service |
Definition at line 207 of file service_container_interface.h.
References register_factory_internal().

|
pure virtual |
Get list of all registered service descriptors.
Implemented in kcenon::common::di::service_container, and kcenon::common::di::service_scope.
|
inline |
Resolve a service by its interface type.
Returns a shared pointer to the service instance. For singletons, returns the cached instance. For transient, creates a new instance. For scoped, returns the instance for the current scope.
| TInterface | The interface type to resolve |
Possible errors:
Definition at line 320 of file service_container_interface.h.
References kcenon::common::make_error(), kcenon::common::ok(), and resolve_internal().
Referenced by resolve_or_null().


|
export |
Resolve a service by interface type.
Definition at line 86 of file di.cppm.
References resolve_impl().

|
exportprotectedpure virtual |
Implemented in kcenon::common::di::ServiceContainer.
Referenced by resolve().

|
protectedpure virtual |
Internal service resolution (type-erased).
Implemented in kcenon::common::di::service_container, and kcenon::common::di::service_scope.
Referenced by resolve().

|
inline |
Try to resolve a service, returning nullptr if not found.
Unlike resolve(), this method does not return an error for unregistered services. Use when the service is optional.
| TInterface | The interface type to resolve |
Definition at line 342 of file service_container_interface.h.
References resolve().

|
inline |
Unregister a service type.
Removes the registration for the specified interface. Existing resolved instances remain valid but no new instances can be resolved.
| TInterface | The interface type to unregister |
Definition at line 392 of file service_container_interface.h.
References unregister_internal().

|
protectedpure virtual |
Internal unregistration (type-erased).
Implemented in kcenon::common::di::service_container, and kcenon::common::di::service_scope.
Referenced by unregister().
