Skip to content
On this page

Bootstrapping a Container

To configure and bootstrap a Lamar container, you have a couple options. You can create a Container object with inline registrations:

cs
var container = new Container(x => { x.AddTransient<IClock, Clock>(); });

snippet source | anchor

Or pass in a configured ServiceRegistry object as shown below:

cs
// Create a Lamar.ServiceRegistry object
// and define your service registrations
var registry = new ServiceRegistry();

// Use ASP.Net Core style registrations
// for basic functionality
registry.AddSingleton<IClock, Clock>();
registry.AddTransient<IWidget, RedWidget>();

// Or use StructureMap style registration syntax
// as an alternative or to use more advanced usage
registry.For<IClockFactory>()
    .Use<ClockFactory>()
    .Singleton();

var container = new Container(registry);

snippet source | anchor

Lamar's ServiceRegistry supports a subset of StructureMap's old Registry class and should be used as a replacement when replacing StructureMap with Lamar. We renamed the class to disambiguate the name from the many other Registry classes in the CLR. ServiceRegistry implements the IServiceCollection interface from ASP.Net Core. You can also create a Lamar container by passing in an instance of IServiceCollection like you'd get within an ASP.Net Core application.