|
@@ -1,7 +1,7 @@
|
|
|
# Lifecycle Enum
|
|
# Lifecycle Enum
|
|
|
|
|
|
|
|
## Overview
|
|
## Overview
|
|
|
-`Lifecycle` defines instance lifetime behavior for registered components. Three lifecycles are supported: transient (new instance per resolve), scoped (one instance per scope), and singleton (one instance per container).
|
|
|
|
|
|
|
+`Lifecycle` defines instance lifetime behavior for registered components. Four lifecycles are supported: transient (new instance per resolve), scoped (one instance per scope), singleton (one instance per container), and startup (eagerly initialized singletons).
|
|
|
|
|
|
|
|
## Namespace
|
|
## Namespace
|
|
|
`Inversion`
|
|
`Inversion`
|
|
@@ -72,10 +72,33 @@ Use for:
|
|
|
- Connection pools
|
|
- Connection pools
|
|
|
- Logging services
|
|
- Logging services
|
|
|
|
|
|
|
|
|
|
+### `STARTUP`
|
|
|
|
|
+A single instance is created at container initialization time. The same instance is returned for all requests across all scopes. These instances are created eagerly when `initialise()` is called.
|
|
|
|
|
+
|
|
|
|
|
+```vala
|
|
|
|
|
+container.register_startup<DatabaseConnection>((s) => new DatabaseConnection());
|
|
|
|
|
+
|
|
|
|
|
+// After all registrations, initialize startup components
|
|
|
|
|
+container.initialise();
|
|
|
|
|
+
|
|
|
|
|
+var scope1 = container.create_scope();
|
|
|
|
|
+var scope2 = container.create_scope();
|
|
|
|
|
+
|
|
|
|
|
+var a = scope1.resolve<DatabaseConnection>(); // Instance A (already created)
|
|
|
|
|
+var b = scope2.resolve<DatabaseConnection>(); // Same Instance A
|
|
|
|
|
+// a == b
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
|
|
+Use for:
|
|
|
|
|
+- Database connections that must be established at startup
|
|
|
|
|
+- Background services that need to start immediately
|
|
|
|
|
+- Health-checkable components
|
|
|
|
|
+- Any singleton that should fail fast if instantiation fails
|
|
|
|
|
+
|
|
|
## Lifecycle Constraints
|
|
## Lifecycle Constraints
|
|
|
|
|
|
|
|
-### Singleton Scope Cannot Resolve Scoped
|
|
|
|
|
-When resolving from a singleton context, scoped registrations cannot be resolved:
|
|
|
|
|
|
|
+### Singleton/Startup Scope Cannot Resolve Scoped
|
|
|
|
|
+When resolving from a singleton or startup context, scoped registrations cannot be resolved:
|
|
|
|
|
|
|
|
```vala
|
|
```vala
|
|
|
// This throws ILLEGAL_LIFECYCLE_COMBINATION
|
|
// This throws ILLEGAL_LIFECYCLE_COMBINATION
|
|
@@ -92,12 +115,12 @@ scope.resolve<SingletonService>(); // Throws!
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
### Scope-Local Registrations
|
|
### Scope-Local Registrations
|
|
|
-Scope-local registrations cannot be singletons:
|
|
|
|
|
|
|
+Scope-local registrations cannot be singletons or startup:
|
|
|
|
|
|
|
|
```vala
|
|
```vala
|
|
|
// Compile-time precondition failure
|
|
// Compile-time precondition failure
|
|
|
scope.register_local<MyService>((s) => new MyService(), Lifecycle.SINGLETON);
|
|
scope.register_local<MyService>((s) => new MyService(), Lifecycle.SINGLETON);
|
|
|
-// Error: requires (lifecycle != Lifecycle.SINGLETON)
|
|
|
|
|
|
|
+// Error: requires (lifecycle != Lifecycle.SINGLETON && lifecycle != Lifecycle.STARTUP)
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
## Registration Methods by Lifecycle
|
|
## Registration Methods by Lifecycle
|
|
@@ -107,15 +130,17 @@ scope.register_local<MyService>((s) => new MyService(), Lifecycle.SINGLETON);
|
|
|
| `register_transient<T>()` | TRANSIENT |
|
|
| `register_transient<T>()` | TRANSIENT |
|
|
|
| `register_scoped<T>()` | SCOPED |
|
|
| `register_scoped<T>()` | SCOPED |
|
|
|
| `register_singleton<T>()` | SINGLETON |
|
|
| `register_singleton<T>()` | SINGLETON |
|
|
|
|
|
+| `register_startup<T>()` | STARTUP |
|
|
|
| `register<T>(..., lifecycle)` | Specified |
|
|
| `register<T>(..., lifecycle)` | Specified |
|
|
|
|
|
|
|
|
## Comparison Table
|
|
## Comparison Table
|
|
|
|
|
|
|
|
-| Lifecycle | Instance Count | Scope Behavior | Memory |
|
|
|
|
|
-|-----------|---------------|----------------|--------|
|
|
|
|
|
-| TRANSIENT | Per resolve | New each time | Highest |
|
|
|
|
|
-| SCOPED | Per scope | Shared in scope | Medium |
|
|
|
|
|
-| SINGLETON | Per container | Shared globally | Lowest |
|
|
|
|
|
|
|
+| Lifecycle | Instance Count | Scope Behavior | Memory | Initialization |
|
|
|
|
|
+|-----------|---------------|----------------|--------|----------------|
|
|
|
|
|
+| TRANSIENT | Per resolve | New each time | Highest | On demand |
|
|
|
|
|
+| SCOPED | Per scope | Shared in scope | Medium | On demand |
|
|
|
|
|
+| SINGLETON | Per container | Shared globally | Lowest | On demand |
|
|
|
|
|
+| STARTUP | Per container | Shared globally | Lowest | Eager (initialise()) |
|
|
|
|
|
|
|
|
## Usage Examples
|
|
## Usage Examples
|
|
|
|
|
|
|
@@ -132,6 +157,20 @@ container.register_singleton<AppConfig>((s) => {
|
|
|
});
|
|
});
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
|
|
+### Startup Database Connection
|
|
|
|
|
+```vala
|
|
|
|
|
+public class DatabaseConnection : Object {
|
|
|
|
|
+ public DatabaseConnection() throws DatabaseError {
|
|
|
|
|
+ // Connect to database - fails fast if connection unavailable
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+container.register_startup<DatabaseConnection>((s) => new DatabaseConnection());
|
|
|
|
|
+
|
|
|
|
|
+// Will throw if database connection fails, allowing early detection
|
|
|
|
|
+container.initialise();
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
### Request-Scoped Context
|
|
### Request-Scoped Context
|
|
|
```vala
|
|
```vala
|
|
|
public class RequestContext : Object {
|
|
public class RequestContext : Object {
|
|
@@ -170,4 +209,4 @@ var handlers = scope.resolve_all<IRequestHandler>();
|
|
|
|
|
|
|
|
---
|
|
---
|
|
|
|
|
|
|
|
-The Lifecycle enum defines three instance lifetime behaviors: TRANSIENT for per-resolution instances, SCOPED for per-scope instances, and SINGLETON for container-wide single instances.
|
|
|
|
|
|
|
+The Lifecycle enum defines four instance lifetime behaviors: TRANSIENT for per-resolution instances, SCOPED for per-scope instances, SINGLETON for container-wide single instances, and STARTUP for eagerly-initialized singletons.
|