JasperFx / lamar

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

FromKeyedServices attribute support #397

Open vadimi-ep opened 3 months ago

vadimi-ep commented 3 months ago

Hello, it looks like lamar does not support FromKeyedServicesAttribute which is available in .net8:

public class TestClient
{
    public TestClient(
        [FromKeyedServices("c1")] IMyClient myClient1,
        [FromKeyedServices("c2")] IMyClient myClient2)
    {
        var res = myClient1.GetValue() == myClient2.GetValue();
    }
}

// Container initialization code
var container = new Lamar.Container(x =>
{
    x.AddKeyedSingleton<IMyClient>("c1", (p, _) => new MyClient(1));
    x.AddKeyedSingleton<IMyClient>("c2", (p, _) => new MyClient(2));
    x.AddTransient<TestClient>();
});

var s = container.GetService<TestClient>();

container.GetService<TestClient>() ignores FromKeyedServices attribute and injects the same instance of the dependency instead of the keyed one.

jeremydmiller commented 1 month ago

@vadimi-ep Just reminding you that Lamar predates that Microsoft introduced attribute by quite a few years. This is an "I take pull requests" issue (and won't be that bad if you care).

Lamar has its own attribute for naming if that's all you need. Or use Lamar's own built in registration DSL for this that Lamar has supported for years before MS did the keyed service thing

vadimi commented 1 month ago

thanks, I'll try to work on the PR then