DryIoc icon indicating copy to clipboard operation
DryIoc copied to clipboard

Cannot register multiple service impls in child container with default service key.

Open Amleto opened this issue 2 years ago • 4 comments


public class DryExamples
{
    [Fact] // This works ok.
    public void TransferMultipleThenResolveEnumerable()
    {
        var services = new ServiceCollection();

        services.AddScoped<IPrinter, Printer>();
        services.AddScoped<IPrinter, PrinterA>();
        services.AddScoped<IPrinter, PrinterB>();
        services.AddScoped<IPrinter, NeighborPrinter>();

        var spf = new DryIocServiceProviderFactory();
        var dryContainer = spf.CreateBuilder(services);
        var msContainer = dryContainer.GetServiceProvider();

        Assert.Equal(
            dryContainer.Resolve<IEnumerable<IPrinter>>().Count(),
            msContainer.GetRequiredService<IEnumerable<IPrinter>>().Count());
    }

    [Fact] // I have not been able to get this to work.
    public void TransferMultipleThenResolveEnumerableFromChild()
    {
        var services = new ServiceCollection();

        services.AddScoped<IPrinter, Printer>();
        services.AddScoped<IPrinter, PrinterA>();
        services.AddScoped<IPrinter, PrinterB>();
        services.AddScoped<IPrinter, NeighborPrinter>();

        var spf = new DryIocServiceProviderFactory();
        var rootContainer = spf.CreateBuilder(new ServiceCollection());
        var childContainer = rootContainer.CreateChild(RegistrySharing.Share, "child-stamp", IfAlreadyRegistered.AppendNewImplementation);
        //childContainer.Populate(services);
        foreach (var service in services)
        {
            childContainer.RegisterDescriptor(service, IfAlreadyRegistered.AppendNewImplementation, "child-stamp");
        }
        var msContainer = childContainer.GetServiceProvider();

        Assert.Equal(
            childContainer.Resolve<IEnumerable<IPrinter>>().Count(),
            msContainer.GetRequiredService<IEnumerable<IPrinter>>().Count());
    }
}

private interface IPrinter{}

private class Printer : IPrinter{}

private class PrinterA : IPrinter{}

private class PrinterB : IPrinter{}

private class NeighborPrinter : IPrinter{}

The first test works, and I expect to be able to do similar with a child container. However, the fact that the child container has a default key seems to break this expectation.

childContainer.Populate(services); hard-codes append-if-not-keyed, so I tried calling childContainer.RegisterDescriptor in the loop, but discovered that AppendNewImplementation and AppendNotKeyed are basically not allowed in Regsitry.WithKeyedService(...).

Functionally, I'm looking to move away from a setup with unity container that has child containers. ie child containers can resolve from parent, but cannot interfere with parent registrations. Parent cannot see child services/registrations.

Please let me know if this is achievable.

I will update IssuesTests shortly.

Amleto avatar May 09 '23 12:05 Amleto

Hi @Amleto

Yes, in regard to childContainer.RegisterDescriptor(service, IfAlreadyRegistered.AppendNewImplementation, "child-stamp"); The IfAlreadyRegistered.AppendNewImplementation is incompatible with the service key. And yes, it is not great that the fact is not reflected by API.

So you have uncovered (for me too) that using the unique key won't allow the multiple default implementations in the child containers.

I think you may use the registration metadata to achieve this behavior, because it is not supposed to be unique. But currently there is no simple way to provide such as rules. I will think on it.

dadhi avatar May 10 '23 09:05 dadhi

@Amleto A duplicate #573 ?

dadhi avatar May 10 '23 09:05 dadhi

Yes. Unfortunately I filed this/these report yesterday when GitHub was having troubles. I got a lot of error responses before one was successful. Seems GitHub reported an error to me even though it posted ok.

Amleto avatar May 10 '23 09:05 Amleto