目录

  • 摘要
  • 元数据分析
  • 使用 Source generators 实现
  • 使用 Source generators 实现程序集分析
  • 使用方法
  • SourceCode && Nuget package
  • 总结

摘要

Source generators 随着 .net5 推出,并在 .net6 中大量运用,它可以基于编译时分析,根据现有代码创建新的代码并添加进编译时。利用 SourceGenerator 可以将开发人员从一些模板化的重复的工作中解放出来,更多的投入创造力的工作,并且和原生代码一致的性能。 在这篇文章中,我们将演示如何使用 Source generators 根据 HTTP API 接口自动生成实现类,以及实现跨项目分析,并且添加进 DI 容器。

元数据分析

Source generators 可以根据编译时语法树(Syntax)或符号(Symbol)分析,来执行创建新代码,因此我们需要在编译前提供足够多的元数据,在本文中我们需要知道哪些接口需要生成实现类,并且接口中定义的方法该以 Get,Post 等哪种方法发送出去,在本文中我们通过注解(Attribute/Annotation)来提供这些元数据,当然您也可以通过接口约束,命名惯例来提供。

首先我们定义接口上的注解,这将决定我们需要扫描的接口以及如何创建 HttpClient:

/// <summary>
/// Identity a Interface which will be implemented by SourceGenerator
/// </summary>
[AttributeUsage(AttributeTargets.Interface)]
public class HttpClientAttribute : Attribute
{
/// <summary>
/// HttpClient name
/// </summary>
public string Name { get; } /// <summary>
/// Create a new <see cref="HttpClientAttribute"/>
/// </summary>
public HttpClientAttribute()
{
} /// <summary>
/// Create a new <see cref="HttpClientAttribute"/> with given name
/// </summary>
/// <param name="name"></param>
public HttpClientAttribute(string name)
{
Name = name;
}
}

然后我们定义接口方法上的注解,表明以何种方式请求 API 以及请求的模板路径,这里以HttpGet方法为例:

/// <summary>
/// Identity a method send HTTP Get request
/// </summary>
public class HttpGetAttribute : HttpMethodAttribute
{
/// <summary>
/// Creates a new <see cref="HttpGetAttribute"/> with the given route template.
/// </summary>
/// <param name="template">route template</param>
public HttpGetAttribute(string template) : base(template)
{
}
} /// <summary>
/// HTTP method abstract type for common encapsulation
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public abstract class HttpMethodAttribute : Attribute
{
/// <summary>
/// Route template
/// </summary>
private string Template { get; } /// <summary>
/// Creates a new <see cref="HttpMethodAttribute"/> with the given route template.
/// </summary>
/// <param name="template">route template</param>
protected HttpMethodAttribute(string template)
{
Template = template;
}
}

当然还提供RequiredServiceAttribute来注入服务,HeaderAttribute来添加头信息等注解这里不做展开,得益于 C# 的字符串插值(String interpolation)语法糖,要支持路由变量等功能,只需要用{}包裹变量就行 例如[HttpGet("/todos/{id}")],这样在运行时就会自动替换成对应的值。

使用 Source generators 实现

新建 HttpClient.SourceGenerator 项目,SourceGenerator 需要引入 Microsoft.CodeAnalysis.Analyzers, Microsoft.CodeAnalysis.CSharp 包,并将 TargetFramework 设置成 netstandard2.0。

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<IncludeBuildOutput>false</IncludeBuildOutput>
...
</PropertyGroup> <ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.0.1" PrivateAssets="all" />
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
...
</ItemGroup> </Project>

要使用 SourceGenerator 需要实现 ISourceGenerator 接口,并添加 [Generator] 注解,一般情况下我们在 Initialize 注册 Syntax receiver,将需要的类添加到接收器中,在 Execute 丢弃掉不是该接收器的上下文,执行具体的代码生成逻辑。

public interface ISourceGenerator
{
void Initialize(GeneratorInitializationContext context); void Execute(GeneratorExecutionContext context);
}

这里我们需要了解下 roslyn api 中的 语法树模型 (SyntaxTree model) 和 语义模型 (Semantic model),简单的讲, 语法树表示源代码的语法和词法结构,表明节点是接口声明还是类声明还是 using 指令块等等,这一部分信息来源于编译器的Parse阶段;语义来源于编译器的Declaration阶段,由一系列 Named symbol 构成,比如TypeSymbolMethodSymbol等,类似于 CLR 类型系统, TypeSymbol 可以得到标记的注解信息,MethodSymbol 可以得到 ReturnType 等信息。

定义 HttpClient Syntax Receiver,这里我们处理节点信息是接口声明语法的节点,并且接口声明语法上有注解,然后再获取其语义模型,根据语义模型判断是否包含我们上边定义的 HttpClientAttribute

class HttpClientSyntax : ISyntaxContextReceiver
{
public List<INamedTypeSymbol> TypeSymbols { get; set; } = new List<INamedTypeSymbol>(); public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
{
if (context.Node is InterfaceDeclarationSyntax ids && ids.AttributeLists.Count > 0)
{
var typeSymbol = ModelExtensions.GetDeclaredSymbol(context.SemanticModel, ids) as INamedTypeSymbol;
if (typeSymbol!.GetAttributes().Any(x =>
x.AttributeClass!.ToDisplayString() ==
"SourceGeneratorPower.HttpClient.HttpClientAttribute"))
{
TypeSymbols.Add(typeSymbol);
}
}
}
}

接下来就是循环处理接收器中的 TypeSymbol,根据接口里面定义的方法以及注解自动生成实现体的方法,这里不做展开详细代码可以查看 Github。

private string GenerateGetMethod(ITypeSymbol typeSymbol, IMethodSymbol methodSymbol, string httpClientName,
string requestUri)
{
var returnType = (methodSymbol.ReturnType as INamedTypeSymbol).TypeArguments[0].ToDisplayString();
var cancellationToken = methodSymbol.Parameters.Last().Name;
var source = GenerateHttpClient(typeSymbol, methodSymbol, httpClientName);
source.AppendLine($@"var response = await httpClient.GetAsync($""{requestUri}"", {cancellationToken});");
source.AppendLine("response!.EnsureSuccessStatusCode();");
source.AppendLine(
$@"return (await response.Content.ReadFromJsonAsync<{returnType}>(cancellationToken: {cancellationToken})!)!;");
source.AppendLine("}");
return source.ToString();
}

我们这里生成一个扩展方法,并将 HTTP API 接口和实现类添加到 DI容器,然后在主项目中调用这个扩展方法,同时为了避免可能的命名空间冲突,我们这里使用 global:: 加上包含命名空间的全名来引用。

   var extensionSource = new StringBuilder($@"
using SourceGeneratorPower.HttpClient;
using Microsoft.Extensions.Configuration; namespace Microsoft.Extensions.DependencyInjection
{{
public static class ScanInjectOptions
{{
public static void AddGeneratedHttpClient(this IServiceCollection services)
{{
"); foreach (var typeSymbol in receiver.TypeSymbols)
{
...
extensionSource.AppendLine(
$@"services.AddScoped<global::{typeSymbol.ToDisplayString()}, global::{typeSymbol.ContainingNamespace.ToDisplayString()}.{typeSymbol.Name.Substring(1)}>();");
} extensionSource.AppendLine("}}}");
var extensionTextFormatted = CSharpSyntaxTree
.ParseText(extensionSource.ToString(), new CSharpParseOptions(LanguageVersion.CSharp8)).GetRoot()
.NormalizeWhitespace().SyntaxTree.GetText().ToString();
context.AddSource($"SourceGeneratorPower.HttpClientExtension.AutoGenerated.cs",
SourceText.From(extensionTextFormatted, Encoding.UTF8));
...

使用 Source generators 实现程序集分析

在上面我们介绍了如何根据语法树来分析哪些接口需要生成这只适合单项目,但在实际工作中常常是分项目开发的,项目之间通过 ProjectReference 引用。

在 Source generators 中我们可以使用 context.Compilation.SourceModule.ReferencedAssemblySymbols 来分析程序集中的代码,这其中包含了框架的引用程序集,项目引用的程序集以及 nuget 包引用的程序集,我们可以通过 PublicKey 为空条件只保留项目引用的程序集。

在程序集符号(IAssemblySymbol)中, 符号的关系如下图,我们需要的是找到最终的 INameTypeSymbol 判断是否是需要我们进行生成的接口。

这里我们可以自定义 Symbol visitor 来实现遍历扫描需要生成的接口。

class HttpClientVisitor : SymbolVisitor
{
private readonly HashSet<INamedTypeSymbol> _httpClientTypeSymbols; public HttpClientVisitor()
{
_httpClientTypeSymbols = new HashSet<INamedTypeSymbol>(SymbolEqualityComparer.Default);
} public ImmutableArray<INamedTypeSymbol> GetHttpClientTypes() => _httpClientTypeSymbols.ToImmutableArray(); public override void VisitAssembly(IAssemblySymbol symbol)
{
symbol.GlobalNamespace.Accept(this);
} public override void VisitNamespace(INamespaceSymbol symbol)
{
foreach (var namespaceOrTypeSymbol in symbol.GetMembers())
{
namespaceOrTypeSymbol.Accept(this);
}
} public override void VisitNamedType(INamedTypeSymbol symbol)
{
if (symbol.DeclaredAccessibility != Accessibility.Public)
{
return;
} if (symbol.GetAttributes().Any(x =>
x.AttributeClass!.ToDisplayString() == "SourceGeneratorPower.HttpClient.HttpClientAttribute"))
{
_httpClientTypeSymbols.Add(symbol);
} var nestedTypes = symbol.GetMembers();
if (nestedTypes.IsDefaultOrEmpty)
{
return;
} foreach (var nestedType in nestedTypes)
{
nestedType.Accept(this);
}
}
}

然后将这部分与上边的 HttpClientSymbolReceiver的 INameTypeSymbol 合并到一起,生成代码的逻辑不变。

public void Execute(GeneratorExecutionContext context)
{
if (!(context.SyntaxContextReceiver is HttpClientSyntax receiver))
{
return;
} var httpClientVisitor = new HttpClientVisitor();
foreach (var assemblySymbol in context.Compilation.SourceModule.ReferencedAssemblySymbols
.Where(x => x.Identity.PublicKey == ImmutableArray<byte>.Empty))
{
assemblySymbol.Accept(httpClientVisitor);
}
receiver.TypeSymbols.AddRange(httpClientVisitor.GetHttpClientTypes());
...
}

使用方法

接口定义

[HttpClient("JsonServer")]
public interface IJsonServerApi
{
[HttpGet("/todos/{id}")]
Task<Todo> Get(int id, CancellationToken cancellationToken = default); [HttpPost(("/todos"))]
Task<Todo> Post(CreateTodo createTodo, CancellationToken cancellationToken = default); [HttpPut("/todos/{todo.Id}")]
Task<Todo> Put(Todo todo, CancellationToken cancellationToken); [HttpPatch("/todos/{id}")]
Task<Todo> Patch(int id, Todo todo, CancellationToken cancellationToken); [HttpDelete("/todos/{id}")]
Task<object> Delete(int id, CancellationToken cancellationToken);
}

主项目引用,并配置对应的 HttpClient

builder.Services.AddGeneratedHttpClient();
builder.Services.AddHttpClient("JsonServer", options => options.BaseAddress = new Uri("https://jsonplaceholder.typicode.com"));

注入接口并使用

public class TodoController: ControllerBase
{
private readonly IJsonServerApi _jsonServerApi; public TodoController(IJsonServerApi jsonServerApi)
{
_jsonServerApi = jsonServerApi;
} [HttpGet("{id}")]
public async Task<Todo> Get(int id, CancellationToken cancellationToken)
{
return await _jsonServerApi.Get(id, cancellationToken);
}
...
}

SourceCode && Nuget package

SourceCode: https://github.com/huiyuanai709/SourceGeneratorPower

Nuget Package: https://www.nuget.org/packages/SourceGeneratorPower.HttpClient.Abstractions

Nuget Package: https://www.nuget.org/packages/SourceGeneratorPower.HttpClient.SourceGenerator

总结

Source generators 非常强(Powerful!!!),以一种现代化的,人类可读(human readable)的方式解决重复编码的问题,并且拥有与原生代码一致的性能,读者可以结合文章以及官方示例用 Source generators 来解决实际工作中的问题,任何建议和新功能需求也欢迎留言或在 Github 上提出。

.NET SourceGenerators 根据 HTTPAPI 接口自动生成实现类的更多相关文章

  1. Mybatis自动生成实体类、dao接口和mapping映射文件

    由于Mybatis是一种半自动的ORM框架,它的工作主要是配置mapping映射文件,为了减少手动书写映射文件,可以利用mybatis生成器,自动生成实体类.dao接口以及它的映射文件,然后直接拷贝到 ...

  2. Mybatis自动生成实体类

    Maven自动生成实体类需要的jar包 一.pom.xml中 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns ...

  3. Springboot mybatis generate 自动生成实体类和Mapper

    https://github.com/JasmineQian/SpringDemo_2019/tree/master/mybatis Springboot让java开发变得方便,Springboot中 ...

  4. 使用T4为数据库自动生成实体类

    T4 (Text Template Transformation Toolkit) 是一个基于模板的代码生成器.使用T4你可以通过写一些ASP.NET-like模板,来生成C#, T-SQL, XML ...

  5. ANTLR和StringTemplate实例:自动生成单元测试类

    ANTLR和StringTemplate实例:自动生成单元测试类 1. ANTLR语法 要想自动生成单元测试,首先第一步就是分析被测试类.这里以Java代码为例,用ANTLR对Java代码进行分析.要 ...

  6. 【原创】有关Silverlight中自动生成的类中 没有WCF层edmx模型新加入的对象 原因分析。

      前端页面层:    编译老是不通过,报如下如所示错误:     -- 然后下意识的查了下 生成的cs文件,没有搜到根据edmx 生成的 对应的类.       结果整理: 1.尽管在 edmx 模 ...

  7. mybatis怎样自动生成java类,配置文件?

    其实没有什么东西是可以自动生成的,只不过是别人已经写好了,你调用罢了. 所以想要mybatis自动生成java类,配置文件等,就必须要一些配置和一些jar包.当然这些配置也很简单. 为了有个初步的认识 ...

  8. .net core 中简单封装Dapper.Extensions 并使用sqlsuger自动生成实体类

    引言 由公司需要使用dapper  同时支持多数据库 又需要支持实体类 又需要支持sql 还需要支持事务 所以采用了 dapper + dapperExtensions  并配套 生成实体类小工具的方 ...

  9. Asp.Net Core如何根据数据库自动生成实体类

    通过引用Nuget包添加实体类 运行 Install-Package Microsoft.EntityFrameworkCore.SqlServer 运行 Install-Package Micros ...

随机推荐

  1. JAVA从字符串中提取纯数字

    /** * 从字符串中提取纯数字 * @param str * @return */ public static String getNumeric(String str) { String regE ...

  2. C语言读写二进制文件

    fseek用法 fseek用来移动文件指针.函数原型 int fseek(FILE * stream, long offset, int fromwhere); 参数解释: stream 是文件流指针 ...

  3. 【LeetCode】1162. 地图分析 As Far from Land as Possible(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 这个题想考察什么? 剩下的任务就是套模板! 日期 题目 ...

  4. 【LeetCode】11. Container With Most Water 盛最多水的容器

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:盛水,容器,题解,leetcode, 力扣,python ...

  5. 【LeetCode】429. N-ary Tree Level Order Traversal 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...

  6. 记一次引入Elasticsearch的系统架构实战

    前言 我曾经面试安踏的技术岗,当时面试官问了我一个问题:如果你想使用某个新技术但是领导不愿意,你怎么办? 对于该问题我相信大家就算没有面试被问到过,现实工作中同事之间的合作也会遇到. 因此从我的角度重 ...

  7. css怎么实现雪人

    冬天来了,怎么能少的了雪人呢,不管是现实中还是程序员的代码中统统都的安排上,那就一次安排几个雪人兄弟,咱们先看效果图: 有喜欢的就赶紧cv拿走吧!!! 其详细代码如下: 图1 html部分: < ...

  8. 带SD读卡的USB HUB方案芯片MA8621|用于带读卡的USB HUB拓展坞方案芯片MA8621

    MA8621是一款带SD读卡器控制器的USB 2.0高速3端口集线器方案芯片,主要用在USB TYPEC拓展坞或者USB typec扩展底座上面. 1. MA8621功能概述 MA8621是USB 2 ...

  9. 编写Java程序,使用 Java 的 I/O 流将 H:\eclipse.zip 文件拷贝至 E 盘下,重新命名为 eclipse 安装 .zip。

    查看本章节 查看作业目录 需求说明: 使用 Java 的 I/O 流将 H:\eclipse.zip 文件拷贝至 E 盘下,重新命名为 eclipse 安装 .zip.在拷贝过程中,每隔2000 毫秒 ...

  10. 03.python封装与解构

    封装与结构 基本概念 t1 = 1, 2 print(type(t1)) # 什么类型 t2 = (1, 2) print(type(t2)) Python等式右侧出现逗号分隔的多值的时候,就会将这几 ...