DotNetCommon.Core
1.9.1
See the version list below for details.
dotnet add package DotNetCommon.Core --version 1.9.1
NuGet\Install-Package DotNetCommon.Core -Version 1.9.1
<PackageReference Include="DotNetCommon.Core" Version="1.9.1" />
paket add DotNetCommon.Core --version 1.9.1
#r "nuget: DotNetCommon.Core, 1.9.1"
// Install DotNetCommon.Core as a Cake Addin #addin nuget:?package=DotNetCommon.Core&version=1.9.1 // Install DotNetCommon.Core as a Cake Tool #tool nuget:?package=DotNetCommon.Core&version=1.9.1
DotNetCommon
介绍
搜集.neter开发常用的功能,运行环境: .net4.7;.netstandard2.0;net5.0;net6.0;
注意: 从DotNetCommon 2.1.0之后,独立出来 DotNetCommon.Core包、DotNetCommon.PinYin包,目的是为了减少核心包体积,如下图:
功能列表
- 通用数据模型;
- 通用树状结构&平铺数据的访问;
- 通用身份认证模型;
- 通用数据类型转换之Object.To方法;
- 通用Dto间转换之Object.Mapper扩展;
- 递归篡改对象的属性值之Modify扩展;
- 将Dto属性投影到Entity之ModifyByDto扩展;
- 校验框架;
- 分布式id&分布式流水号;
- 编码和加解密;
- 序列化;
- 汉字转拼音;
- 压缩&解压缩;
- 注册表;
- 验证码生成;
- 随机数;
- 对象池;
- 基于内存的并发消息队列;
- 反射工具;
- 主机诊断报告;
- 对象深度比对工具;
- 网络帮助类;
- 单位转换器(B/KB/MS/GB);
- 金额大小写转换;
- 枚举类型扩展方法;
- 常用扩展方法;
- 中文乱码检测(GBK or UTF-8);
- 读取Properties文件;
- 通用日志Logger;
- 常用内存缓存;
- 异步锁(AsyncLocker);
- 表达式帮助类(ExpressionHelper);
更多功能介绍
查看:https://gitee.com/jackletter/DotNetCommon/tree/master/docs
快速开始
1.安装包
dotnet add package DotNetCommon
2. 引入命名空间
using DotNetCommon;
using DotNetCommon.Extensions;
3. 功能示例
3.1 数据模型
public Result<Person> GetUserById(int id)
{
if (id < 0) return Result.NotOk("id必须大于0!");
//...
return Result.Ok(new Person());
}
3.2 加解密
public void EncryptTest()
{
var sensitiveData = "敏感信息";
var key = "12345678";
//加密
var res = DESEncrypt.Encrypt(sensitiveData, key);
//解密
var res2= DESEncrypt.Decrypt(res, key);
}
3.3 分布式Id & 分布式流水号
public void Test()
{
//首先设置当前机器id: 0-1023
Machine.SetMachineId(1);
//生成分布式id
//输出示例: 185081270290616320
long id = DistributeGenerator.NewId("key");
//生成分布式流水号
//输出示例: sno202105250001000001
string sno = DistributeGenerator.NewSNO("sno", SerialFormat.CreateDistributeFast("sno", "yyyyMMdd", 6));
}
3.4 序列化
/// <summary>
/// 指定常用的设置,序列化为json字符串
/// </summary>
/// <param name="obj"></param>
/// <param name="dateFormatString">日期时间格式</param>
/// <param name="isLongToString">是否将long型转为字符串</param>
/// <param name="IgnoreNull">是否忽略null值的属性</param>
/// <param name="enum2String">是否将枚举转换为字符串</param>
/// <param name="lowerCamelCase">属性名称的首字母是否小写</param>
/// <param name="isIntend">是否格式缩进</param>
/// <param name="isAllToString">是否将所有数据类型转换为字符串</param>
/// <param name="allNumDigit">设定的所有数字的最大小数位数(默认为null,即: 不限制)</param>
/// <param name="decimalDigit">仅针对decimal设定的最大小数位数(默认为null,即: 不限制)</param>
/// <param name="otherSettings">其他的设置</param>
/// <returns></returns>
public static string ToJsonFast(this object obj, string dateFormatString = "yyyy-MM-dd HH:mm:ss", bool IgnoreNull = false, bool enum2String = true, bool lowerCamelCase = false, bool isIntend = false, bool isLongToString = false, bool isAllToString = false, int? allNumDigit = null, int? decimalDigit = null, Action<JsonSerializerSettings> otherSettings = null)
3.5 压缩&解压缩
//压缩单个文件
ZipHelper.ZipFile("c:\\tmp.zip","d:\\test.txt");
//压缩多个文件
ZipHelper.ZipFile("c:\\tmp.zip","d:\\test.txt","d:\\test2.txt");
//压缩单个目录
ZipHelper.ZipFolder("c:\\tmp.zip","d:\\test1");
//压缩多个目录
ZipHelper.ZipFolder("c:\\tmp.zip","d:\\test1","d:\\test2");
//压缩多个文件,并为每个文件指定名称
ZipHelper.ZipFile("c:\\tmp.zip",
("c:\\testfolder-中文B.txt", "重命名1.txt"),
("c:\\testsubfolder-suba.txt", "\\sub\\重命名2.txt"))
3.6 类似AutoMapper的转换
public class Cat
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public DateTime Birth { get; set; }
}
public class CatDto
{
public int Id { get; set; }
public string Name { get; set; }
public int Age
{
get
{
return DateTime.Now.Year - Birth.Year;
}
}
public DateTime Birth { get; set; }
}
//转换示例
var cat = new Cat()
{
Id = 1,
Name = "小明",
Birth = DateTime.Parse("1989-01-02"),
Age = 20
};
var dto = cat.Mapper<CatDto>();
dto.ShouldNotBeNull();
dto.Id.ShouldBe(1);
dto.Name.ShouldBe("小明");
dto.Age.ShouldNotBe(20);
3.7 类FluentValidation校验组件
//Service层方法,添加实体
public Result<bool> AddStudent(Student student)
{
var res = ValidateModelHelper.ValidResult(student, Student.ValidAdd);
if (!res.Success) return res;
//...新增操作
return Result.Ok(true);
}
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int? Age { get; set; }
public DateTime? Birth { get; set; }
public string IdCard { get; set; }
public string Addr { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
/// <summary>
/// 校验新增Student
/// </summary>
/// <param name="ctx"></param>
public static void ValidAdd(ValidateContext<Student> ctx)
{
//请求实体不能为null,否则直接中断校验
ctx.MustNotNull().IfFailThenExit();
//Id必须为0
ctx.RuleFor(i => i.Id).MustEqualTo(0);
//姓名不能为空且长度在1-4之间
ctx.RuleFor(i => i.Name).MustNotNullOrEmptyOrWhiteSpace().MustLengthInRange(1, 4);
//年龄要么为null,要么>=0
ctx.RuleFor(i => i.Age).When(i => i != null, ctx => ctx.MustGreaterThanOrEuqalTo(0));
//出生日期要么为null,要么>=1800-01-01
ctx.RuleFor(i => i.Birth).When(i => i != null, ctx => ctx.MustGreaterThanOrEuqalTo(DateTime.Parse("1800-01-01")));
//校验身份证号
ctx.RuleFor(i => i.IdCard).MustIdCard();
//如果手机号码不为null就校验格式
ctx.RuleFor(i => i.Phone).When(i => i != null, ctx => ctx.MustCellPhone());
//如果邮箱不为null就校验格式
ctx.RuleFor(i => i.Email).When(i => i != null, ctx => ctx.MustEmailAddress());
}
}
3.8 注册表
public void Test2()
{
var path = @"HKEY_CURRENT_USER\TestApplication\Res";
//判断是否存在
RegistryHelper.Exists(path);
//删除项
RegistryHelper.DeletePath(path);
//设置值
RegistryHelper.SetString(path, "name", "小明");
//读取值
var name = RegistryHelper.GetString(path, "name");
}
更多介绍,参考:https://gitee.com/jackletter/DotNetCommon/tree/master/docs
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. 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 | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 is compatible. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 is compatible. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETCoreApp 3.1
- Microsoft.CodeAnalysis.CSharp (>= 4.0.1)
- Microsoft.Extensions.Primitives (>= 6.0.0)
- Newtonsoft.Json (>= 12.0.3)
- System.Reflection.Emit (>= 4.3.0)
- System.Reflection.Emit.Lightweight (>= 4.3.0)
- System.Text.Encoding.CodePages (>= 5.0.0)
-
.NETFramework 4.7
- Microsoft.CodeAnalysis.CSharp (>= 4.0.1)
- Microsoft.Extensions.Primitives (>= 6.0.0)
- Newtonsoft.Json (>= 12.0.3)
- System.Reflection.Emit (>= 4.3.0)
- System.Reflection.Emit.Lightweight (>= 4.3.0)
- System.Runtime.InteropServices.RuntimeInformation (>= 4.3.0)
- System.Text.Encoding.CodePages (>= 5.0.0)
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 4.0.1)
- Microsoft.Extensions.Primitives (>= 6.0.0)
- Newtonsoft.Json (>= 12.0.3)
- System.Reflection.Emit (>= 4.3.0)
- System.Reflection.Emit.Lightweight (>= 4.3.0)
- System.Text.Encoding.CodePages (>= 5.0.0)
-
net5.0
- Microsoft.CodeAnalysis.CSharp (>= 4.0.1)
- Microsoft.Extensions.Primitives (>= 6.0.0)
- Newtonsoft.Json (>= 12.0.3)
- System.Reflection.Emit (>= 4.3.0)
- System.Reflection.Emit.Lightweight (>= 4.3.0)
- System.Text.Encoding.CodePages (>= 5.0.0)
-
net6.0
- Microsoft.CodeAnalysis.CSharp (>= 4.0.1)
- Microsoft.Extensions.Primitives (>= 6.0.0)
- Newtonsoft.Json (>= 12.0.3)
- System.Reflection.Emit (>= 4.3.0)
- System.Reflection.Emit.Lightweight (>= 4.3.0)
- System.Text.Encoding.CodePages (>= 5.0.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on DotNetCommon.Core:
Package | Downloads |
---|---|
DotNetCommon
.net常用功能及数据模型,包含: - 压缩/解压缩(CompressHelper) - 注册表操作(RegistryHelper) - 验证码(VerifyCodeHelper) - 汉字转拼音 - 数据模型: Result/ResultPage/PageQuery - 通用用户模型: User.Current.IdString - 编码加解密(DES/AES/RSA/MD5/Base64UrlSafe) - 分布式Id/分布式流水号 - 通用数据类型转换方法Object.To() - 通用dto属性映射转换方法Object.Mapper(); - 对象(poco)深度克隆方法Object.DeepClone(); - 树状结构数据操作(ToTree/ToFlat/FetchToTree) - json序列化(Object.ToJsonFast()) - 类FluentValidation功能的校验组件 - 随机数(RandomHelper) - 进程内队列模型(ProducerConsumerQueue) - 帮助根据dto更新表的ModifyByDto()扩展方法 - 各种常用扩展 - 中文乱码检测 - 通用日志组件 - 类java *.properties 文件读取 - 异步锁(AsyncLocker) - 等等。。。 |
|
DotNetCommon.Compress
压缩/解压缩帮助类: CompressHelper.cs |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated | |
---|---|---|---|
4.18.2 | 88 | 11/1/2024 | |
4.18.1 | 89 | 10/25/2024 | |
4.18.0 | 91 | 10/5/2024 | |
4.17.0 | 95 | 9/30/2024 | |
4.16.0 | 120 | 9/14/2024 | |
4.15.0 | 140 | 9/9/2024 | |
4.14.2 | 121 | 9/2/2024 | |
4.14.1 | 109 | 8/29/2024 | |
4.14.0 | 102 | 8/29/2024 | |
4.13.0 | 118 | 8/26/2024 | |
4.12.0 | 101 | 8/7/2024 | |
4.11.0 | 151 | 6/4/2024 | |
4.10.0 | 132 | 5/13/2024 | |
4.9.1 | 141 | 5/6/2024 | |
4.9.0 | 131 | 4/23/2024 | |
4.8.2 | 257 | 2/17/2024 | |
4.8.1 | 250 | 2/16/2024 | |
4.8.0 | 387 | 1/15/2024 | |
4.7.0 | 474 | 12/17/2023 | |
4.6.0 | 513 | 10/31/2023 | |
4.5.5 | 501 | 10/20/2023 | |
4.5.3 | 484 | 10/11/2023 | |
4.5.2 | 515 | 10/2/2023 | |
4.5.1 | 513 | 9/27/2023 | |
4.5.0 | 502 | 9/22/2023 | |
4.4.2 | 506 | 9/20/2023 | |
4.4.1 | 463 | 9/19/2023 | |
4.4.0 | 570 | 9/18/2023 | |
4.3.1 | 569 | 8/15/2023 | |
4.3.0 | 535 | 8/12/2023 | |
4.2.4 | 542 | 7/26/2023 | |
4.2.3 | 533 | 7/26/2023 | |
4.2.2 | 526 | 7/10/2023 | |
4.2.1 | 535 | 7/2/2023 | |
4.1.1 | 516 | 6/14/2023 | |
4.1.0 | 545 | 6/13/2023 | |
4.0.1 | 601 | 6/5/2023 | |
3.2.0 | 558 | 5/16/2023 | |
3.1.4 | 541 | 5/16/2023 | |
3.1.3 | 521 | 5/13/2023 | |
3.1.2 | 572 | 4/20/2023 | |
3.1.1 | 671 | 3/29/2023 | |
3.1.0 | 608 | 3/27/2023 | |
3.0.1 | 684 | 2/8/2023 | |
3.0.0 | 1,158 | 11/1/2022 | |
1.10.0 | 981 | 10/30/2022 | |
1.9.3 | 1,027 | 10/20/2022 | |
1.9.2 | 1,143 | 9/22/2022 | |
1.9.1 | 1,039 | 9/22/2022 | |
1.9.0 | 1,011 | 9/21/2022 | |
1.8.5 | 1,043 | 9/13/2022 | |
1.8.4 | 1,030 | 8/24/2022 | |
1.8.3 | 1,062 | 7/29/2022 | |
1.8.2 | 1,078 | 6/20/2022 | |
1.7.2 | 1,131 | 4/28/2022 | |
1.7.1 | 1,055 | 4/26/2022 | |
1.7.0 | 1,065 | 4/18/2022 | |
1.6.0 | 1,114 | 3/30/2022 | |
1.5.0 | 1,145 | 1/24/2022 | |
1.4.1 | 742 | 1/9/2022 | |
1.3.0 | 859 | 12/6/2021 | |
1.2.0 | 892 | 11/14/2021 | |
1.1.4 | 874 | 10/21/2021 | |
1.1.3 | 865 | 9/26/2021 | |
1.1.2 | 943 | 9/26/2021 | |
1.1.1 | 826 | 9/21/2021 | |
1.1.0 | 1,072 | 9/5/2021 | |
1.0.0 | 878 | 8/27/2021 |