using DryIoc; using Xunit; namespace ServiceResolver.Ioc.Test.Impl.DryIoc; public class ContainerTest { [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(); var consumerB = container.Resolve(); Assert.NotNull(consumerA); Assert.NotNull(consumerB); Assert.Equal(typeof(MyServiceA), consumerA.Service.GetType()); Assert.Equal(typeof(MyServiceB), consumerB.Service.GetType()); } [Fact] public void TestWithDefaultScopeToSingleton() { // issue opened // https://github.com/dadhi/DryIoc/discussions/630 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(); var consumerB = container.Resolve(); Assert.NotNull(consumerA); Assert.NotNull(consumerB); Assert.Equal(typeof(MyServiceA), consumerA.Service.GetType()); Assert.Equal(typeof(MyServiceB), consumerB.Service.GetType()); } } 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 { }