Skip to content
Download

addScoped

mach::AppBuilder::addSingleton

Registers a singleton service.

Registers a singleton service with the DI container.

template <typename T, typename... Deps>
AppBuilder& addSingleton();

A reference to the current AppBuilder instance.

Parameter Description
T The service type to register.
Deps The dependency types used to construct the service.
Exception Condition
std::logic_error Thrown if the service type has already been registered or is reserved by the framework.
  • A single instance of the service is created and shared by all consumers that resolve it for the lifetime of the application.
  • A singleton service cannot depend on a scoped service.

This method is not thread-safe and should only be called during application configuration, before the application starts handling requests.

builder.addSingleton<Database>();
builder.addSingleton<UserRepository, Database>();
Name Description
addScoped Registers a scoped service.
addTransient Registers a transient service.