Introduce priority for preferred constructor (#10)

This commit is contained in:
2022-05-05 08:10:30 +02:00
committed by GitHub
parent 84728090d5
commit 76462bf368
6 changed files with 67 additions and 11 deletions
@@ -5,15 +5,29 @@ namespace Slim.Tests.Models
public sealed class ServiceWithPrefferedConstructor
{
public bool CalledPrefferedConstructor { get; init; }
public bool CalledPrefferedConstructor0 { get; init; }
public bool CalledPrefferedConstructor1 { get; init; }
public ServiceWithPrefferedConstructor()
{
}
[PrefferedConstructor]
[PreferredConstructor]
public ServiceWithPrefferedConstructor(IIndependentService independentService)
{
this.CalledPrefferedConstructor = true;
}
[PreferredConstructor(Priority = 0)]
public ServiceWithPrefferedConstructor(IDependentService dependentService)
{
this.CalledPrefferedConstructor0 = true;
}
[PreferredConstructor(Priority = 1)]
public ServiceWithPrefferedConstructor(IDependentService2 dependentService2)
{
this.CalledPrefferedConstructor1 = true;
}
}
}
+29
View File
@@ -750,5 +750,34 @@ namespace Slim.Tests
service.CalledPrefferedConstructor.Should().BeFalse();
}
[TestMethod]
public void ServiceWithPrefferedConstructorCallsPrefferedConstructorWithHighestPriority()
{
var di = new ServiceManager();
di.RegisterSingleton<IIndependentService, IndependentService>();
di.RegisterSingleton<IndependentService>();
di.RegisterSingleton<IDependentService2, DependentService2>();
di.RegisterSingleton<IDependentService, DependentService>();
di.RegisterSingleton<ServiceWithPrefferedConstructor>();
var service = di.GetService<ServiceWithPrefferedConstructor>();
service.CalledPrefferedConstructor0.Should().BeTrue();
}
[TestMethod]
public void ServiceWithPrefferedConstructorCallsPrefferedConstructorWithSecondHighestPriority()
{
var di = new ServiceManager();
di.RegisterSingleton<IIndependentService, IndependentService>();
di.RegisterSingleton<IndependentService>();
di.RegisterSingleton<IDependentService2, DependentService2>();
di.RegisterSingleton<ServiceWithPrefferedConstructor>();
var service = di.GetService<ServiceWithPrefferedConstructor>();
service.CalledPrefferedConstructor1.Should().BeTrue();
}
}
}
@@ -6,12 +6,17 @@ namespace Slim.Attributes
/// Attribute used to mark constructors to be used with priority by the <see cref="ServiceManager"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Constructor)]
public sealed class PrefferedConstructorAttribute : Attribute
public sealed class PreferredConstructorAttribute : Attribute
{
/// <summary>
/// Creates a new instance of <see cref="PrefferedConstructorAttribute"/>.
/// Priority of the constructor. Constructors are ordered using this value when present.
/// </summary>
public PrefferedConstructorAttribute()
public int Priority { get; set; } = int.MaxValue - 1;
/// <summary>
/// Creates a new instance of <see cref="PreferredConstructorAttribute"/>.
/// </summary>
public PreferredConstructorAttribute()
{
}
}
+4 -1
View File
@@ -687,7 +687,10 @@ namespace Slim
* Order constructors to give priority to constructors that have PrefferedConstructorAttribute decorator
*/
var constructors = implementType.GetConstructors()
.OrderBy(c => c.GetCustomAttribute<PrefferedConstructorAttribute>() is null);
.OrderBy(c =>
c.GetCustomAttribute<PreferredConstructorAttribute>() is PreferredConstructorAttribute preferredConstructorAttribute ?
preferredConstructorAttribute.Priority :
int.MaxValue);
foreach (var constructor in constructors)
{
/*
+3 -3
View File
@@ -8,10 +8,10 @@
<Company />
<Product></Product>
<Description>Service lifetime management and dependency injection framework.</Description>
<AssemblyVersion>1.7.1</AssemblyVersion>
<FileVersion>1.7.1</FileVersion>
<AssemblyVersion>1.7.2</AssemblyVersion>
<FileVersion>1.7.2</FileVersion>
<PackageProjectUrl>https://github.com/AlexMacocian/Slim</PackageProjectUrl>
<Version>1.7.1</Version>
<Version>1.7.2</Version>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
+8 -3
View File
@@ -4,14 +4,19 @@
<name>Slim</name>
</assembly>
<members>
<member name="T:Slim.Attributes.PrefferedConstructorAttribute">
<member name="T:Slim.Attributes.PreferredConstructorAttribute">
<summary>
Attribute used to mark constructors to be used with priority by the <see cref="T:Slim.ServiceManager"/>.
</summary>
</member>
<member name="M:Slim.Attributes.PrefferedConstructorAttribute.#ctor">
<member name="P:Slim.Attributes.PreferredConstructorAttribute.Priority">
<summary>
Creates a new instance of <see cref="T:Slim.Attributes.PrefferedConstructorAttribute"/>.
Priority of the constructor. Constructors are ordered using this value when present.
</summary>
</member>
<member name="M:Slim.Attributes.PreferredConstructorAttribute.#ctor">
<summary>
Creates a new instance of <see cref="T:Slim.Attributes.PreferredConstructorAttribute"/>.
</summary>
</member>
<member name="T:Slim.Exceptions.DependencyInjectionException">