From 76462bf368e7388e7e0ef1b34874c1d85a183019 Mon Sep 17 00:00:00 2001 From: Macocian Alexandru Victor Date: Thu, 5 May 2022 08:10:30 +0200 Subject: [PATCH] Introduce priority for preferred constructor (#10) --- .../Models/ServiceWithPrefferedConstructor.cs | 16 +++++++++- Slim.Tests/ServiceManagerTests.cs | 29 +++++++++++++++++++ .../PrefferedConstructorAttribute.cs | 11 +++++-- Slim/ServiceManager.cs | 5 +++- Slim/Slim.csproj | 6 ++-- Slim/Slim.xml | 11 +++++-- 6 files changed, 67 insertions(+), 11 deletions(-) diff --git a/Slim.Tests/Models/ServiceWithPrefferedConstructor.cs b/Slim.Tests/Models/ServiceWithPrefferedConstructor.cs index 1a996a9..e9510e8 100644 --- a/Slim.Tests/Models/ServiceWithPrefferedConstructor.cs +++ b/Slim.Tests/Models/ServiceWithPrefferedConstructor.cs @@ -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; + } } } diff --git a/Slim.Tests/ServiceManagerTests.cs b/Slim.Tests/ServiceManagerTests.cs index 230633b..3011ae3 100644 --- a/Slim.Tests/ServiceManagerTests.cs +++ b/Slim.Tests/ServiceManagerTests.cs @@ -750,5 +750,34 @@ namespace Slim.Tests service.CalledPrefferedConstructor.Should().BeFalse(); } + + [TestMethod] + public void ServiceWithPrefferedConstructorCallsPrefferedConstructorWithHighestPriority() + { + var di = new ServiceManager(); + di.RegisterSingleton(); + di.RegisterSingleton(); + di.RegisterSingleton(); + di.RegisterSingleton(); + di.RegisterSingleton(); + + var service = di.GetService(); + + service.CalledPrefferedConstructor0.Should().BeTrue(); + } + + [TestMethod] + public void ServiceWithPrefferedConstructorCallsPrefferedConstructorWithSecondHighestPriority() + { + var di = new ServiceManager(); + di.RegisterSingleton(); + di.RegisterSingleton(); + di.RegisterSingleton(); + di.RegisterSingleton(); + + var service = di.GetService(); + + service.CalledPrefferedConstructor1.Should().BeTrue(); + } } } diff --git a/Slim/Attributes/PrefferedConstructorAttribute.cs b/Slim/Attributes/PrefferedConstructorAttribute.cs index 69ac125..5a31a07 100644 --- a/Slim/Attributes/PrefferedConstructorAttribute.cs +++ b/Slim/Attributes/PrefferedConstructorAttribute.cs @@ -6,12 +6,17 @@ namespace Slim.Attributes /// Attribute used to mark constructors to be used with priority by the . /// [AttributeUsage(AttributeTargets.Constructor)] - public sealed class PrefferedConstructorAttribute : Attribute + public sealed class PreferredConstructorAttribute : Attribute { /// - /// Creates a new instance of . + /// Priority of the constructor. Constructors are ordered using this value when present. /// - public PrefferedConstructorAttribute() + public int Priority { get; set; } = int.MaxValue - 1; + + /// + /// Creates a new instance of . + /// + public PreferredConstructorAttribute() { } } diff --git a/Slim/ServiceManager.cs b/Slim/ServiceManager.cs index dec503c..cb17524 100644 --- a/Slim/ServiceManager.cs +++ b/Slim/ServiceManager.cs @@ -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() is null); + .OrderBy(c => + c.GetCustomAttribute() is PreferredConstructorAttribute preferredConstructorAttribute ? + preferredConstructorAttribute.Priority : + int.MaxValue); foreach (var constructor in constructors) { /* diff --git a/Slim/Slim.csproj b/Slim/Slim.csproj index dcd92bc..fca1ae2 100644 --- a/Slim/Slim.csproj +++ b/Slim/Slim.csproj @@ -8,10 +8,10 @@ Service lifetime management and dependency injection framework. - 1.7.1 - 1.7.1 + 1.7.2 + 1.7.2 https://github.com/AlexMacocian/Slim - 1.7.1 + 1.7.2 true true LICENSE diff --git a/Slim/Slim.xml b/Slim/Slim.xml index 3347085..7b8c0f0 100644 --- a/Slim/Slim.xml +++ b/Slim/Slim.xml @@ -4,14 +4,19 @@ Slim - + Attribute used to mark constructors to be used with priority by the . - + - Creates a new instance of . + Priority of the constructor. Constructors are ordered using this value when present. + + + + + Creates a new instance of .