Skip to content
On this page

Registering Existing Objects

It's frequently common to register existing objects with a Lamar Container and there are overloads of the ServiceRegistry.For().Use(object) and ServiceRegistry.For().Add(object) methods to do just that:

cs
[Fact]
public void should_be_able_to_resolve_from_the_generic_family_expression()
{
    var widget = new AWidget();
    var container = new Container(x => x.For(typeof(IWidget)).Use(widget).Named("mine"));

    container.GetInstance<IWidget>("mine").ShouldBeTheSameAs(widget);
}

snippet source | anchor

Injecting an existing object into the Container makes it a de facto singleton, but the Container treats it with a special scope called ObjectLifecycle if you happen to look into the WhatDoIHave() diagnostics.

Lamar will attempt to call the IDisposable.Dispose() on any objects that are directly injected into a Container that implement IDisposable when the Container itself is disposed.