日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

WeihanLi.Npoi 1.18.0 Released

發布時間:2023/12/4 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WeihanLi.Npoi 1.18.0 Released 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

WeihanLi.Npoi 1.18.0 Released

Intro

前段時間一直在想,把現在的配置做成類似于 AutoMapper 和 FluentValidation 那樣,把每個類型的 mapping 配置放在一個類中,這樣我們就可以比較好地組織我們的 mapping 關系,也可以配置多個 mapping,動態地進行切換,于是就想著今天實現這個 feature。

Sample

在 1.18.0 版本中會加入一個 IMappingProfile<TEntity> 的接口,要使用 fluent API 方式自定義 mapping 關系的時候可以實現這個接口,這個接口的定義非常的簡單,定義如下:

public?interface?IMappingProfile { }public?interface?IMappingProfile<T> {public?void?Configure(IExcelConfiguration<T>?configuration); }

這里增加了一個非泛型的接口,實際使用主要是使用泛型接口,非泛型的接口目前是一個空接口,用來過濾不符合條件的類型。

使用的示例如下:

public?class?NoticeProfile:?IMappingProfile<Notice> {public?void?Configure(IExcelConfiguration<Notice>?noticeSetting){noticeSetting.HasAuthor("WeihanLi").HasTitle("WeihanLi.Npoi?test").HasSheetSetting(setting?=>{setting.SheetName?=?"NoticeList";setting.AutoColumnWidthEnabled?=?true;});noticeSetting.Property(_?=>?_.Id).HasColumnIndex(0);noticeSetting.Property(_?=>?_.Title).HasColumnIndex(1);noticeSetting.Property(_?=>?_.Content).HasColumnIndex(2);noticeSetting.Property(_?=>?_.Publisher).HasColumnIndex(3);noticeSetting.Property(_?=>?_.PublishedAt).HasColumnIndex(4).HasColumnOutputFormatter(x?=>?x.ToStandardTimeString());} }

在注冊 IMappingProfile 的時候我們可以通過指定 Type 和程序集掃描兩種方式來注冊,Type 注冊可以獲取類型的可訪問性,只要能夠編譯通過就能注冊成功,程序集掃描只掃描 public 的類型成員,可以根據需要自行選擇:

void?LoadMappingProfiles(params?Assembly[]?assemblies); void?LoadMappingProfiles(params?Type[]?types);

使用示例如下:

//?Load?by?type FluentSettings.LoadMappingProfiles(typeof(NoticeProfile)); //?Load?by?assembly FluentSettings.LoadMappingProfiles(typeof(NoticeProfile).Assembly);

What's Inside

實現方式比較簡單,通過掃描程序集或加載指定類型,通過反射創建一個 mapping profile 實例并注冊 mapping 關系。

foreach?(var?type?in?types.Where(x?=>?x.IsAssignableTo<IMappingProfile>())) {var?profileInterfaceType?=?type.GetImplementedInterfaces().FirstOrDefault(x?=>?x.IsGenericType?&&?x.GetGenericTypeDefinition()?==?s_profileGenericTypeDefinition);if?(profileInterfaceType?is?null){continue;}var?profile?=?Activator.CreateInstance(type);var?entityType?=?profileInterfaceType.GetGenericArguments()[0];var?configuration?=?InternalHelper.GetExcelConfigurationMapping(entityType);var?method?=?profileInterfaceType.GetMethod(MappingProfileConfigureMethodName,new[]?{typeof(IExcelConfiguration<>).MakeGenericType(entityType)});method?.Invoke(profile,?new?object[]?{configuration}); }

More

具體使用可以參考項目單元測試和另外一個示例項目:https://github.com/OpenReservation/ReservationServer

利用 Source Generator 我們可以進一步的將反射的這一過程進行優化,在編譯時生成強類型的注冊代碼,這樣也可以進一步地優化注冊性能,不過考慮實際注冊的時候一般只會執行一次,而且目前 VS、Rider 對 Source Generator 的支持不是特別好,也就暫時沒考慮使用 Source Generator 的方式來做,后面可以再做優化

希望能夠通過這樣的功能把 mapping 關系的配置更好的組織起來,如果使用時有遇到問題或者覺得需要改進的,歡迎通過項目 issue 反饋

References

  • https://github.com/WeihanLi/WeihanLi.Npoi

  • https://github.com/OpenReservation/ReservationServer

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的WeihanLi.Npoi 1.18.0 Released的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。