From dd1b589b5d41b3a79f45e505860a8e8d80cd5166 Mon Sep 17 00:00:00 2001 From: Macocian Alexandru Victor Date: Thu, 12 May 2022 18:13:28 +0200 Subject: [PATCH] Implement DoNotInject attribute (#11) * Implement DoNotInject attribute * Upgrade pipelines --- .github/workflows/cd.yaml | 2 +- .github/workflows/ci.yaml | 2 +- Slim.Tests/Models/DoNotInjectConstructorService.cs | 12 ++++++++++++ Slim.Tests/ServiceManagerTests.cs | 14 ++++++++++++++ Slim.Tests/Slim.Tests.csproj | 4 ++-- Slim.sln | 14 ++++++++++---- Slim/Attributes/DoNotInjectAttribute.cs | 12 ++++++++++++ Slim/ServiceManager.cs | 1 + Slim/Slim.csproj | 6 +++--- Slim/Slim.xml | 5 +++++ 10 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 Slim.Tests/Models/DoNotInjectConstructorService.cs create mode 100644 Slim/Attributes/DoNotInjectAttribute.cs diff --git a/.github/workflows/cd.yaml b/.github/workflows/cd.yaml index ab7f5a8..70df647 100644 --- a/.github/workflows/cd.yaml +++ b/.github/workflows/cd.yaml @@ -31,7 +31,7 @@ jobs: - name: Install .NET Core uses: actions/setup-dotnet@v1 with: - dotnet-version: '5.0.202' + dotnet-version: '6.x' - name: Setup MSBuild.exe uses: microsoft/setup-msbuild@v1.0.1 diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 0131d45..d1f8b7c 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -33,7 +33,7 @@ jobs: - name: Install .NET Core uses: actions/setup-dotnet@v1 with: - dotnet-version: '5.0.202' + dotnet-version: '6.x' - name: Setup MSBuild.exe uses: microsoft/setup-msbuild@v1.0.1 diff --git a/Slim.Tests/Models/DoNotInjectConstructorService.cs b/Slim.Tests/Models/DoNotInjectConstructorService.cs new file mode 100644 index 0000000..9637c9b --- /dev/null +++ b/Slim.Tests/Models/DoNotInjectConstructorService.cs @@ -0,0 +1,12 @@ +using Slim.Attributes; + +namespace Slim.Tests.Models +{ + public sealed class DoNotInjectConstructorService + { + [DoNotInject] + public DoNotInjectConstructorService() + { + } + } +} diff --git a/Slim.Tests/ServiceManagerTests.cs b/Slim.Tests/ServiceManagerTests.cs index 3011ae3..caab560 100644 --- a/Slim.Tests/ServiceManagerTests.cs +++ b/Slim.Tests/ServiceManagerTests.cs @@ -779,5 +779,19 @@ namespace Slim.Tests service.CalledPrefferedConstructor1.Should().BeTrue(); } + + [TestMethod] + public void ServiceWithDoNotInjectAttribute_FailsToInject() + { + var di = new ServiceManager(); + di.RegisterSingleton(); + + var action = () => + { + var service = di.GetService(); + }; + + action.Should().Throw(); + } } } diff --git a/Slim.Tests/Slim.Tests.csproj b/Slim.Tests/Slim.Tests.csproj index 8ddfd99..27c4463 100644 --- a/Slim.Tests/Slim.Tests.csproj +++ b/Slim.Tests/Slim.Tests.csproj @@ -1,8 +1,8 @@ - net5.0-windows - + net6.0 + latest false diff --git a/Slim.sln b/Slim.sln index 22d9ac5..fdcb3b6 100644 --- a/Slim.sln +++ b/Slim.sln @@ -1,15 +1,21 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.30104.148 +# Visual Studio Version 17 +VisualStudioVersion = 17.1.32319.34 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Slim", "Slim\Slim.csproj", "{04F435A2-BEF9-4F49-9D1A-EC1AEFCC3088}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Slim", "Slim\Slim.csproj", "{04F435A2-BEF9-4F49-9D1A-EC1AEFCC3088}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Slim.Tests", "Slim.Tests\Slim.Tests.csproj", "{F03F7353-0BD5-4691-A318-BC6FC93AB00E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Slim.Tests", "Slim.Tests\Slim.Tests.csproj", "{F03F7353-0BD5-4691-A318-BC6FC93AB00E}" ProjectSection(ProjectDependencies) = postProject {04F435A2-BEF9-4F49-9D1A-EC1AEFCC3088} = {04F435A2-BEF9-4F49-9D1A-EC1AEFCC3088} EndProjectSection EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Pipelines", "Pipelines", "{04D9F456-6AE5-4880-B4BB-89FF3B084585}" + ProjectSection(SolutionItems) = preProject + .github\workflows\cd.yaml = .github\workflows\cd.yaml + .github\workflows\ci.yaml = .github\workflows\ci.yaml + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/Slim/Attributes/DoNotInjectAttribute.cs b/Slim/Attributes/DoNotInjectAttribute.cs new file mode 100644 index 0000000..900f96c --- /dev/null +++ b/Slim/Attributes/DoNotInjectAttribute.cs @@ -0,0 +1,12 @@ +using System; + +namespace Slim.Attributes +{ + /// + /// Attribute used to mark constructors to not be used by the . + /// + [AttributeUsage(AttributeTargets.Constructor)] + public sealed class DoNotInjectAttribute : Attribute + { + } +} diff --git a/Slim/ServiceManager.cs b/Slim/ServiceManager.cs index cb17524..3076581 100644 --- a/Slim/ServiceManager.cs +++ b/Slim/ServiceManager.cs @@ -687,6 +687,7 @@ namespace Slim * Order constructors to give priority to constructors that have PrefferedConstructorAttribute decorator */ var constructors = implementType.GetConstructors() + .Where(c => c.GetCustomAttribute() is null) .OrderBy(c => c.GetCustomAttribute() is PreferredConstructorAttribute preferredConstructorAttribute ? preferredConstructorAttribute.Priority : diff --git a/Slim/Slim.csproj b/Slim/Slim.csproj index fca1ae2..177d184 100644 --- a/Slim/Slim.csproj +++ b/Slim/Slim.csproj @@ -8,10 +8,10 @@ Service lifetime management and dependency injection framework. - 1.7.2 - 1.7.2 + 1.7.3 + 1.7.3 https://github.com/AlexMacocian/Slim - 1.7.2 + 1.7.3 true true LICENSE diff --git a/Slim/Slim.xml b/Slim/Slim.xml index 7b8c0f0..60cf1cb 100644 --- a/Slim/Slim.xml +++ b/Slim/Slim.xml @@ -4,6 +4,11 @@ Slim + + + Attribute used to mark constructors to not be used by the . + + Attribute used to mark constructors to be used with priority by the .