library.inversion.registration.md 4.1 KB

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

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<Type> 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.

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.

container.register_singleton<MyImplementation>((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.

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

public delegate Object FactoryDelegate(Scope scope) throws Error;

Factory delegate for custom instantiation logic. Receives the current scope for dependency resolution.

Usage

// Simple factory
container.register_transient<MyService>((scope) => new MyService());

// Factory with dependencies
container.register_transient<OrderService>((scope) => {
    var repo = scope.resolve<IOrderRepository>();
    return new OrderService(repo);
});

Registration Patterns

Single Interface

container.register_singleton<ConsoleLogger>((s) => new ConsoleLogger())
    .as_type(typeof(ILogger));

Multiple Interfaces

container.register_scoped<UserRepository>((s) => new UserRepository())
    .as_type(typeof(IUserReader))
    .as_type(typeof(IUserWriter))
    .as_type(typeof(IUserRepository));

Implementation Type Only

// Can be resolved as MyService but not as interface
container.register_transient<MyService>((s) => new MyService());

Custom Factory Instance

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

    var reg = container.register_singleton<Impl>((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:

// These all return Registration for fluent configuration
container.register<T>(factory, lifecycle);
container.register_transient<T>(factory);
container.register_scoped<T>(factory);
container.register_singleton<T>(factory);

Integration with Scope

Scope-local registrations use the same Registration class:

scope.register_local_scoped<RequestContext>((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.