mirror of
https://github.com/AlexMacocian/Slim.git
synced 2026-07-15 15:19:59 +00:00
Implement DoNotInject attribute (#11)
* Implement DoNotInject attribute * Upgrade pipelines
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
using Slim.Attributes;
|
||||
|
||||
namespace Slim.Tests.Models
|
||||
{
|
||||
public sealed class DoNotInjectConstructorService
|
||||
{
|
||||
[DoNotInject]
|
||||
public DoNotInjectConstructorService()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -779,5 +779,19 @@ namespace Slim.Tests
|
||||
|
||||
service.CalledPrefferedConstructor1.Should().BeTrue();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ServiceWithDoNotInjectAttribute_FailsToInject()
|
||||
{
|
||||
var di = new ServiceManager();
|
||||
di.RegisterSingleton<DoNotInjectConstructorService>();
|
||||
|
||||
var action = () =>
|
||||
{
|
||||
var service = di.GetService<ServiceWithPrefferedConstructor>();
|
||||
};
|
||||
|
||||
action.Should().Throw<DependencyInjectionException>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0-windows</TargetFramework>
|
||||
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
|
||||
namespace Slim.Attributes
|
||||
{
|
||||
/// <summary>
|
||||
/// Attribute used to mark constructors to not be used by the <see cref="ServiceManager"/>.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Constructor)]
|
||||
public sealed class DoNotInjectAttribute : Attribute
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -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<DoNotInjectAttribute>() is null)
|
||||
.OrderBy(c =>
|
||||
c.GetCustomAttribute<PreferredConstructorAttribute>() is PreferredConstructorAttribute preferredConstructorAttribute ?
|
||||
preferredConstructorAttribute.Priority :
|
||||
|
||||
+3
-3
@@ -8,10 +8,10 @@
|
||||
<Company />
|
||||
<Product></Product>
|
||||
<Description>Service lifetime management and dependency injection framework.</Description>
|
||||
<AssemblyVersion>1.7.2</AssemblyVersion>
|
||||
<FileVersion>1.7.2</FileVersion>
|
||||
<AssemblyVersion>1.7.3</AssemblyVersion>
|
||||
<FileVersion>1.7.3</FileVersion>
|
||||
<PackageProjectUrl>https://github.com/AlexMacocian/Slim</PackageProjectUrl>
|
||||
<Version>1.7.2</Version>
|
||||
<Version>1.7.3</Version>
|
||||
<EnableNETAnalyzers>true</EnableNETAnalyzers>
|
||||
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
|
||||
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||
|
||||
@@ -4,6 +4,11 @@
|
||||
<name>Slim</name>
|
||||
</assembly>
|
||||
<members>
|
||||
<member name="T:Slim.Attributes.DoNotInjectAttribute">
|
||||
<summary>
|
||||
Attribute used to mark constructors to not be used by the <see cref="T:Slim.ServiceManager"/>.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:Slim.Attributes.PreferredConstructorAttribute">
|
||||
<summary>
|
||||
Attribute used to mark constructors to be used with priority by the <see cref="T:Slim.ServiceManager"/>.
|
||||
|
||||
Reference in New Issue
Block a user