发布时间:2022-04-08 09:34:08来源:ABC=>Go阅读(877)
原文:https://www.cnblogs.com/ABC-wangyuhan/p/15853442.html
1.1 安装两个依赖包 <PackageReference Include="Mapster.DependencyInjection" Version="1.0.0" /> <PackageReference Include="Mapster" Version="7.2.0" />
1.2 配置映射关系
public class InitMapsterAdaptConifg
{
public TypeAdapterConfig InitMapperConfig()
{
TypeAdapterConfig config = new TypeAdapterConfig();
config.ForType()
.Map(s => s.SchoolId, d => d.Id)
.Map(s => s.SchoolIdName, d => d.Id + d.Name)
.Map(d => d.SchoolName, s => s.Name)
.IgnoreNullValues(true);
return config;
}
}
1.3 Startup 中注入服务 TypeAdapterConfig 必须Singleton 单例注入
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton(new InitMapsterAdaptConifg().InitMapperConfig());
services.AddScoped();
}
1.4 在服务中注入 调用
private readonly IMapper _mapper;
public SchoolAppService(ISqlSugarClient context = null, IMapper mapper = null) : base(context)
{
base.Context = context;
_mapper = mapper;
}
public async Task> GetSchoolPage(SchoolPagedInput input)
{
PagedOutput output = new PagedOutput();
RefAsync total = 0;
var query = Context.Queryable()
.WhereIF(!string.IsNullOrWhiteSpace(input.Name), x => x.Name.Contains(input.Name));
output.TotalCount = await query.CountAsync();
var scList = await query
.OrderBy(x => x.Id, OrderByType.Desc)
.ToPageListAsync(input.PageIndex, input.PageSize, total);
output.TotalCount = total.Value;
output.Data = _mapper.Map, IEnumerable>(scList);
return output;
}
关键字: Mapster
965
1577
902
1447
1368
1793
5528
1355
1088
1215
9598
6001
5528
5122
4573
4276
3421
3340
3339
3274