Mapster
Mapster 是一个比较小巧、比较快捷的对象映射器,可以将一个对象映射到另一个对象。Fireasy 提供了 Mapster 对 对象映射器 的实现。
修改配置文件如下:
- .Net Framework 下的 app.config 或 web.config 文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="fireasy">
<section name="objMappers" type="Fireasy.Common.Mapper.Configuration.ObjectMapperConfigurationSectionHandler, Fireasy.Common" />
</sectionGroup>
</configSections>
<fireasy>
<objMappers default="mapster">
<mapper name="mapster" type="Fireasy.Mapster.ObjectMapper, Fireasy.Mapster" />
</objMappers>
</fireasy>
</configuration>
- .Net Core 下的 appsettings.json 文件
{
"fireasy": {
"objMappers": {
"default": "mapster",
"settings": {
"mapster": {
"type": "Fireasy.Mapster.ObjectMapper, Fireasy.Mapster"
}
}
}
}
}
在 .Net Core 应用中可以使用扩展方法 AddMapster 切换到 Maspter,同时使用 CreateMap 方法进行映射。如下所示:
namespace demo
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddFireasy().AddMapster(s =>
{
s.CreateMap<SysUser, UserDto>()
.CreateMap<SysModule, ModuleDto>(t =>
t.Map(w => w.Url, w => "http://www.fireasy.cn/" + w.Url))
.CreateMap<SysRole, RoleDto>();
//程序集搜索 IRegister
s.AddRegisters(this.GetType().Assembly);
});
}
}
}