Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Amleto opened this issue May 9, 2023 · 3 comments
Assignees
Milestone

Comments

@Amleto
Copy link
Contributor

Amleto commented May 9, 2023

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.

dadhi added a commit that referenced this issue May 10, 2023
Add failing test for Gh issue #574
@dadhi dadhi self-assigned this May 10, 2023
@dadhi
Copy link
Owner

dadhi commented May 10, 2023

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 dadhi added this to the v6.0.0 milestone May 10, 2023
@dadhi
Copy link
Owner

dadhi commented May 10, 2023

@Amleto A duplicate #573 ?

@Amleto
Copy link
Contributor Author

Amleto commented May 10, 2023 via email

dadhi added a commit that referenced this issue May 27, 2023
dadhi added a commit that referenced this issue May 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants