-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add TryAddEnumerable * Bump version
- Loading branch information
Showing
20 changed files
with
224 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using Bindicate.Attributes.Enumerable; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
|
||
namespace Bindicate.Tests.Enumerable; | ||
|
||
public class TryAddEnumberableTests | ||
{ | ||
private readonly Assembly _testAssembly = typeof(TryAddEnumberableTests).Assembly; | ||
|
||
[Fact] | ||
public void TryAddEnumerable_MultipleImplementations_RegistersAll() | ||
{ | ||
// Arrange | ||
var services = new ServiceCollection(); | ||
services.AddAutowiringForAssembly(_testAssembly).Register(); | ||
var serviceProvider = services.BuildServiceProvider(); | ||
|
||
// Act | ||
var myServices = serviceProvider.GetServices<IMyService>().ToList(); | ||
|
||
// Assert | ||
myServices.Should().NotBeNull(); | ||
myServices.Count.Should().Be(2); | ||
myServices.Should().ContainSingle(s => s.GetType() == typeof(MyServiceA)); | ||
myServices.Should().ContainSingle(s => s.GetType() == typeof(MyServiceB)); | ||
} | ||
|
||
[Fact] | ||
public void TryAddEnumerable_DuplicateImplementation_NotRegisteredTwice() | ||
{ | ||
// Arrange | ||
var services = new ServiceCollection(); | ||
|
||
// Manually add MyServiceA to simulate duplicate registration | ||
services.TryAddEnumerable(ServiceDescriptor.Transient<IMyService, MyServiceA>()); | ||
services.AddAutowiringForAssembly(_testAssembly).Register(); | ||
var serviceProvider = services.BuildServiceProvider(); | ||
|
||
// Act | ||
var myServices = serviceProvider.GetServices<IMyService>().ToList(); | ||
|
||
// Assert | ||
myServices.Should().NotBeNull(); | ||
myServices.Count().Should().Be(2); | ||
myServices.Should().ContainSingle(s => s.GetType() == typeof(MyServiceA)); | ||
myServices.Should().ContainSingle(s => s.GetType() == typeof(MyServiceB)); | ||
} | ||
|
||
[Fact] | ||
public void TryAddEnumerable_Lifetime_IsRespected() | ||
{ | ||
// Arrange | ||
var services = new ServiceCollection(); | ||
services.AddAutowiringForAssembly(_testAssembly).Register(); | ||
var serviceProvider = services.BuildServiceProvider(); | ||
|
||
// Act | ||
var scope1Services = serviceProvider.GetServices<IMyService>().ToList(); | ||
var scope2Services = serviceProvider.GetServices<IMyService>().ToList(); | ||
|
||
// Assert | ||
// Since the services are registered as Transient, each resolve should return new instances | ||
scope1Services.Should().NotBeSameAs(scope2Services); | ||
|
||
var serviceA1 = scope1Services.First(s => s.GetType() == typeof(MyServiceA)); | ||
var serviceA2 = scope2Services.First(s => s.GetType() == typeof(MyServiceA)); | ||
|
||
serviceA1.Should().NotBeSameAs(serviceA2); | ||
} | ||
} | ||
|
||
public interface IMyService | ||
{ | ||
void Execute(); | ||
} | ||
|
||
[TryAddEnumerable(Lifetime.TryAddEnumerableTransient, typeof(IMyService))] | ||
public class MyServiceA : IMyService | ||
{ | ||
public void Execute() | ||
{ | ||
} | ||
} | ||
|
||
[TryAddEnumerable(Lifetime.TryAddEnumerableTransient, typeof(IMyService))] | ||
public class MyServiceB : IMyService | ||
{ | ||
public void Execute() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
global using System.Linq; | ||
global using FluentAssertions; | ||
global using System.Linq; | ||
global using System.Reflection; | ||
global using Xunit; | ||
global using FluentAssertions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/Bindicate/Attributes/Enumerable/TryAddEnumerableAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace Bindicate.Attributes.Enumerable; | ||
|
||
/// <summary> | ||
/// Specifies that a service should be registered using TryAddEnumerable with the dependency injection container. | ||
/// This allows multiple implementations of the same service type to be registered. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)] | ||
public class TryAddEnumerableAttribute : BaseServiceAttribute | ||
{ | ||
public override Lifetime Lifetime { get; } | ||
|
||
public TryAddEnumerableAttribute(Lifetime lifetime) | ||
{ | ||
Lifetime = lifetime; | ||
} | ||
|
||
public TryAddEnumerableAttribute(Lifetime lifetime, Type serviceType) : base(serviceType) | ||
{ | ||
Lifetime = lifetime; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 2 additions & 4 deletions
6
src/Bindicate/Attributes/Singleton/TryAddSingletonAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 2 additions & 4 deletions
6
src/Bindicate/Attributes/Transient/TryAddTransientAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.