# Auto.Core (基於AspectCore) ![logo.png](https://github.com/zhangxianchengvip/auto/blob/main/logo/logo.png?raw=true) #### 介紹 **[Auto.Core](https://githu ...
Auto.Core (基於AspectCore)
介紹
Auto.Core是基於 .Net Standard 2.1用於簡化 ASP.NET Core開發,Auto.Core 在 AspectCore 的基礎上進行功能開發,AspectCore 在性能上都比反射有2個數量級的優化,達到了和硬編碼調用相同的數量級。
AspectCore 方法調用反射擴展
性能測試:(Reflection為.NET Core提供的反射調用,Reflector為AspectCore.Extension.Reflection調用,Native為硬編碼調用
| Method | Mean | Error | StdDev | StdErr | Op/s |
|------------------- |------------:|----------:|----------:|----------:|----------------:|
| Native_Call | 1.0473 ns | 0.0064 ns | 0.0050 ns | 0.0015 ns | 954,874,046.8 |
| Reflection_Call | 91.9543 ns | 0.3540 ns | 0.3311 ns | 0.0855 ns | 10,874,961.4 |
| Reflector_Call | 7.1544 ns | 0.0628 ns | 0.0587 ns | 0.0152 ns | 139,774,408.3 |
快速開始
- 安裝
Install-Package Auto.Core
dotnet add package Auto.Core
- 配置 ServiceProviderFactory
builder.Host.UseServiceProviderFactory(new AutoServiceProviderFactory());
- 註冊AutoCore
builder.Services.AddAutoCore(builder.Configuration);
- AutoOptions (選項)
//appsettings.json
{
"Redis": {
"Host": "localhost",
"Port": 6379,
"Password": "zxc123..."
}
}
//選項類:標記綁定
[AutoOptions(Node ="Redis")]
public class Redis
{
public string Host { get; set; }
public int Port { get; set; }
public string Password { get; set; }
}
//構造函數註入
private readonly Redis _redis;
public WeatherForecast(IOptionsSnapshot<Redis> options)
{
_redis = options.Value;
}
- AutoCache (緩存)
//方法:標記緩存
[AutoCache]
public virtual async Task<IEnumerable<WeatherForecast>> Get(User user)
{
var ss = Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
}).ToArray();
return ss;
}
- AutoService(服務註冊)
//介面
public interface IUser
{
void Get();
}
//實現:標記註冊
[AutoService]
public class User : IUser
{
public void Get()
{
Console.WriteLine(1);
}
}
//構造函數註入
private readonly IUser _user;
public WeatherForecastController(IUser user)
{
_user = user;
}
- 參數校驗
//參數校驗:參數標記校驗方法
public WeatherForecast([NotNull] string userName)
{
string un = userName;
}
AutoCache(緩存)
- redis緩存提供
Install-Package Auto.Core.Redis
- 註冊
builder.Services.AddAutoRedis();
- appsettings.json
{
"RedisOptions": {
"Host": "127.0.0.1",
"Port": 6379,
"Database": 0
}
}
AutoValidation(參數校驗)
-
字元串最大長度 [MaxLengthAttribute]
-
字元串最小長度 [MinLengthAttribute]
-
字元串不能為空或Null [NotNullOrEmptyAttribute]
-
字元串不能為Null或空格 [NotNullOrWhiteSpaceAttribute]
-
對象不能為Null [NotNullAttribute]
-
範圍 [RangeAttribute]
常見問題
功能無法正常使用
- 檢查方法設置為 virtual
[HttpPost(Name = "GetWeatherForecast")]
[AutoCache]
public virtual async Task<IEnumerable<WeatherForecast>> Get(User user)
{
var ss = Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
}).ToArray();
return ss;
}
}
註意:控制器中的方法需要註冊為服務後才可以使用
builder.Services.AddControllers().AddControllersAsServices();
- 檢查是否註冊AutoCore
builder.Services.AddAutoCore(builder.Configuration);
- 檢查是否配置ServiceProviderFactory
builder.Host.UseServiceProviderFactory(new AutoServiceProviderFactory());