# cpl_core.dependency_injection — Common Python Library documentation

# cpl_core.dependency_injection

## cpl_core.dependency_injection.service_collection

class cpl_core.dependency_injection.service_collection.ServiceCollection(config: ConfigurationABC )

  Bases: `ServiceCollectionABC`

  Representation of the collection of services

  add_db_context(db_context_type: Type[ DatabaseContextABC ], db_settings: DatabaseSettings )

    Adds database context

    > db_context: Type[`cpl_core.database.context.database_context_abc.DatabaseContextABC`]

    >   Database context

  add_logging()

    Adds the CPL internal logger

  add_pipes()

    Adds the CPL internal pipes as transient

  add_scoped(service_type: Type, service: Optional[Callable] = None)

    Adds a service with scoped lifetime

    > service_type: `Type`

    >   Type of the service

    > service: `Callable`

    >   Object of the service

    > self: :class:`cpl_core.dependency_injection.service_collection_abc.ServiceCollectionABC

  add_singleton(service_type: Union[type, object], service: Optional[Union[type, object]] = None)

    Adds a service with singleton lifetime

    > service_type: `Type`

    >   Type of the service

    > service: `Callable`

    >   Object of the service

    > self: :class:`cpl_core.dependency_injection.service_collection_abc.ServiceCollectionABC

  add_transient(service_type: type, service: Optional[type] = None)

    Adds a service with transient lifetime

    > service_type: `Type`

    >   Type of the service

    > service: `Callable`

    >   Object of the service

    > self: :class:`cpl_core.dependency_injection.service_collection_abc.ServiceCollectionABC

  build_service_provider() → ServiceProviderABC

    Creates instance of the service provider

    > Object of type `cpl_core.dependency_injection.service_provider_abc.ServiceProviderABC`

## cpl_core.dependency_injection.service_collection_abc

class cpl_core.dependency_injection.service_collection_abc.ServiceCollectionABC

  Bases: `ABC`

  ABC for the class `cpl_core.dependency_injection.service_collection.ServiceCollection`

  abstract add_db_context(db_context_type: Type[ DatabaseContextABC ], db_settings: DatabaseSettings )

    Adds database context

    > db_context: Type[`cpl_core.database.context.database_context_abc.DatabaseContextABC`]

    >   Database context

  add_discord()

    Adds the CPL discord

  abstract add_logging()

    Adds the CPL internal logger

  abstract add_pipes()

    Adds the CPL internal pipes as transient

  abstract add_scoped(service_type: Type, service: Optional[Callable] = None) → ServiceCollectionABC

    Adds a service with scoped lifetime

    > service_type: `Type`

    >   Type of the service

    > service: `Callable`

    >   Object of the service

    > self: :class:`cpl_core.dependency_injection.service_collection_abc.ServiceCollectionABC

  abstract add_singleton(service_type: Type, service: Optional[Callable] = None) → ServiceCollectionABC

    Adds a service with singleton lifetime

    > service_type: `Type`

    >   Type of the service

    > service: `Callable`

    >   Object of the service

    > self: :class:`cpl_core.dependency_injection.service_collection_abc.ServiceCollectionABC

  abstract add_transient(service_type: Type, service: Optional[Callable] = None) → ServiceCollectionABC

    Adds a service with transient lifetime

    > service_type: `Type`

    >   Type of the service

    > service: `Callable`

    >   Object of the service

    > self: :class:`cpl_core.dependency_injection.service_collection_abc.ServiceCollectionABC

  add_translation()

    Adds the CPL translation

  abstract build_service_provider() → ServiceProviderABC

    Creates instance of the service provider

    > Object of type `cpl_core.dependency_injection.service_provider_abc.ServiceProviderABC`

## cpl_core.dependency_injection.service_descriptor

class cpl_core.dependency_injection.service_descriptor.ServiceDescriptor(implementation: Optional[Union[type, object]], lifetime: ServiceLifetimeEnum, base_type=None)

  Bases: `object`

  Descriptor of a service

  > implementation: Union[`type`, Optional[`object`]]

  >   Object or type of service

  > lifetime: `cpl_core.dependency_injection.service_lifetime_enum.ServiceLifetimeEnum`

  >   Lifetime of the service

  property base_type: type
  property implementation: Optional[Union[type, object]]
  property lifetime: ServiceLifetimeEnum
  property service_type: type

## cpl_core.dependency_injection.service_lifetime_enum

class cpl_core.dependency_injection.service_lifetime_enum.ServiceLifetimeEnum(value)

  Bases: `Enum`

  An enumeration.

  scoped = 1
  singleton = 0
  transient = 2

## cpl_core.dependency_injection.service_provider

class cpl_core.dependency_injection.service_provider.ServiceProvider(service_descriptors: list[ cpl_core.dependency_injection.service_descriptor.ServiceDescriptor ], config: ConfigurationABC, db_context: Optional[ DatabaseContextABC ])

  Bases: `ServiceProviderABC`

  Provider for the services

  > service_descriptors: list[`cpl_core.dependency_injection.service_descriptor.ServiceDescriptor`]

  >   Descriptor of the service

  > config: `cpl_core.configuration.configuration_abc.ConfigurationABC`

  >   CPL Configuration

  > db_context: Optional[`cpl_core.database.context.database_context_abc.DatabaseContextABC`]

  >   Database representation

  build_service(service_type: type) → object

    Creates instance of given type

    > instance_type: `Type`

    >   The type of the searched instance

    > Object of the given type

  create_scope() → ScopeABC

    Creates a service scope

    > Object of type `cpl_core.dependency_injection.scope.Scope`

  get_service(service_type: type) → Optional[object]

    Returns instance of given type

    > instance_type: `Type`

    >   The type of the searched instance

    > Object of type Optional[Callable[`object`]]

  set_scope(scope: ScopeABC)

    Sets the scope of service provider

    > scope `cpl_core.dependency_injection.scope.Scope`

    >   Service scope

## cpl_core.dependency_injection.service_provider_abc

class cpl_core.dependency_injection.service_provider_abc.ServiceProviderABC

  Bases: `ABC`

  ABC for the class `cpl_core.dependency_injection.service_provider.ServiceProvider`

  abstract build_service(service_type: Type) → object

    Creates instance of given type

    > instance_type: `Type`

    >   The type of the searched instance

    > Object of the given type

  abstract create_scope() → ScopeABC

    Creates a service scope

    > Object of type `cpl_core.dependency_injection.scope.Scope`

  abstract get_service(instance_type: Type) → Optional[object]

    Returns instance of given type

    > instance_type: `Type`

    >   The type of the searched instance

    > Object of type Optional[Callable[`object`]]

  abstract set_scope(scope: ScopeABC)

    Sets the scope of service provider

    > scope `cpl_core.dependency_injection.scope.Scope`

    >   Service scope