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

Type registration uses TryAdd and TryAddEnumerable #689

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,54 @@ public void GivenADelegateFromTypeBuilder_WhenResolvingComponent_ThenResolverRet
IComponent instance = componentFactory.Invoke();

Assert.IsType<ComponentB>(instance);

Assert.Equal(2, provider.GetServices<IComponent>().Count());
}

[Fact]
public void GivenATypeBuilder_WhenRegisteringMultipleTimes_ThenNoDuplicatesAreAdded()
{
_collection.Add<ComponentA>()
.Transient()
.AsSelf()
.AsService<IComponent>();

_collection.Add<ComponentA>()
.Transient()
.AsSelf()
.AsService<IComponent>();

var provider = _collection.BuildServiceProvider();
var services = provider.GetServices<IComponent>();
Assert.Single(services);
}

[Fact]
public void GivenATypeBuilder_WhenRegisteringMultipleTimes_ThenSecondRegistrationIsIgnored()
{
_collection.Add<ComponentA>()
.Transient()
.AsSelf()
.AsService<IComponent>();

_collection.Add<ComponentA>()
.Singleton()
.AsSelf()
.AsService<IComponent>();

_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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -255,16 +248,28 @@ private void SetupRootRegistration(Type serviceType, bool replace)
{
serviceDescriptor = serviceDescriptor.WithMetadata(_type);

if (replace)
{
_serviceCollection.Replace(serviceDescriptor);
brendankowitz marked this conversation as resolved.
Show resolved Hide resolved
}
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);
}
}
}