Auto.Core
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 |快速开始
[*]安装
[*]Package Manager
Install-Package Auto.Core
[*].NET CLI
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..."
}
}
//选项类:标记绑定
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 (缓存)
//方法:标记缓存
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
}).ToArray();
return ss;
}
[*]AutoService(服务注册)
//接口
public interface IUser
{
void Get();
}
//实现:标记注册
public class User : IUser
{
public void Get()
{
Console.WriteLine(1);
}
}
//构造函数注入
private readonly IUser _user;
public WeatherForecastController(IUser user)
{
_user = user;
}
[*]参数校验
//参数校验:参数标记校验方法
public WeatherForecast( string userName)
{
stringun = userName;
}AutoCache(缓存)
[*]redis缓存提供
Install-Package Auto.Core.Redis
[*]注册
builder.Services.AddAutoRedis();
[*]appsettings.json
{
"RedisOptions": {
"Host": "127.0.0.1",
"Port": 6379,
"Database": 0
}
}AutoValidation(参数校验)
[*]字符串最大长度
[*]字符串最小长度
[*]字符串不能为空或Null
[*]字符串不能为Null或空格
[*]对象不能为Null
[*]范围
常见问题
功能无法正常使用
[*]检查方法设置为 virtual
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
}).ToArray();
return ss;
}
}注意:控制器中的方法需要注册为服务后才可以使用
builder.Services.AddControllers().AddControllersAsServices();
[*]检查是否注册AutoCore
builder.Services.AddAutoCore(builder.Configuration);
[*]检查是否配置ServiceProviderFactory
builder.Host.UseServiceProviderFactory(new AutoServiceProviderFactory());
来源:https://www.cnblogs.com/xianchengzhang/archive/2023/07/13/17551390.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页:
[1]