Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
kcenon::common::di::IServiceScope Interface Referenceabstract

Scoped service container for request-level isolation. More...

#include <service_container_interface.h>

Inheritance diagram for kcenon::common::di::IServiceScope:
Inheritance graph
Collaboration diagram for kcenon::common::di::IServiceScope:
Collaboration graph

Public Member Functions

 ~IServiceScope () override=default
 
virtual IServiceContainerparent ()=0
 Get the parent container.
 
virtual const IServiceContainerparent () const =0
 Get the parent container (const).
 
- Public Member Functions inherited from kcenon::common::di::IServiceContainer
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< IServiceScopecreate_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_descriptorregistered_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.
 

Additional Inherited Members

- Protected Member Functions inherited from kcenon::common::di::IServiceContainer
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
 

Detailed Description

Scoped service container for request-level isolation.

A service scope inherits all registrations from its parent container but maintains its own instances for scoped services. When the scope is destroyed, all scoped instances are disposed.

Thread Safety:

  • IServiceScope implementations should be thread-safe
  • Typically used within a single request/thread context

Usage:

void handle_request(IServiceContainer& container) {
auto scope = container.create_scope();
// Each request gets its own database connection
auto db = scope->resolve<IDatabase>().value();
auto db2 = scope->resolve<IDatabase>().value();
// db == db2 (same scoped instance)
// Process request...
} // scope destroyed, scoped instances disposed
Abstract interface for dependency injection containers.
Definition di.cppm:39
virtual std::unique_ptr< IServiceScope > create_scope()=0
Create a new service scope.

Definition at line 469 of file service_container_interface.h.

Constructor & Destructor Documentation

◆ ~IServiceScope()

kcenon::common::di::IServiceScope::~IServiceScope ( )
overridedefault

Member Function Documentation

◆ parent() [1/2]

virtual const IServiceContainer & kcenon::common::di::IServiceScope::parent ( ) const
pure virtual

Get the parent container (const).

Returns
Const reference to the parent container

Implemented in kcenon::common::di::service_scope.

◆ parent() [2/2]

virtual IServiceContainer & kcenon::common::di::IServiceScope::parent ( )
pure virtual

Get the parent container.

Returns
Reference to the parent container

Implemented in kcenon::common::di::service_scope.


The documentation for this interface was generated from the following file: