- A+
所属分类:.NET技术
Auto Core (基于AspectCore)
介绍
AutoCore是基于 .Net Standard 2.1用于简化 ASP.NET Core开发,AutoCore 在 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());