diff --git a/Directory.Build.props b/Directory.Build.props index adc3097..8e2e335 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,7 +1,7 @@ - 1.0.2 + 1.0.3 AIDotNet Token 自动依赖注入,利用源生成器实现自动依赖注入,并且不使用反射进行实现功能。 diff --git a/Gnarly.sln b/Gnarly.sln index 68415df..3917253 100644 --- a/Gnarly.sln +++ b/Gnarly.sln @@ -16,7 +16,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Directory.Build.props = Directory.Build.props EndProjectSection EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "src\Test\Test.csproj", "{B241F5DD-1A66-4E36-9B98-C7D4A5D5A1A8}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gnarly.Application", "src\Gnarly.Application\Gnarly.Application.csproj", "{B241F5DD-1A66-4E36-9B98-C7D4A5D5A1A8}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/src/Test/Test.csproj b/src/Gnarly.Application/Gnarly.Application.csproj similarity index 87% rename from src/Test/Test.csproj rename to src/Gnarly.Application/Gnarly.Application.csproj index d702a40..59fa0d3 100644 --- a/src/Test/Test.csproj +++ b/src/Gnarly.Application/Gnarly.Application.csproj @@ -3,6 +3,7 @@ netstandard2.0 false + Test diff --git a/src/Test/ITestService.cs b/src/Gnarly.Application/ITestService.cs similarity index 100% rename from src/Test/ITestService.cs rename to src/Gnarly.Application/ITestService.cs diff --git a/src/Test/TestService.cs b/src/Gnarly.Application/TestService.cs similarity index 96% rename from src/Test/TestService.cs rename to src/Gnarly.Application/TestService.cs index 9937708..c3144d8 100644 --- a/src/Test/TestService.cs +++ b/src/Gnarly.Application/TestService.cs @@ -9,6 +9,7 @@ public class TestService : ITestService, ISingletonDependency public async Task SendMessageAsync() { Console.WriteLine("Hello, Gnarly!"); + await Task.CompletedTask; } } diff --git a/src/Gnarly.Data/Gnarly.Data.csproj b/src/Gnarly.Data/Gnarly.Data.csproj index e7155e0..fe2d221 100644 --- a/src/Gnarly.Data/Gnarly.Data.csproj +++ b/src/Gnarly.Data/Gnarly.Data.csproj @@ -3,6 +3,7 @@ netstandard2.0 true + latest diff --git a/src/Gnarly.Data/ITransientDependency.cs b/src/Gnarly.Data/ITransientDependency.cs index 0d0f6de..68cb0cc 100644 --- a/src/Gnarly.Data/ITransientDependency.cs +++ b/src/Gnarly.Data/ITransientDependency.cs @@ -5,6 +5,5 @@ /// public interface ITransientDependency { - } } \ No newline at end of file diff --git a/src/Gnarly.WebApi/Gnarly.WebApi.csproj b/src/Gnarly.WebApi/Gnarly.WebApi.csproj index bd4d581..ee41f93 100644 --- a/src/Gnarly.WebApi/Gnarly.WebApi.csproj +++ b/src/Gnarly.WebApi/Gnarly.WebApi.csproj @@ -14,7 +14,7 @@ - + diff --git a/src/Gnarly/ScanService.cs b/src/Gnarly/ScanService.cs index 5032508..258a3e5 100644 --- a/src/Gnarly/ScanService.cs +++ b/src/Gnarly/ScanService.cs @@ -1,8 +1,6 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp.Syntax; namespace Gnarly; @@ -15,45 +13,8 @@ public class ScanService private static INamedTypeSymbol iSingletonDependencySymbol; private static INamedTypeSymbol iTransientDependencySymbol; - /// - /// 扫描程序集中的所有类,找到实现了接口的类ConditionalExpressionSyntax conditionalExpression; - /// - /// - /// - public static IEnumerable<(int type, INamedTypeSymbol namedTypeSymbol)> ScanForDependencyTypes(Compilation compilation) - { - iScopeDependencySymbol ??= compilation.GetTypeByMetadataName("Gnarly.Data.IScopeDependency"); - iSingletonDependencySymbol ??= compilation.GetTypeByMetadataName("Gnarly.Data.ISingletonDependency"); - iTransientDependencySymbol ??= compilation.GetTypeByMetadataName("Gnarly.Data.ITransientDependency"); - - foreach (var syntaxTree in compilation.SyntaxTrees) - { - var semanticModel = compilation.GetSemanticModel(syntaxTree); - var root = syntaxTree.GetRoot(); - - var classes = root.DescendantNodes().OfType(); - - foreach (var @class in classes) - { - if (semanticModel.GetDeclaredSymbol(@class) is INamedTypeSymbol symbol) - { - if (symbol.AllInterfaces.Contains(iScopeDependencySymbol)) - { - yield return (1, symbol); - } - else if (symbol.AllInterfaces.Contains(iSingletonDependencySymbol)) - { - yield return (2, symbol); - } - else if (symbol.AllInterfaces.Contains(iTransientDependencySymbol)) - { - yield return (3, symbol); - } - } - } - } - } - public static void ScanAndCollect(Compilation compilationContext, List scopeMethods, List singletonMethods, List transientMethods) + public static void ScanAndCollect(Compilation compilationContext, List scopeMethods, + List singletonMethods, List transientMethods) { var visitedAssemblies = new HashSet(SymbolEqualityComparer.Default); @@ -63,7 +24,8 @@ public static void ScanAndCollect(Compilation compilationContext, List s void ScanAssembly(IAssemblySymbol assemblySymbol) { - if (visitedAssemblies.Contains(assemblySymbol) || assemblySymbol.Name.StartsWith("Microsoft") || assemblySymbol.Name.StartsWith("System")) + if (visitedAssemblies.Contains(assemblySymbol) || assemblySymbol.Name.StartsWith("Microsoft") || + assemblySymbol.Name.StartsWith("System")) { return; } @@ -97,7 +59,7 @@ void ScanType(INamedTypeSymbol namedTypeSymbol) if (registrationAttribute != null && registrationAttribute.ConstructorArguments.FirstOrDefault().Value as INamedTypeSymbol is - { } registrationType && registrationType.TypeKind == TypeKind.Interface) + { } registrationType && registrationType.TypeKind == TypeKind.Interface) { switch (type) { diff --git a/src/Gnarly/ServiceRegistrationGenerator.cs b/src/Gnarly/ServiceRegistrationGenerator.cs index da6774f..c4563eb 100644 --- a/src/Gnarly/ServiceRegistrationGenerator.cs +++ b/src/Gnarly/ServiceRegistrationGenerator.cs @@ -32,7 +32,7 @@ private void Execute(Compilation compilation, SourceProductionContext context) var transientRegistrations = string.Join("\n", transientMethods.Select(m => $"services.AddTransient<{m}>();")); // 生成源代码 - string source = $@"// 这是由SourceGenerator自动生成的代码 + string source = $@"// Gnarly自动生成的源代码,请勿手动修改 using System; using Microsoft.Extensions.DependencyInjection; @@ -45,9 +45,9 @@ public static class GnarlyExtensions /// public static IServiceCollection AddAutoGnarly(this IServiceCollection services) {{ - {scopeRegistrations} - {singletonRegistrations} - {transientRegistrations} + {scopeRegistrations.Trim()} + {singletonRegistrations.Trim()} + {transientRegistrations.Trim()} return services; }} }}