View on GitHub

SARAFF SOFTWARE

All products / Saraff.IoC

Nested Containers and Scopes

Nested containers let you create an isolated child context that can:

A scope (CreateScope()) is a nested container with a dedicated scoped cache and a convenient IServiceScope API.

using (var root = new ServiceContainer()) {
    root.Register<IClock, SystemClock>();

    using (var child = root.CreateNestedContainer()) {
        child.Register<IRequestContext, RequestContext>(ServiceLifetime.Scoped);

        var clock = child.Resolve<IClock>();   // from root
        var context = child.Resolve<IRequestContext>(); // owned by child
    } // child and its scoped services are disposed here
} // root is disposed here
flowchart TB
    Root["Root container<br/>IClock (singleton)"]
    Child["Nested container / scope<br/>IRequestContext (scoped)"]
    Grandchild["Nested container / scope"]

    Root -->|"CreateNestedContainer()<br/>CreateScope()"| Child
    Child -->|"CreateNestedContainer()<br/>CreateScope()"| Grandchild

    Root -.->|visible to child| Child
    Child -.->|visible to grandchild| Grandchild

Resolution order

Resolution starts at the current container and walks up to the root:

sequenceDiagram
    participant App
    participant Child as Nested container
    participant Root as Root container

    App->>Child: Resolve IClock
    Child->>Child: look in child registrations
    alt found
        Child-->>App: instance
    else not found
        Child->>Root: look in parent
        Root-->>Child: instance
        Child-->>App: instance
    end

Consequences:

using (var root = new ServiceContainer()) {
    root.Register<IGreeting, RootGreeting>(ServiceLifetime.Transient, "root");

    using (var child = root.CreateNestedContainer()) {
        child.Register<IGreeting, ChildGreeting>(ServiceLifetime.Transient, "child");

        child.ResolveNamed<IGreeting>("root");  // RootGreeting
        child.ResolveNamed<IGreeting>("child"); // ChildGreeting

        var all = child.ResolveAll<IGreeting>(); // ChildGreeting, RootGreeting
    }
}

Isolation

Registrations are isolated in the downward direction:

using (var root = new ServiceContainer()) {
    using (var child = root.CreateNestedContainer()) {
        child.Register<IGreeting, Greeting>();

        Assert.NotNull(child.Resolve<IGreeting>());
        Assert.Null(root.Resolve<IGreeting>()); // not visible to the parent
    }
}

This is useful for plugins, tests, request-specific overrides and feature modules.

Disposal

Nested containers have independent lifetimes:

var root = new ServiceContainer();
root.Register<IClock, SystemClock>();

var child = root.CreateNestedContainer();
var clock = child.Resolve<IClock>(); // singleton owned by root

child.Dispose();   // child is released; clock is still alive
root.Dispose();    // clock is disposed here
stateDiagram-v2
    [*] --> Active: CreateNestedContainer()
    Active --> Disposed: Dispose / DisposeAsync
    Active --> ParentDisposed: Parent disposed
    ParentDisposed --> Disposed
    Disposed --> [*]

Scopes

CreateScope() is the recommended way to model a request or a unit of work:

using (var scope = container.CreateScope()) {
    var request = scope.Resolve<IRequestContext>();
    var handler = scope.Resolve<RequestHandler>();

    handler.Handle(request);
}
  CreateNestedContainer() CreateScope()
Return type ServiceContainer IServiceScope
Scoped cache Yes (it is a container) Yes
ServiceProvider property No (container is a provider) Yes
Intended use Modules, plugins, isolation Requests, operations
Async methods Yes (ResolveAsync, DisposeAsync) Yes (ResolveAsync, DisposeAsync)
Dispose semantics Same Same

Use CreateScope() in application code. Use CreateNestedContainer() when you need the full container API or a long-lived child context.

Nested containers and lifetimes

flowchart TB
    Root["Root: singleton S"]
    ChildA["Scope A: scoped C1"]
    ChildB["Scope B: scoped C2"]

    Root --> ChildA
    Root --> ChildB

    ChildA -->|"Resolve S"| Root
    ChildB -->|"Resolve S"| Root
    ChildA -->|"Resolve C"| C1["C1"]
    ChildB -->|"Resolve C"| C2["C2"]

Cycle detection across containers

Cycle detection works across nested containers. If a dependency chain forms a cycle, the container throws CircularDependencyException with a readable trace:

IoC. Обнаружена циклическая зависимость в "MyApp.ServiceA".
Трассировка:
  в MyApp.ServiceA
  в MyApp.ServiceB
  в MyApp.ServiceA

Thread safety

Creating and disposing nested containers is thread-safe. Concurrent resolutions of a singleton return the same instance, and concurrent disposal does not deadlock or dispose an object twice.

See also