# Registration Class ## Overview `Registration` represents a component registration in the IoC container. It maps implementation types to service types (interfaces), specifies lifecycle behavior, and holds the factory for instance creation. ## Namespace `Inversion` ## Class Declaration ```vala public class Registration : Object ``` ## Properties | Property | Type | Description | |----------|------|-------------| | `implementation_type` | `Type` | The concrete type being registered | | `lifecycle` | `Lifecycle` | Instance lifecycle (default: `TRANSIENT`) | | `factory` | `Factory` | Factory for creating instances | | `type_aliases` | `ImmutableLot` | All service types this registration provides | ## Constructor ### `Registration(Type implementation_type, Factory factory, Lifecycle lifecycle)` Creates a new registration. The implementation type is automatically added as a service type. ```vala var factory = new DelegateFactory((scope) => new MyService()); var registration = new Registration(typeof(MyService), factory, Lifecycle.SINGLETON); ``` ## Fluent Configuration Methods ### `as_type(Type type) → Registration` Adds a service type alias to the registration. Returns `this` for chaining. ```vala container.register_singleton((s) => new MyImplementation()) .as_type(typeof(IServiceA)) .as_type(typeof(IServiceB)); ``` ## Query Methods ### `provides(Type type) → bool` Checks if this registration provides the specified service type. ```vala if (registration.provides(typeof(ILogger))) { // Can resolve as ILogger } ``` ## Signals ### `alias_added(Type alias)` Emitted when a new type alias is added via `as_type()`. Used internally by `Container` and `Scope` to update their catalogues. ## FactoryDelegate ### Definition ```vala public delegate Object FactoryDelegate(Scope scope) throws Error; ``` Factory delegate for custom instantiation logic. Receives the current scope for dependency resolution. ### Usage ```vala // Simple factory container.register_transient((scope) => new MyService()); // Factory with dependencies container.register_transient((scope) => { var repo = scope.resolve(); return new OrderService(repo); }); ``` ## Registration Patterns ### Single Interface ```vala container.register_singleton((s) => new ConsoleLogger()) .as_type(typeof(ILogger)); ``` ### Multiple Interfaces ```vala container.register_scoped((s) => new UserRepository()) .as_type(typeof(IUserReader)) .as_type(typeof(IUserWriter)) .as_type(typeof(IUserRepository)); ``` ### Implementation Type Only ```vala // Can be resolved as MyService but not as interface container.register_transient((s) => new MyService()); ``` ### Custom Factory Instance ```vala var factory = new MyCustomFactory(); container.register_factory_type(typeof(MyService), factory, Lifecycle.SINGLETON) .as_type(typeof(IMyService)); ``` ## Type Aliases Behavior When `as_type()` is called: 1. Type is added to internal set 2. `type_aliases` property is updated 3. `alias_added` signal is emitted 4. Container/Scope adds mapping for the new type ```vala var reg = container.register_singleton((s) => new Impl()); // At this point: reg.type_aliases contains [typeof(Impl)] reg.as_type(typeof(IService)); // Now: reg.type_aliases contains [typeof(Impl), typeof(IService)] // Container can now resolve both Impl and IService to this registration ``` ## Integration with Container Registrations are typically created via container methods: ```vala // These all return Registration for fluent configuration container.register(factory, lifecycle); container.register_transient(factory); container.register_scoped(factory); container.register_singleton(factory); ``` ## Integration with Scope Scope-local registrations use the same `Registration` class: ```vala scope.register_local_scoped((s) => new RequestContext()) .as_type(typeof(IContext)); ``` --- The Registration class encapsulates component registration with implementation type, service aliases, lifecycle, and factory, providing fluent configuration via `as_type()` method chaining.