DotNetCommon-搜集.neter开发常用的功能
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                DotNetCommon-搜集.neter开发常用的功能
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                介紹
搜集.neter開發常用的功能,運行環境:.net4.7;.netstandard2.0;net5.0;
得益于在BC公司負責維護底層框架代碼,自己的精力逐漸從 “全棧” 轉移到專注于后端。
于是,以BC公司的公共類庫為基礎,搜集整理再加上自己的補充便有了現在的一個類庫。
功能列表
通用數據模型;
樹狀結構&平鋪數據的訪問;
序列化;
注冊表;
編碼和加解密;
分布式id&分布式流水號;
校驗框架;
壓縮&解壓縮;
驗證碼生成;
漢字轉拼音;
Dto和Entity轉換之Mapper擴展;
遞歸篡改對象的屬性值之Modify擴展;
將Dto屬性投影到Entity之ModifyByDto擴展;
不同數據類型間的轉換之To方法;
隨機數;
對象池;
基于內存的并發消息隊列;
反射工具;
主機診斷報告;
對象深度比對工具;
網絡幫助類;
單位轉換器(B/KB/MS/GB);
金額大小寫轉換;
枚舉類型擴展方法;
常用擴展方法;
更多功能介紹
查看Wiki:https://gitee.com/jackletter/DotNetCommon/wikis/
快速開始
1.安裝包
dotnet add package DotNetCommon --version 1.1.02. 引入命名空間
using DotNetCommon; using DotNetCommon.Extensions;3. 功能示例
3.1 數據模型
public Result<Person> GetUserNameById(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-1023Machine.SetMachineId(1);//生成分布式id//輸出示例: 185081270290616320long id = DistributeGenerator.NewId("key");//生成分布式流水號//輸出示例: sno202105250001000001string 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必須為0ctx.RuleFor(i => i.Id).MustEqualTo(0);//姓名不能為空且長度在1-4之間ctx.RuleFor(i => i.Name).MustNotNullOrEmptyOrWhiteSpace().MustLengthInRange(1, 4);//年齡要么為null,要么>=0ctx.RuleFor(i => i.Age).When(i => i != null, ctx => ctx.MustGreaterThanOrEuqalTo(0));//出生日期要么為null,要么>=1800-01-01ctx.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"); }更多介紹,參考Wiki:https://gitee.com/jackletter/DotNetCommon/wikis/
總結
以上是生活随笔為你收集整理的DotNetCommon-搜集.neter开发常用的功能的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Magicodes.IE 2.5.4.2
- 下一篇: 又一次Task.Wait引起的教训
