From bf5933f27521e09470a5f279ac8a81574dcc50f5 Mon Sep 17 00:00:00 2001 From: Brendan Kowitz Date: Tue, 21 Mar 2023 15:22:34 -0700 Subject: [PATCH 1/2] Types registration uses TryAdd and TryAddEnumerable --- .../TypeRegistrationTests.cs | 48 +++++++++++++++++++ .../TypeRegistrationBuilder.cs | 37 +++++++------- 2 files changed, 69 insertions(+), 16 deletions(-) diff --git a/src/Microsoft.Health.Extensions.DependencyInjection.UnitTests/TypeRegistrationTests.cs b/src/Microsoft.Health.Extensions.DependencyInjection.UnitTests/TypeRegistrationTests.cs index 70f66c4e..1086a994 100644 --- a/src/Microsoft.Health.Extensions.DependencyInjection.UnitTests/TypeRegistrationTests.cs +++ b/src/Microsoft.Health.Extensions.DependencyInjection.UnitTests/TypeRegistrationTests.cs @@ -377,6 +377,54 @@ public void GivenADelegateFromTypeBuilder_WhenResolvingComponent_ThenResolverRet IComponent instance = componentFactory.Invoke(); Assert.IsType(instance); + + Assert.Equal(2, provider.GetServices().Count()); + } + + [Fact] + public void GivenATypeBuilder_WhenRegisteringMultipleTimes_ThenNoDuplicatesAreAdded() + { + _collection.Add() + .Transient() + .AsSelf() + .AsService(); + + _collection.Add() + .Transient() + .AsSelf() + .AsService(); + + var provider = _collection.BuildServiceProvider(); + var services = provider.GetServices(); + Assert.Single(services); + } + + [Fact] + public void GivenATypeBuilder_WhenRegisteringMultipleTimes_ThenErrorWithDifferentScopes() + { + _collection.Add() + .Transient() + .AsSelf() + .AsService(); + + _collection.Add() + .Singleton() + .AsSelf() + .AsService(); + + _collection.BuildServiceProvider(); + + Assert.Collection(_collection, r => + { + Assert.Equal(ServiceLifetime.Transient, r.Lifetime); + Assert.Equal(typeof(ComponentA), r.ServiceType); + Assert.Equal(typeof(ComponentA), r.ImplementationType); + }, r => + { + Assert.Equal(ServiceLifetime.Transient, r.Lifetime); + Assert.Equal(typeof(IComponent), r.ServiceType); + Assert.Equal(typeof(ComponentA), r.ImplementationType); + }); } [Fact] diff --git a/src/Microsoft.Health.Extensions.DependencyInjection/TypeRegistrationBuilder.cs b/src/Microsoft.Health.Extensions.DependencyInjection/TypeRegistrationBuilder.cs index 61e05213..5cab7dc0 100644 --- a/src/Microsoft.Health.Extensions.DependencyInjection/TypeRegistrationBuilder.cs +++ b/src/Microsoft.Health.Extensions.DependencyInjection/TypeRegistrationBuilder.cs @@ -212,14 +212,7 @@ private void RegisterType(Type serviceType, bool replace = false) serviceDescriptor = serviceDescriptor.WithMetadata(_type); - if (replace) - { - _serviceCollection.Replace(serviceDescriptor); - } - else - { - _serviceCollection.Add(serviceDescriptor); - } + AddToServiceCollection(serviceDescriptor, replace); } private void SetupRootRegistration(Type serviceType, bool replace) @@ -255,16 +248,28 @@ private void SetupRootRegistration(Type serviceType, bool replace) { serviceDescriptor = serviceDescriptor.WithMetadata(_type); - if (replace) - { - _serviceCollection.Replace(serviceDescriptor); - } - else - { - _serviceCollection.Add(serviceDescriptor); - } + AddToServiceCollection(serviceDescriptor, replace); } _cachedResolver = provider => provider.GetService(_firstRegisteredType); } + + private void AddToServiceCollection(ServiceDescriptor serviceDescriptor, bool replace) + { + if (replace) + { + _serviceCollection.RemoveAll(serviceDescriptor.ServiceType); + } + + if (serviceDescriptor.ServiceType == serviceDescriptor.ImplementationType || serviceDescriptor.ImplementationFactory != null) + { + // Concrete type registration + _serviceCollection.TryAdd(serviceDescriptor); + } + else + { + // Register services as enumerable + _serviceCollection.TryAddEnumerable(serviceDescriptor); + } + } } From 9daae1f7d5c54f684fc05f38bde78877e3ded649 Mon Sep 17 00:00:00 2001 From: Brendan Kowitz Date: Thu, 16 May 2024 09:20:09 -0700 Subject: [PATCH 2/2] Update src/Microsoft.Health.Extensions.DependencyInjection.UnitTests/TypeRegistrationTests.cs --- .../TypeRegistrationTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.Health.Extensions.DependencyInjection.UnitTests/TypeRegistrationTests.cs b/src/Microsoft.Health.Extensions.DependencyInjection.UnitTests/TypeRegistrationTests.cs index 1086a994..0df07a95 100644 --- a/src/Microsoft.Health.Extensions.DependencyInjection.UnitTests/TypeRegistrationTests.cs +++ b/src/Microsoft.Health.Extensions.DependencyInjection.UnitTests/TypeRegistrationTests.cs @@ -400,7 +400,7 @@ public void GivenATypeBuilder_WhenRegisteringMultipleTimes_ThenNoDuplicatesAreAd } [Fact] - public void GivenATypeBuilder_WhenRegisteringMultipleTimes_ThenErrorWithDifferentScopes() + public void GivenATypeBuilder_WhenRegisteringMultipleTimes_ThenSecondRegistrationIsIgnored() { _collection.Add() .Transient()