.net6之automapper
概念
AutoMapper是以.NET(C#)语言开发的一个轻量的处理一个实体对象到另一个实体对象之间映射关系的组件库。开发人员需要做的是通过AutoMapper配置两个实体对象之间的一些映射关系。就可以直接实现映射关系的复用,提高开发效率,减少重复代码。
项目概览
引用外部包
IAutoMapperConfiguration
[IgnoreAutoInjection]
public interface IAutoMapperConfiguration
{
void Configuration(MapperConfigurationExpression mapper);
}
IMapperExtensions
public static class IMapperExtensions
{
private static IMapper _mapper;
/// <summary>
/// 设置对象映射执行者
/// </summary>
/// <param name="mapper"></param>
internal static void SetMapper(IMapper mapper)
{
_mapper = mapper;
}
/// <summary>
/// 将对象映射为指定类型
/// </summary>
/// <typeparam name="TTarget"></typeparam>
/// <param name="source"></param>
/// <returns></returns>
public static TTarget MapTo<TTarget>(this object source)
{
if (source == null)
return default;
return _mapper.Map<TTarget>(source);
}
/// <summary>
/// 将对象映射为指定类型
/// </summary>
/// <typeparam name="TSource"></typeparam>
/// <typeparam name="TTarget"></typeparam>
/// <param name="source"></param>
/// <returns></returns>
public static TTarget MapTo<TSource, TTarget>(this TSource source)
{
if (source == null)
return default;
return _mapper.Map<TSource, TTarget>(source);
}
/// <summary>
/// 将对象映射为指定类型
/// </summary>
/// <typeparam name="TSource"></typeparam>
/// <typeparam name="TTarget"></typeparam>
/// <param name="source"></param>
/// <param name="desc"></param>
/// <returns></returns>
public static TTarget MapTo<TSource, TTarget>(this TSource source, TTarget desc)
{
if (source == null)
return default;
return _mapper.Map(source, desc);
}
/// <summary>
/// 将对象分页集合映射为指定类型
/// </summary>
/// <typeparam name="TSource"></typeparam>
/// <typeparam name="TTarget"></typeparam>
/// <param name="source"></param>
/// <returns></returns>
public static IPagedList<TTarget> MapTo<TSource, TTarget>(this IPagedList<TSource> source)
{
if (source == null)
return default;
IEnumerable<TTarget> items = null;
if (source.HasVal())
items = _mapper.Map<IEnumerable<TSource>, IEnumerable<TTarget>>(source.Items);
return new PagedList<TTarget>(
source.PageIndex,
source.PageSize,
source.Total,
items);
}
}
ServiceCollectionExtensions
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddAutoMapper(this IServiceCollection services, Action<MapperConfigurationExpression> setup = null)
{
var cfg = new MapperConfigurationExpression();
setup?.Invoke(cfg);
CreateAttributeMap(cfg);
foreach (Type cType in AssemblyManager.FindTypes(type => type.IsClass && type.GetInterfaces().Contains(typeof(IAutoMapperConfiguration))))
{
((IAutoMapperConfiguration)Activator.CreateInstance(cType)).Configuration(cfg);
}
IMapper mapper = new MapperConfiguration(cfg).CreateMapper();
IMapperExtensions.SetMapper(mapper);
services.AddSingleton(mapper);
return services;
}
static void CreateAttributeMap(MapperConfigurationExpression cfg)
{
var tuples = new List<(Type source, Type target)>();
foreach (var targetType in AssemblyManager.FindTypes(type => type.IsClass && !type.IsAbstract && type.IsDefined(typeof(AutoMapFromAttribute), true)))
{
foreach (Type sourceType in targetType.GetAttribute<AutoMapFromAttribute>(true).SourceTypes)
{
var tuple = ValueTuple.Create(sourceType, targetType);
tuples.AddIfNotExist(tuple);
}
}
foreach (var sourceType in AssemblyManager.FindTypes(type => type.IsClass && !type.IsAbstract && type.IsDefined(typeof(AutoMapToAttribute), true)))
{
foreach (Type targetType in sourceType.GetAttribute<AutoMapToAttribute>(true).TargetTypes)
{
var tuple = ValueTuple.Create(sourceType, targetType);
tuples.AddIfNotExist(tuple);
}
}
foreach ((Type source, Type target) tuple in tuples)
{
cfg.CreateMap(tuple.source, tuple.target);
}
}
}
打包传送门 https://www.cnblogs.com/inclme/p/16053978.html
.net6中使用
引用上述nuget包
添加服务
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAutoMapper();
使用示例
配置映射明细
/// <summary>
/// 模型映射配置
/// </summary>
internal class AutoMapperConfiguration : IAutoMapperConfiguration
{
public void Configuration(MapperConfigurationExpression mapper)
{
mapper.CreateMap<Dto.Request.User.CreateUserRequest, User>();
}
}
使用
var user = request.MapTo<User>();