.Net Source generator powerful components which used roslyn's SourceGenerator feature
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.
dotnet add package SourceGeneratorPower.Options.Abstractions
dotnet add package SourceGeneratorPower.Options.SourceGenerator
- Marked Option class with OptionAttribute and Configuration section key
[Option("Greet")]
public class GreetOption
{
public string Text { get; set; }
}
- 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
v1.1.3
- Fix namespace conflict with
global::
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.
dotnet add package SourceGeneratorPower.HttpClient.Abstractions
dotnet add package SourceGeneratorPower.HttpClient.SourceGenerator
- Marked interface with HttpClientAttribute and given HttpClient name
[HttpClient("JsonServer")]
public interface IJsonServerApi
{
[HttpGet("/todos/{id}")]
Task<Todo> Get(int id, CancellationToken cancellationToken = default);
}
- 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
v1.1.3
- Fix namespace conflict with
global::