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

Conditional registrations #631

Open
dadhi opened this issue Feb 4, 2024 Discussed in #630 · 0 comments
Open

Conditional registrations #631

dadhi opened this issue Feb 4, 2024 Discussed in #630 · 0 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@dadhi
Copy link
Owner

dadhi commented Feb 4, 2024

Discussed in #630

Originally posted by TheHunter February 2, 2024

ContainerTest.txt

Hi everyone,
I'm finding a problem using a conditional registrations, when I use at the end one registration without condition, the previous registrations (with condition) don't work in one specific case, when registrations with condition have a lower scope than the default registration (without condition), and it seems that the default one have the precedence, ignoring previous conditional registrations.

Take a look to this example:

public class ServiceConsumerA(IService service)
{
    public IService Service { get; private set; } = service;
}

public class ServiceConsumerB(IService service)
{
    public IService Service { get; private set; } = service;
}

public class MyServiceA : IService { }

public class MyServiceB : IService { }

public interface IService { }
[Fact]
public void TestWithDefaultScopeToSingleton()
{
    var container = new Container();

    // it works even for Reuse.Singleton
    var scopeA = Reuse.Transient;
    var scopeB = Reuse.Singleton;

    container.Register(typeof(IService), typeof(MyServiceA),
        reuse: scopeA
        , setup: Setup.With(condition: request => request.DirectParent.ServiceType == typeof(ServiceConsumerA))
    );

    // the intention is having a default resolution if previous conditions aren't satisfied !!
    container.Register(typeof(IService), typeof(MyServiceB),
        reuse: scopeB
    );

    container.Register(typeof(ServiceConsumerA));
    container.Register(typeof(ServiceConsumerB));

    var consumerA = container.Resolve<ServiceConsumerA>();
    var consumerB = container.Resolve<ServiceConsumerB>();

    Assert.NotNull(consumerA);
    Assert.NotNull(consumerB);

    // here is the mismatch from consumerA.Service type !!.
    Assert.Equal(typeof(MyServiceA), consumerA.Service.GetType());
    Assert.Equal(typeof(MyServiceB), consumerB.Service.GetType());
}

If you try it only changing the scope of scopeA in Singleton it works.

[Fact]
public void TestWithIdenticalScopes()
{
    var container = new Container();

    // it works even for Reuse.Singleton
    var scopeA = Reuse.Transient;
    var scopeB = Reuse.Transient;

    container.Register(typeof(IService), typeof(MyServiceA),
        reuse: scopeA
        , setup: Setup.With(condition: request => request.DirectParent.ServiceType == typeof(ServiceConsumerA))
    );

    // the intention is having a default resolution if previous conditions aren't satisfied !!
    container.Register(typeof(IService), typeof(MyServiceB),
        reuse: scopeB
    );

    container.Register(typeof(ServiceConsumerA));
    container.Register(typeof(ServiceConsumerB));

    var consumerA = container.Resolve<ServiceConsumerA>();
    var consumerB = container.Resolve<ServiceConsumerB>();

    Assert.NotNull(consumerA);
    Assert.NotNull(consumerB);

    // those asserts work, so types match !
    Assert.Equal(typeof(MyServiceA), consumerA.Service.GetType());
    Assert.Equal(typeof(MyServiceB), consumerB.Service.GetType());
}

Probably something is wrong in the example, mainly in the registration of default registration (without condition).

The complete unit test is attached in this post.

Any help will be appreciated :).

Thanks

@dadhi dadhi added this to the v6.0.0 milestone Feb 4, 2024
@dadhi dadhi self-assigned this Feb 4, 2024
dadhi added a commit that referenced this issue Feb 14, 2024
@dadhi dadhi added the bug Something isn't working label Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant