JasperFx / lamar

Fast Inversion of Control Tool and Successor to StructureMap
https://jasperfx.github.io/lamar
MIT License
566 stars 117 forks source link

AddInstances with ServiceRegistry #221

Closed philwindsor closed 4 years ago

philwindsor commented 4 years ago

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?

Any help is appreciated :)

I have also asked this question on stackoverflow https://stackoverflow.com/questions/59324724/lamar-addinstances-with-serviceregistry

philwindsor commented 4 years ago

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

services.For().Add().Named("1"); services.For().Add().Named("2");

hisuwh commented 2 years ago

They need to remove this from the documentation if its deprecated