From e7f78786e3ea41eb75343aa4c55c7241eeeda798 Mon Sep 17 00:00:00 2001 From: Wwb <22379099@qq.com> Date: Tue, 12 Oct 2021 04:04:43 +0800 Subject: [PATCH] init --- .github/workflows/dotnet.yml | 33 +++++++++++++ .gitignore | 8 +++ WWB.DI.sln | 42 ++++++++++++++++ .../WWB.DI.Tests.Service.csproj | 11 +++++ src/WWB.DI.Tests/TestService.cs | 23 +++++++++ src/WWB.DI.Tests/UnitTest1.cs | 22 +++++++++ src/WWB.DI.Tests/WWB.DI.Tests.csproj | 27 ++++++++++ src/WWB.DI/Dependencies/IScopedOnlySelf.cs | 7 +++ .../Dependencies/IScopedWithInterfaces.cs | 7 +++ src/WWB.DI/Dependencies/ISingletonOnlySelf.cs | 7 +++ .../Dependencies/ISingletonWithInterfaces.cs | 7 +++ src/WWB.DI/Dependencies/ITransientOnlySelf.cs | 7 +++ .../Dependencies/ITransientWithInterfaces.cs | 7 +++ src/WWB.DI/Extensions.cs | 49 +++++++++++++++++++ src/WWB.DI/WWB.DI.csproj | 18 +++++++ 15 files changed, 275 insertions(+) create mode 100644 .github/workflows/dotnet.yml create mode 100644 .gitignore create mode 100644 WWB.DI.sln create mode 100644 src/WWB.DI.Tests.Service/WWB.DI.Tests.Service.csproj create mode 100644 src/WWB.DI.Tests/TestService.cs create mode 100644 src/WWB.DI.Tests/UnitTest1.cs create mode 100644 src/WWB.DI.Tests/WWB.DI.Tests.csproj create mode 100644 src/WWB.DI/Dependencies/IScopedOnlySelf.cs create mode 100644 src/WWB.DI/Dependencies/IScopedWithInterfaces.cs create mode 100644 src/WWB.DI/Dependencies/ISingletonOnlySelf.cs create mode 100644 src/WWB.DI/Dependencies/ISingletonWithInterfaces.cs create mode 100644 src/WWB.DI/Dependencies/ITransientOnlySelf.cs create mode 100644 src/WWB.DI/Dependencies/ITransientWithInterfaces.cs create mode 100644 src/WWB.DI/Extensions.cs create mode 100644 src/WWB.DI/WWB.DI.csproj diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml new file mode 100644 index 0000000..35e6d09 --- /dev/null +++ b/.github/workflows/dotnet.yml @@ -0,0 +1,33 @@ +name: .NET + +on: + push: + tags: + - 'v*' + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: 建立.netcore环境 + uses: actions/setup-dotnet@v1 + with: + dotnet-version: 3.1.403 + - name: 安装依赖 + run: dotnet restore + - name: 构建 + run: dotnet build --configuration Release --no-restore + - name: 添加github仓库 + run: | + nuget sources add -name github -Source https://nuget.pkg.github.com/my6521/index.json -Username my6521 -Password ${{ secrets.GITHUBTOKEN }} + - name: 安装nuget + uses: nuget/setup-nuget@v1 + with: + nuget-version: '5.x' + - name: 发布生成包到github和nuget + run: | + nuget push ./src/WWB.DI/bin/Release/*.nupkg -Source https://api.nuget.org/v3/index.json -SkipDuplicate -ApiKey ${{ secrets.NUGETKEY }} -NoSymbols + nuget push ./src/WWB.DI/bin/Release/*.nupkg -Source github -SkipDuplicate diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3b02f1d --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +################################################################################ +# 此 .gitignore 文件已由 Microsoft(R) Visual Studio 自动创建。 +################################################################################ + +.vs +obj/ +bin/ + diff --git a/WWB.DI.sln b/WWB.DI.sln new file mode 100644 index 0000000..10823f7 --- /dev/null +++ b/WWB.DI.sln @@ -0,0 +1,42 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31717.71 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{33685618-180F-4462-851A-A5B144750EFF}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WWB.DI", "src\WWB.DI\WWB.DI.csproj", "{D49F2395-6A85-4CA9-889F-404B7064ED96}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WWB.DI.Tests", "src\WWB.DI.Tests\WWB.DI.Tests.csproj", "{72144FE4-8DA0-49F4-9D08-C101384FC900}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{90C13733-C7E7-491F-805C-1ADE277FC945}" + ProjectSection(SolutionItems) = preProject + .github\workflows\dotnet.yml = .github\workflows\dotnet.yml + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D49F2395-6A85-4CA9-889F-404B7064ED96}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D49F2395-6A85-4CA9-889F-404B7064ED96}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D49F2395-6A85-4CA9-889F-404B7064ED96}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D49F2395-6A85-4CA9-889F-404B7064ED96}.Release|Any CPU.Build.0 = Release|Any CPU + {72144FE4-8DA0-49F4-9D08-C101384FC900}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {72144FE4-8DA0-49F4-9D08-C101384FC900}.Debug|Any CPU.Build.0 = Debug|Any CPU + {72144FE4-8DA0-49F4-9D08-C101384FC900}.Release|Any CPU.ActiveCfg = Release|Any CPU + {72144FE4-8DA0-49F4-9D08-C101384FC900}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {D49F2395-6A85-4CA9-889F-404B7064ED96} = {33685618-180F-4462-851A-A5B144750EFF} + {72144FE4-8DA0-49F4-9D08-C101384FC900} = {33685618-180F-4462-851A-A5B144750EFF} + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {4E432B43-34A8-421A-BF96-543995000EAE} + EndGlobalSection +EndGlobal diff --git a/src/WWB.DI.Tests.Service/WWB.DI.Tests.Service.csproj b/src/WWB.DI.Tests.Service/WWB.DI.Tests.Service.csproj new file mode 100644 index 0000000..d632184 --- /dev/null +++ b/src/WWB.DI.Tests.Service/WWB.DI.Tests.Service.csproj @@ -0,0 +1,11 @@ + + + + netcoreapp3.1 + + + + + + + diff --git a/src/WWB.DI.Tests/TestService.cs b/src/WWB.DI.Tests/TestService.cs new file mode 100644 index 0000000..f7ccc2c --- /dev/null +++ b/src/WWB.DI.Tests/TestService.cs @@ -0,0 +1,23 @@ +using System; +using WWB.DI.Dependencies; + +namespace WWB.DI.Tests.Service +{ + public interface IServiceAA + { + void Say(); + } + + public interface IServiceA : IServiceAA + { + void Say(); + } + + public class ServiceA : IServiceA, IScopedWithInterfaces + { + public void Say() + { + Console.WriteLine("Hello"); + } + } +} diff --git a/src/WWB.DI.Tests/UnitTest1.cs b/src/WWB.DI.Tests/UnitTest1.cs new file mode 100644 index 0000000..da5113b --- /dev/null +++ b/src/WWB.DI.Tests/UnitTest1.cs @@ -0,0 +1,22 @@ +using Microsoft.Extensions.DependencyInjection; +using WWB.DI.Tests.Service; +using Xunit; + +namespace WWB.DI.Tests +{ + public class UnitTest1 + { + [Fact] + public void Test1() + { + var collection = new ServiceCollection(); + collection.AddServicesFromAllAssembly(); + + var serviceProvider = collection.BuildServiceProvider(); + var serviceA = serviceProvider.GetService(); + var serviceAA = serviceProvider.GetService(); + serviceA.Say(); + serviceAA.Say(); + } + } +} diff --git a/src/WWB.DI.Tests/WWB.DI.Tests.csproj b/src/WWB.DI.Tests/WWB.DI.Tests.csproj new file mode 100644 index 0000000..1d3a269 --- /dev/null +++ b/src/WWB.DI.Tests/WWB.DI.Tests.csproj @@ -0,0 +1,27 @@ + + + + netcoreapp3.1 + + false + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/src/WWB.DI/Dependencies/IScopedOnlySelf.cs b/src/WWB.DI/Dependencies/IScopedOnlySelf.cs new file mode 100644 index 0000000..fa9eb6a --- /dev/null +++ b/src/WWB.DI/Dependencies/IScopedOnlySelf.cs @@ -0,0 +1,7 @@ +namespace WWB.DI.Dependencies +{ + public interface IScopedOnlySelf + { + + } +} diff --git a/src/WWB.DI/Dependencies/IScopedWithInterfaces.cs b/src/WWB.DI/Dependencies/IScopedWithInterfaces.cs new file mode 100644 index 0000000..e3cd70b --- /dev/null +++ b/src/WWB.DI/Dependencies/IScopedWithInterfaces.cs @@ -0,0 +1,7 @@ +namespace WWB.DI.Dependencies +{ + public interface IScopedWithInterfaces + { + + } +} diff --git a/src/WWB.DI/Dependencies/ISingletonOnlySelf.cs b/src/WWB.DI/Dependencies/ISingletonOnlySelf.cs new file mode 100644 index 0000000..1eda3dd --- /dev/null +++ b/src/WWB.DI/Dependencies/ISingletonOnlySelf.cs @@ -0,0 +1,7 @@ +namespace WWB.DI.Dependencies +{ + public interface ISingletonOnlySelf + { + + } +} diff --git a/src/WWB.DI/Dependencies/ISingletonWithInterfaces.cs b/src/WWB.DI/Dependencies/ISingletonWithInterfaces.cs new file mode 100644 index 0000000..311463a --- /dev/null +++ b/src/WWB.DI/Dependencies/ISingletonWithInterfaces.cs @@ -0,0 +1,7 @@ +namespace WWB.DI.Dependencies +{ + public interface ISingletonWithInterfaces + { + + } +} diff --git a/src/WWB.DI/Dependencies/ITransientOnlySelf.cs b/src/WWB.DI/Dependencies/ITransientOnlySelf.cs new file mode 100644 index 0000000..cb93f00 --- /dev/null +++ b/src/WWB.DI/Dependencies/ITransientOnlySelf.cs @@ -0,0 +1,7 @@ +namespace WWB.DI.Dependencies +{ + public interface ITransientOnlySelf + { + + } +} diff --git a/src/WWB.DI/Dependencies/ITransientWithInterfaces.cs b/src/WWB.DI/Dependencies/ITransientWithInterfaces.cs new file mode 100644 index 0000000..cc3898d --- /dev/null +++ b/src/WWB.DI/Dependencies/ITransientWithInterfaces.cs @@ -0,0 +1,7 @@ +namespace WWB.DI.Dependencies +{ + public interface ITransientWithInterfaces + { + + } +} diff --git a/src/WWB.DI/Extensions.cs b/src/WWB.DI/Extensions.cs new file mode 100644 index 0000000..116f454 --- /dev/null +++ b/src/WWB.DI/Extensions.cs @@ -0,0 +1,49 @@ +using Microsoft.Extensions.DependencyInjection; +using System.Linq; +using WWB.DI.Dependencies; + +namespace WWB.DI +{ + public static class Extensions + { + public static IServiceCollection AddServicesFromAllAssembly(this IServiceCollection services) + { + string[] filters = + { + "mscorlib", + "netstandard", + "dotnet", + "api-ms-win-core", + "runtime.", + "System", + "Microsoft", + "Window", + }; + + return services.Scan(scan => + { + scan + .FromApplicationDependencies(assembly => !filters.Any(x => assembly.FullName.StartsWith(x))) + .AddClasses(classes => classes.AssignableTo()) + .AsImplementedInterfaces() + .WithScopedLifetime() + .AddClasses(classes => classes.AssignableTo()) + .AsSelfWithInterfaces() + .WithTransientLifetime() + .AddClasses(classes => classes.AssignableTo()) + .AsSelfWithInterfaces() + .WithSingletonLifetime() + .AddClasses(classes => classes.AssignableTo()) + .AsSelf() + .WithScopedLifetime() + .AddClasses(classes => classes.AssignableTo()) + .AsSelf() + .WithTransientLifetime() + .AddClasses(classes => classes.AssignableTo()) + .AsSelf() + .WithSingletonLifetime(); + }); + } + + } +} diff --git a/src/WWB.DI/WWB.DI.csproj b/src/WWB.DI/WWB.DI.csproj new file mode 100644 index 0000000..11bc2cf --- /dev/null +++ b/src/WWB.DI/WWB.DI.csproj @@ -0,0 +1,18 @@ + + + + netcoreapp3.1 + WWB.DI + 1.0.0 + my6521 + NetCore3.1 批量依赖注入。 + MIT + https://github.com/my6521/WWB.DI + True + + + + + + +