Search code examples
c#asp.net-coredependency-injectionioc-containerlamar

Lamar AddInstances with ServiceRegistry


I have a dotnet core website setup with Lamar, I have the following method in Startup.cs

public void ConfigureContainer(ServiceRegistry services)
{
  ...
}

I want to use AddInstances() as described in the documents at https://jasperfx.github.io/lamar/documentation/ioc/registration/registry-dsl/

Add Many Registrations with For().AddInstances()

// registry is a StructureMap Registry object
registry.For<IService>().AddInstances(x =>
{
    // Equivalent to For<IService>().Add<ColorService>().....
    x.Type<ColorService>().Named("Red").Ctor<string>("color").Is("Red");

    // Equivalent to For<IService>().Add(new ColorService("Yellow"))......
    x.Object(new ColorService("Yellow")).Named("Yellow");

    // Equivalent to For<IService>().Use(() => new ColorService("Purple"))....
    x.ConstructedBy(() => new ColorService("Purple")).Named("Purple");

    x.Type<ColorService>().Named("Decorated").Ctor<string>("color").Is("Orange");
});

However, AddInstances() does not exist, I get the following error

'ServiceRegistry.InstanceExpression' does not contain a definition for 'AddInstances' and no accessible extension method 'AddInstances' accepting a first argument of type 'ServiceRegistry.InstanceExpression' could be found (are you missing a using directive or an assembly reference?)

services.For<IService>().AddInstances(x =>
{
 ...

 });

Is this in another namespace? does it only exist on a StructureMap Registry object as per the example, if so how do I get that from the injected ServiceRegistry?


Solution

  • I guess the method has been deprecated as you can simply do...

    services.For<IService>().Add<Service1>().Named("1");
    services.For<IService>().Add<Service2>().Named("2");