addScoped
mach::AppBuilder::addSingleton
Registers a singleton service.
Description
Section titled “Description”Registers a singleton service with the DI container.
Syntax
Section titled “Syntax”template <typename T, typename... Deps>AppBuilder& addSingleton();Return Value
Section titled “Return Value”A reference to the current AppBuilder instance.
Template Parameters
Section titled “Template Parameters”| Parameter | Description |
|---|---|
T |
The service type to register. |
Deps |
The dependency types used to construct the service. |
Exceptions
Section titled “Exceptions”| Exception | Condition |
|---|---|
std::logic_error |
Thrown if the service type has already been registered or is reserved by the framework. |
Remarks
Section titled “Remarks”- 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.
Thread Safety
Section titled “Thread Safety”This method is not thread-safe and should only be called during application configuration, before the application starts handling requests.
Example
Section titled “Example”builder.addSingleton<Database>();builder.addSingleton<UserRepository, Database>();See Also
Section titled “See Also”| Name | Description |
|---|---|
addScoped |
Registers a scoped service. |
addTransient |
Registers a transient service. |