public void simple_Multiple_services_discovery() { var collector = TestHelper.CreateStObjCollector(); collector.RegisterType(typeof(S1)); collector.RegisterType(typeof(S2)); var result = TestHelper.CreateAutomaticServices(collector); try { result.Map.Services.SimpleMappings.ContainsKey(typeof(IHostedService)).Should().BeFalse(); IStObjServiceClassDescriptor s1 = result.Map.Services.SimpleMappings[typeof(S1)]; IStObjServiceClassDescriptor s2 = result.Map.Services.SimpleMappings[typeof(S2)]; s1.MultipleMappings.Should().BeEquivalentTo(new[] { typeof(IHostedService) }); s2.MultipleMappings.Should().BeEquivalentTo(new[] { typeof(IHostedService) }); s1.IsScoped.Should().BeFalse("Nothing prevents S1 to be singleton."); s2.IsScoped.Should().BeFalse("Nothing prevents S2 to be singleton."); var hosts = result.Services.GetRequiredService <IEnumerable <IHostedService> >(); hosts.Should().HaveCount(2); } finally { result.Services.Dispose(); } }
public void registered_AutoService_Marshaller_handles_only_the_exact_type() { var collector = TestHelper.CreateStObjCollector(); collector.RegisterType(typeof(FrontService1)); collector.RegisterType(typeof(MService1)); var map = TestHelper.GetSuccessfulResult(collector).EngineMap; Debug.Assert(map != null, "No initialization error."); IStObjServiceClassDescriptor d1 = map.Services.SimpleMappings[typeof(IFrontService1)]; d1.AutoServiceKind.Should().Be(AutoServiceKind.IsFrontProcessService | AutoServiceKind.IsFrontService | AutoServiceKind.IsMarshallable | AutoServiceKind.IsScoped); var dM = map.Services.SimpleMappings[typeof(MService1)]; var dMClass = map.Services.SimpleMappings[typeof(Model.IMarshaller <FrontService1>)]; dM.Should().NotBeNull(); dM.IsScoped.Should().BeFalse("Nothing prevents the marshaller to be singleton."); dM.AutoServiceKind.Should().Be(AutoServiceKind.IsSingleton, "A marshaller is not a Front service."); dMClass.Should().BeSameAs(dM); map.Services.SimpleMappings.ContainsKey(typeof(Model.IMarshaller <IFrontService1>)) .Should().BeFalse("Mashalling the IService MUST be explicitly supported by the marshaller implementation."); }
public void an_impl_that_depends_on_a_front_service_is_a_Front_service() { var collector = TestHelper.CreateStObjCollector(); collector.RegisterType(typeof(FrontService1)); collector.RegisterType(typeof(FrontDependentService1)); var map = TestHelper.GetSuccessfulResult(collector).EngineMap; Debug.Assert(map != null, "No initialization error."); IStObjServiceClassDescriptor descriptor = map.Services.SimpleMappings[typeof(FrontDependentService1)]; descriptor.AutoServiceKind.Should().Be(AutoServiceKind.IsFrontProcessService | AutoServiceKind.IsFrontService | AutoServiceKind.IsScoped); }
public void registering_a_Frontservice_with_its_AutoService_Marshaller_makes_it_marshallable() { var collector = TestHelper.CreateStObjCollector(); collector.RegisterType(typeof(FrontService1)); collector.RegisterType(typeof(MService1)); var map = TestHelper.GetSuccessfulResult(collector).EngineMap; Debug.Assert(map != null, "No initialization error."); IStObjServiceClassDescriptor dI = map.Services.SimpleMappings[typeof(IFrontService1)]; IStObjServiceClassDescriptor dC = map.Services.SimpleMappings[typeof(FrontService1)]; dI.Should().BeSameAs(dC); dI.AutoServiceKind.Should().Be(AutoServiceKind.IsFrontProcessService | AutoServiceKind.IsFrontService | AutoServiceKind.IsMarshallable | AutoServiceKind.IsScoped); }
public void simple_front_only_registration() { var collector = TestHelper.CreateStObjCollector(); collector.SetAutoServiceKind(typeof(FrontService1), AutoServiceKind.IsScoped); collector.RegisterType(typeof(FrontService1)); var map = TestHelper.GetSuccessfulResult(collector).EngineMap; Debug.Assert(map != null, "No initialization error."); IStObjServiceClassDescriptor descriptor = map.Services.SimpleMappings[typeof(IFrontService1)]; descriptor.Should().BeSameAs(map.Services.SimpleMappings[typeof(FrontService1)]); descriptor.IsScoped.Should().BeTrue(); descriptor.AutoServiceKind.Should().Be(AutoServiceKind.IsFrontProcessService | AutoServiceKind.IsFrontService | AutoServiceKind.IsScoped); }
public void Front_services_are_transitively_propagated_through_the_constructors() { var collector = TestHelper.CreateStObjCollector(); collector.RegisterType(typeof(FrontDependentService2)); collector.RegisterType(typeof(FrontDependentService1)); collector.RegisterType(typeof(FrontService1)); var map = TestHelper.GetSuccessfulResult(collector).EngineMap; Debug.Assert(map != null, "No initialization error."); IStObjServiceClassDescriptor dDep2 = map.Services.SimpleMappings[typeof(IFrontDependentService2)]; IStObjServiceClassDescriptor dDep1 = map.Services.SimpleMappings[typeof(FrontDependentService1)]; IStObjServiceClassDescriptor d1 = map.Services.SimpleMappings[typeof(IFrontService1)]; dDep2.AutoServiceKind.Should().Be(AutoServiceKind.IsFrontProcessService | AutoServiceKind.IsFrontService | AutoServiceKind.IsScoped); dDep1.AutoServiceKind.Should().Be(AutoServiceKind.IsFrontProcessService | AutoServiceKind.IsFrontService | AutoServiceKind.IsScoped); d1.AutoServiceKind.Should().Be(AutoServiceKind.IsFrontProcessService | AutoServiceKind.IsFrontService | AutoServiceKind.IsScoped); }
public void IsMutiple_works_on_external_interfaces_and_this_is_the_magic_for_IHostedService_auto_registering() { // Here class HNot is a IOfficialHostedService but not an IAutoService and not explicitly registered: it is not automatically registered. { var collector = TestHelper.CreateStObjCollector(); collector.SetAutoServiceKind(typeof(IOfficialHostedService), AutoServiceKind.IsMultipleService); collector.RegisterType(typeof(H1)); collector.RegisterType(typeof(H2)); collector.RegisterType(typeof(HNot)); var result = TestHelper.CreateAutomaticServices(collector); try { result.Map.Services.SimpleMappings.ContainsKey(typeof(IOfficialHostedService)).Should().BeFalse("A Multiple interface IS NOT mapped."); IStObjServiceClassDescriptor s1 = result.Map.Services.SimpleMappings[typeof(H1)]; IStObjServiceClassDescriptor s2 = result.Map.Services.SimpleMappings[typeof(H2)]; result.Map.Services.SimpleMappings.ContainsKey(typeof(HNot)).Should().BeFalse("HNot is not an AutoService!"); s1.MultipleMappings.Should().BeEquivalentTo(new[] { typeof(IOfficialHostedService) }); s2.MultipleMappings.Should().BeEquivalentTo(new[] { typeof(IOfficialHostedService) }); s1.IsScoped.Should().BeFalse("Nothing prevents H1 to be singleton."); s2.IsScoped.Should().BeTrue("H2 is IScopedAutoService."); var hosts = result.Services.GetRequiredService <IEnumerable <IOfficialHostedService> >(); hosts.Should().HaveCount(2, "Only H1 and H2 are considered."); } finally { result.Services.Dispose(); } } // Here, the HNot totally external service is registered in the ServiceCollection (at runtime): // it appears in the IEnumerable<IOfficialHostedService>. // Of course, in this case, no lifetime analysis can be done. { var collector = TestHelper.CreateStObjCollector(); collector.SetAutoServiceKind(typeof(IOfficialHostedService), AutoServiceKind.IsMultipleService); collector.RegisterType(typeof(H1)); collector.RegisterType(typeof(H2)); var result = TestHelper.CreateAutomaticServices(collector, configureServices: services => { // Here we use ServiceRegistrar that takes care of the registration with logs. services.Register(typeof(IOfficialHostedService), typeof(HNot), isScoped: false, allowMultipleRegistration: true); // // We could also have used the standard ServiceCollection registration of the mapping: // but without logs nor duplicate registration detection. // services.Services.AddScoped<IOfficialHostedService, HNot>(); }); try { result.Map.Services.SimpleMappings.ContainsKey(typeof(IOfficialHostedService)).Should().BeFalse("A Multiple interface IS NOT mapped."); IStObjServiceClassDescriptor s1 = result.Map.Services.SimpleMappings[typeof(H1)]; IStObjServiceClassDescriptor s2 = result.Map.Services.SimpleMappings[typeof(H2)]; result.Map.Services.SimpleMappings.ContainsKey(typeof(HNot)).Should().BeFalse("HNot is not an AutoService!"); s1.MultipleMappings.Should().BeEquivalentTo(new[] { typeof(IOfficialHostedService) }); s2.MultipleMappings.Should().BeEquivalentTo(new[] { typeof(IOfficialHostedService) }); s1.IsScoped.Should().BeFalse("Nothing prevents H1 to be singleton."); s2.IsScoped.Should().BeTrue("H2 is IScopedAutoService."); var hosts = result.Services.GetRequiredService <IEnumerable <IOfficialHostedService> >(); hosts.Should().HaveCount(3, "H1, H2 AND HNot are now considered. HNot appear in the DI container."); } finally { result.Services.Dispose(); } } }