Child containers and scopes

Every yocto container is also a scope.

using var root = new Container();
root.Register<IClock, SystemClock>().AsSingleton();
root.Register<IUnitOfWork, UnitOfWork>().AsScoped();

using (var child = root.CreateChildContainer())
{
    child.Register<IClock, FrozenClock>();       // override just for this child
    var uow = child.Resolve<IUnitOfWork>();      // one per child
}                                                // child and its instances are disposed here

ContainerBuilder

using var builder = new ContainerBuilder(parent: null) { ValidateOnBuild = true };
builder.Register<IClock, SystemClock>();
builder.RegisterModule<BillingModule>();
IContainer container = builder.Build();   // once; later calls throw

Application.Current

An optional ambient container for places that can’t use constructor injection, such as XAML:

Application.Current.Register<IClock, SystemClock>();
var scope = Application.Push();   // child of Current, now Current
// ...
Application.Pop();                // disposes it and restores the parent

Prefer constructor injection everywhere else.

Next: Modules and assembly registration →

Last reviewed October 13, 2026 · yocto 3.0.0