php网站开发就业杭州app定制公司
web/
2025/9/26 22:56:42/
文章来源:
php网站开发就业,杭州app定制公司,房地产贷款最新政策,网站下载的网页修改下面版权所有WeihanLi.Npoi 1.18.0 ReleasedIntro前段时间一直在想#xff0c;把现在的配置做成类似于 AutoMapper 和 FluentValidation 那样#xff0c;把每个类型的 mapping 配置放在一个类中#xff0c;这样我们就可以比较好地组织我们的 mapping 关系#xff0c;也可以配置多个 map… WeihanLi.Npoi 1.18.0 ReleasedIntro前段时间一直在想把现在的配置做成类似于 AutoMapper 和 FluentValidation 那样把每个类型的 mapping 配置放在一个类中这样我们就可以比较好地组织我们的 mapping 关系也可以配置多个 mapping动态地进行切换于是就想着今天实现这个 feature。Sample在 1.18.0 版本中会加入一个 IMappingProfileTEntity 的接口要使用 fluent API 方式自定义 mapping 关系的时候可以实现这个接口这个接口的定义非常的简单定义如下public interface IMappingProfile
{
}public interface IMappingProfileT
{public void Configure(IExcelConfigurationT configuration);
}
这里增加了一个非泛型的接口实际使用主要是使用泛型接口非泛型的接口目前是一个空接口用来过滤不符合条件的类型。使用的示例如下public class NoticeProfile: IMappingProfileNotice
{public void Configure(IExcelConfigurationNotice 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);
Whats Inside实现方式比较简单通过扫描程序集或加载指定类型通过反射创建一个 mapping profile 实例并注册 mapping 关系。foreach (var type in types.Where(x x.IsAssignableToIMappingProfile()))
{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 反馈Referenceshttps://github.com/WeihanLi/WeihanLi.Npoihttps://github.com/OpenReservation/ReservationServer
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/81071.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!