Skip to content

huiyuanai709/SourceGeneratorPower

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SourceGeneratorPower

.Net Source generator powerful components which used roslyn's SourceGenerator feature

Components

SourceGeneratorPower.Options

SourceGeneratorPower.Options is used C# roslyn's Source Generator feature to auto inject Options class which marked by OptionAttribute. It is convenient to develop, almost no loss of performance.

Installation

dotnet add package SourceGeneratorPower.Options.Abstractions
dotnet add package SourceGeneratorPower.Options.SourceGenerator

Usages

  1. Marked Option class with OptionAttribute and Configuration section key
[Option("Greet")]
public class GreetOption
{
    public string Text { get; set; }
}
  1. appsettings.json add Greet section
{
  "Greet": {
    "Text": "Hello world!"
  }
}

Main project use IServiceCollection extension method AutoInjectOptions

// .Net 6
builder.Services.AutoInjectOptions(builder.Configuration);

// .Net 5 StartUp.cs
service.AutoInjectOptions(Configuration);

Then build solution

See document auto inject options

Changelog

v1.1.3

  1. Fix namespace conflict with global::

SourceGeneratorPower.HttpClient

SourceGeneratorPower.HttpClient is used C# roslyn's Source Generator feature to auto implement HTTP API Caller interface, It depends on IHttpClientFactory to create HttpClient to sending request and receive response with System.Text.Json.

Installation

dotnet add package SourceGeneratorPower.HttpClient.Abstractions
dotnet add package SourceGeneratorPower.HttpClient.SourceGenerator

Usages

  1. Marked interface with HttpClientAttribute and given HttpClient name
[HttpClient("JsonServer")]
public interface IJsonServerApi
{
    [HttpGet("/todos/{id}")]
    Task<Todo> Get(int id, CancellationToken cancellationToken = default);
}
  1. Add implement type and HttpClient to DI container
builder.Services.AddGeneratedHttpClient();
builder.Services.AddHttpClient("JsonServer", options => options.BaseAddress = new Uri("https://jsonplaceholder.typicode.com"));

More attribute using in string interpolation

RequiredServiceAttribute
UsingAttribute

Then build solution

Changelog

v1.1.3

  1. Fix namespace conflict with global::

About

.Net 6 Source generator powerful components

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages