|
| 1 | +using Autofac.Extras.IocManager.TestBase; |
| 2 | +using Autofac.Features.ResolveAnything; |
| 3 | + |
| 4 | +using Shouldly; |
| 5 | + |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace Autofac.Extras.IocManager.Tests |
| 9 | +{ |
| 10 | + public class IfNotRegistered_Tests : TestBaseWithIocBuilder |
| 11 | + { |
| 12 | + [Fact] |
| 13 | + public void if_not_registered_should_work() |
| 14 | + { |
| 15 | + Building(builder => |
| 16 | + { |
| 17 | + builder.RegisterServices(r => r.UseBuilder(cb => |
| 18 | + { |
| 19 | + cb.RegisterType<ServiceA>().As<IService>(); |
| 20 | + cb.RegisterType<ServiceB>().As<IService>().IfNotRegistered(typeof(ServiceA)); |
| 21 | + })); |
| 22 | + }); |
| 23 | + |
| 24 | + LocalIocManager.IsRegistered<ServiceB>().ShouldBe(false); |
| 25 | + } |
| 26 | + |
| 27 | + [Fact] |
| 28 | + public void RegisterIfAbsent_should_work_with_generic_T() |
| 29 | + { |
| 30 | + Building(builder => |
| 31 | + { |
| 32 | + builder.RegisterServices(r => |
| 33 | + { |
| 34 | + r.Register<IService, ServiceA>(); |
| 35 | + r.RegisterIfAbsent<IService, ServiceB>(); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + LocalIocManager.IsRegistered(typeof(ServiceB)).ShouldBe(false); |
| 40 | + LocalIocManager.Resolve<IService>().ShouldBeAssignableTo<ServiceA>(); |
| 41 | + } |
| 42 | + |
| 43 | + [Fact] |
| 44 | + public void RegisterIfAbsent_should_work_with_type_parameters_() |
| 45 | + { |
| 46 | + Building(builder => |
| 47 | + { |
| 48 | + builder.RegisterServices(r => |
| 49 | + { |
| 50 | + r.Register<IService, ServiceA>(); |
| 51 | + r.RegisterIfAbsent(typeof(IService), typeof(ServiceB)); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + LocalIocManager.IsRegistered(typeof(ServiceB)).ShouldBe(false); |
| 56 | + LocalIocManager.Resolve<IService>().ShouldBeAssignableTo<ServiceA>(); |
| 57 | + } |
| 58 | + |
| 59 | + [Fact] |
| 60 | + public void RegisterIfAbsent_should_work_with_type_parameters_propertyinjection_should_not_be_null() |
| 61 | + { |
| 62 | + Building(builder => |
| 63 | + { |
| 64 | + builder.RegisterServices(r => |
| 65 | + { |
| 66 | + r.RegisterIfAbsent(typeof(MyClass)); |
| 67 | + r.RegisterIfAbsent(typeof(IService), typeof(ServiceB)); |
| 68 | + r.RegisterIfAbsent<MyClass>(); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + LocalIocManager.IsRegistered(typeof(ServiceB)).ShouldBe(true); |
| 73 | + LocalIocManager.Resolve<IService>().ShouldBeAssignableTo<ServiceB>(); |
| 74 | + LocalIocManager.Resolve<IService>().MyProperty.ShouldNotBeNull(); |
| 75 | + } |
| 76 | + |
| 77 | + [Fact] |
| 78 | + public void RegisterIfAbsent_should_work_with_TGeneric_propertyinjection_should_not_be_null() |
| 79 | + { |
| 80 | + Building(builder => |
| 81 | + { |
| 82 | + builder.RegisterServices(r => |
| 83 | + { |
| 84 | + r.RegisterType(typeof(MyClass)); |
| 85 | + r.RegisterIfAbsent<IService, ServiceB>(); |
| 86 | + r.RegisterIfAbsent<MyClass>(); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + LocalIocManager.IsRegistered(typeof(ServiceB)).ShouldBe(true); |
| 91 | + LocalIocManager.Resolve<IService>().ShouldBeAssignableTo<ServiceB>(); |
| 92 | + LocalIocManager.Resolve<IService>().MyProperty.ShouldNotBeNull(); |
| 93 | + } |
| 94 | + |
| 95 | + [Fact] |
| 96 | + public void RegisterIfAbsent_should_work_on_propertyInjection() |
| 97 | + { |
| 98 | + Building(builder => |
| 99 | + { |
| 100 | + builder.RegisterServices(r => |
| 101 | + { |
| 102 | + r.RegisterIfAbsent<IService, ServiceA>(); |
| 103 | + r.RegisterIfAbsent(typeof(IService), typeof(ServiceB)); |
| 104 | + r.RegisterType(typeof(MyClass)); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + LocalIocManager.IsRegistered(typeof(ServiceB)).ShouldBe(false); |
| 109 | + LocalIocManager.Resolve<IService>().ShouldBeAssignableTo<ServiceA>(); |
| 110 | + |
| 111 | + var instanceA = LocalIocManager.Resolve<IService>(); |
| 112 | + instanceA.MyProperty.ShouldNotBeNull(); |
| 113 | + } |
| 114 | + |
| 115 | + [Fact] |
| 116 | + public void IfNotRegistered_PropertyInjection_should_work() |
| 117 | + { |
| 118 | + var builder = new ContainerBuilder(); |
| 119 | + builder.RegisterType<MyClass>().AsSelf().IfNotRegistered(typeof(MyClass)); |
| 120 | + builder.RegisterType<ServiceA>().As<IService>().IfNotRegistered(typeof(ServiceA)).AsSelf().PropertiesAutowired(); |
| 121 | + IContainer resolver = builder.Build(); |
| 122 | + |
| 123 | + var serviceAInstance = resolver.Resolve<ServiceA>(); |
| 124 | + serviceAInstance.MyProperty.ShouldNotBeNull(); |
| 125 | + } |
| 126 | + |
| 127 | + [Fact] |
| 128 | + public void IfNotRegistered_PropertyInjection_should_work_single() |
| 129 | + { |
| 130 | + var builder = new ContainerBuilder(); |
| 131 | + builder.RegisterType<MyClass>().AsSelf().IfNotRegistered(typeof(MyClass)); |
| 132 | + builder.RegisterType<ServiceA>().AsSelf().IfNotRegistered(typeof(ServiceA)).PropertiesAutowired(); |
| 133 | + IContainer resolver = builder.Build(); |
| 134 | + |
| 135 | + var serviceAInstance = resolver.Resolve<ServiceA>(); |
| 136 | + serviceAInstance.MyProperty.ShouldNotBeNull(); |
| 137 | + } |
| 138 | + |
| 139 | + internal class ServiceA : IService |
| 140 | + { |
| 141 | + public MyClass MyProperty { get; set; } |
| 142 | + } |
| 143 | + |
| 144 | + internal interface IService |
| 145 | + { |
| 146 | + MyClass MyProperty { get; set; } |
| 147 | + } |
| 148 | + |
| 149 | + internal class ServiceB : IService |
| 150 | + { |
| 151 | + public MyClass MyProperty { get; set; } |
| 152 | + } |
| 153 | + |
| 154 | + internal class MyClass |
| 155 | + { |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments