Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Use equals for TryAdd and Replace
  • Loading branch information
tommysor committed Dec 9, 2023
commit d5e2d66d07470f4501683cb28a71f5960cb70ff0
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static void TryAdd(
for (int i = 0; i < count; i++)
{
if (collection[i].ServiceType == descriptor.ServiceType
&& collection[i].ServiceKey == descriptor.ServiceKey)
&& object.Equals(collection[i].ServiceKey, descriptor.ServiceKey))
{
// Already added
return;
Expand Down Expand Up @@ -474,7 +474,7 @@ public static void TryAddEnumerable(
ServiceDescriptor service = services[i];
if (service.ServiceType == descriptor.ServiceType &&
service.GetImplementationType() == implementationType &&
service.ServiceKey == descriptor.ServiceKey)
object.Equals(service.ServiceKey, descriptor.ServiceKey))
{
// Already added
return;
Expand Down Expand Up @@ -532,7 +532,7 @@ public static IServiceCollection Replace(
int count = collection.Count;
for (int i = 0; i < count; i++)
{
if (collection[i].ServiceType == descriptor.ServiceType && collection[i].ServiceKey == descriptor.ServiceKey)
if (collection[i].ServiceType == descriptor.ServiceType && object.Equals(collection[i].ServiceKey, descriptor.ServiceKey))
{
collection.RemoveAt(i);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public static TheoryData TryAddImplementationTypeData
{ collection => collection.TryAddKeyedTransient<IFakeService, FakeService>("key-2"), serviceType, "key-2", implementationType, ServiceLifetime.Transient },
{ collection => collection.TryAddKeyedTransient<IFakeService>("key-3"), serviceType, "key-3", serviceType, ServiceLifetime.Transient },
{ collection => collection.TryAddKeyedTransient(implementationType, "key-4"), implementationType, "key-4", implementationType, ServiceLifetime.Transient },
{ collection => collection.TryAddKeyedTransient(implementationType, 9), implementationType, 9, implementationType, ServiceLifetime.Transient },

{ collection => collection.TryAddKeyedScoped(serviceType, "key-1", implementationType), serviceType, "key-1", implementationType, ServiceLifetime.Scoped },
{ collection => collection.TryAddKeyedScoped<IFakeService, FakeService>("key-2"), serviceType, "key-2", implementationType, ServiceLifetime.Scoped },
Expand Down Expand Up @@ -325,6 +326,40 @@ public void TryAddEnumerable_DoesNotAddDuplicate(
Assert.Equal(expectedLifetime, d.Lifetime);
}

[Fact]
public void TryAddEnumerable_DoesNotAddDuplicateWhenKeyIsInt()
{
// Arrange
var collection = new ServiceCollection();
var descriptor1 = ServiceDescriptor.KeyedTransient<IFakeService, FakeService>(1);
collection.TryAddEnumerable(descriptor1);
var descriptor2 = ServiceDescriptor.KeyedTransient<IFakeService, FakeService>(1);

// Act
collection.TryAddEnumerable(descriptor2);

// Assert
var d = Assert.Single(collection);
Assert.Same(descriptor1, d);
}

[Fact]
public void TryAddEnumerable_DoesNotAddDuplicateWhenKeyIsString()
{
// Arrange
var collection = new ServiceCollection();
var descriptor1 = ServiceDescriptor.KeyedTransient<IFakeService, FakeService>("service1");
collection.TryAddEnumerable(descriptor1);
var descriptor2 = ServiceDescriptor.KeyedTransient<IFakeService, FakeService>("service1");

// Act
collection.TryAddEnumerable(descriptor2);

// Assert
var d = Assert.Single(collection);
Assert.Same(descriptor1, d);
}

public static TheoryData TryAddEnumerableInvalidImplementationTypeData
{
get
Expand Down Expand Up @@ -412,6 +447,24 @@ public void Replace_ReplacesFirstServiceWithMatchingServiceType()
Assert.Equal(new[] { descriptor2, descriptor3 }, collection);
}

[Fact]
public void Replace_ReplacesFirstServiceWithMatchingServiceTypeWhenKeyIsInt()
{
// Arrange
var collection = new ServiceCollection();
var descriptor1 = new ServiceDescriptor(typeof(IFakeService), 1, typeof(FakeService), ServiceLifetime.Transient);
var descriptor2 = new ServiceDescriptor(typeof(IFakeService), 1, typeof(FakeService), ServiceLifetime.Transient);
collection.Add(descriptor1);
collection.Add(descriptor2);
var descriptor3 = new ServiceDescriptor(typeof(IFakeService), 1, typeof(FakeService), ServiceLifetime.Singleton);

// Act
collection.Replace(descriptor3);

// Assert
Assert.Equal(new[] { descriptor2, descriptor3 }, collection);
}

[Fact]
public void RemoveAll_RemovesAllServicesWithMatchingServiceType()
{
Expand All @@ -436,9 +489,10 @@ private enum ServiceKeyEnum { First, Second }
[Fact]
public void RemoveAll_RemovesAllMatchingServicesWhenKeyIsEnum()
{
var descriptor = new ServiceDescriptor(typeof(IFakeService), ServiceKeyEnum.First, typeof(FakeService), ServiceLifetime.Transient);
var collection = new ServiceCollection
{
new ServiceDescriptor(typeof(IFakeService), ServiceKeyEnum.First, typeof(FakeService), ServiceLifetime.Transient),
descriptor,
new ServiceDescriptor(typeof(IFakeService), ServiceKeyEnum.Second, typeof(FakeService), ServiceLifetime.Transient),
new ServiceDescriptor(typeof(IFakeService), ServiceKeyEnum.Second, typeof(FakeService), ServiceLifetime.Transient),
};
Expand All @@ -447,16 +501,16 @@ public void RemoveAll_RemovesAllMatchingServicesWhenKeyIsEnum()
collection.RemoveAllKeyed<IFakeService>(ServiceKeyEnum.Second);

// Assert
Assert.Contains(collection, x => ServiceKeyEnum.First.Equals(x.ServiceKey));
Assert.DoesNotContain(collection, x => ServiceKeyEnum.Second.Equals(x.ServiceKey));
Assert.Equal(new[] { descriptor }, collection);
}

[Fact]
public void RemoveAll_RemovesAllMatchingServicesWhenKeyIsInt()
{
var descriptor = new ServiceDescriptor(typeof(IFakeService), 1, typeof(FakeService), ServiceLifetime.Transient);
var collection = new ServiceCollection
{
new ServiceDescriptor(typeof(IFakeService), 1, typeof(FakeService), ServiceLifetime.Transient),
descriptor,
new ServiceDescriptor(typeof(IFakeService), 2, typeof(FakeService), ServiceLifetime.Transient),
new ServiceDescriptor(typeof(IFakeService), 2, typeof(FakeService), ServiceLifetime.Transient),
};
Expand All @@ -465,26 +519,7 @@ public void RemoveAll_RemovesAllMatchingServicesWhenKeyIsInt()
collection.RemoveAllKeyed<IFakeService>(2);

// Assert
Assert.Contains(collection, x => 1.Equals(x.ServiceKey));
Assert.DoesNotContain(collection, x => 2.Equals(x.ServiceKey));
}

[Fact]
public void RemoveAll_RemovesAllMatchingServicesWhenKeyIsDouble()
{
var collection = new ServiceCollection
{
new ServiceDescriptor(typeof(IFakeService), 1.0, typeof(FakeService), ServiceLifetime.Transient),
new ServiceDescriptor(typeof(IFakeService), 2.0, typeof(FakeService), ServiceLifetime.Transient),
new ServiceDescriptor(typeof(IFakeService), 2.0, typeof(FakeService), ServiceLifetime.Transient),
};

// Act
collection.RemoveAllKeyed<IFakeService>(2.0);

// Assert
Assert.Contains(collection, x => 1.0.Equals(x.ServiceKey));
Assert.DoesNotContain(collection, x => 2.0.Equals(x.ServiceKey));
Assert.Equal(new[] { descriptor }, collection);
}

public static TheoryData NullServiceKeyData
Expand Down