Auto.Core 1.0.0

dotnet add package Auto.Core --version 1.0.0
NuGet\Install-Package Auto.Core -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Auto.Core" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Auto.Core --version 1.0.0
#r "nuget: Auto.Core, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Auto.Core as a Cake Addin
#addin nuget:?package=Auto.Core&version=1.0.0

// Install Auto.Core as a Cake Tool
#tool nuget:?package=Auto.Core&version=1.0.0

Auto Core (基于AspectCore)

logo.png

介绍

AutoCore是基于 .Net Standard 2.1用于简化 ASP.NET Core开发,AutoCoreAspectCore 的基础上进行功能开发,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 |
快速开始
  1. 安装
Install-Package Auto.Core
dotnet add package Auto.Core
  1. 配置 ServiceProviderFactory
builder.Host.UseServiceProviderFactory(new AutoServiceProviderFactory());
  1. 注册AutoCore
builder.Services.AddAutoCore(builder.Configuration);
  1. 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;
}

  1. 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;
}
  1. 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;
}
  1. 参数校验
//参数校验:参数标记校验方法
public WeatherForecast([NotNull] string userName)
{
  string  un = userName;
}
AutoCache(缓存)
  1. redis缓存提供
Install-Package Auto.Core.Redis
  1. appsettings.json
{
  "RedisOptions": {
    "Host": "127.0.0.1",
    "Port": 6379,
    "Database": 0
  }
}
AutoValidation(参数校验)
  1. 字符串最大长度 [MaxLengthAttribute]

  2. 字符串最小长度 [MinLengthAttribute]

  3. 字符串不能为空或Null [NotNullOrEmptyAttribute]

  4. 字符串不能为Null或空格 [NotNullOrWhiteSpaceAttribute]

  5. 对象不能为Null [NotNullAttribute]

  6. 范围 [RangeAttribute]

常见问题

功能无法正常使用

  1. 检查方法设置为 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();
  1. 检查是否注册AutoCore
builder.Services.AddAutoCore(builder.Configuration);
  1. 检查是否配置ServiceProviderFactory
builder.Host.UseServiceProviderFactory(new AutoServiceProviderFactory());
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Auto.Core:

Package Downloads
Auto.Caching.Redis

基于FreeRedis实现的缓存

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 225 7/13/2023

缓存;选项;参数校验;