2021-11-29 18:25:50 +01:00
|
|
|
namespace zero.Mapper;
|
|
|
|
|
|
|
|
|
|
public static class MapperExtensions
|
|
|
|
|
{
|
|
|
|
|
//public static TDestination Register<TSource, TDestination>(this IZeroMapper mapper, Action<TSource, TDestination, IZeroMapperContext> map) where TDestination : class, new()
|
|
|
|
|
//{
|
|
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//public static TDestination Register<TSource, TDestination>(this IZeroMapper mapper, Action<TSource, TDestination, IZeroMapperContext> map, TDestination destination)
|
|
|
|
|
//{
|
|
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static TDestination Map<TSource, TDestination>(this IZeroMapper mapper, TSource source, TDestination destination)
|
|
|
|
|
{
|
|
|
|
|
return mapper.Map(source, typeof(TSource), destination);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static TDestination Map<TSource, TDestination>(this IZeroMapper mapper, TSource source)
|
|
|
|
|
{
|
|
|
|
|
return mapper.Map<TDestination>(source, typeof(TSource), default);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-30 14:32:10 +01:00
|
|
|
public static Paged<TDestination> Map<TSource, TDestination>(this IZeroMapper mapper, Paged<TSource> source, Action<TSource, TDestination> modify = null)
|
2021-11-29 18:25:50 +01:00
|
|
|
{
|
2021-11-30 14:32:10 +01:00
|
|
|
return source.MapTo(srcItem =>
|
|
|
|
|
{
|
|
|
|
|
TDestination destination = mapper.Map<TSource, TDestination>(srcItem);
|
|
|
|
|
modify?.Invoke(srcItem, destination);
|
|
|
|
|
return destination;
|
|
|
|
|
});
|
2021-11-29 18:25:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Result<TDestination> Map<TSource, TDestination>(this IZeroMapper mapper, Result<TSource> source)
|
|
|
|
|
{
|
|
|
|
|
TDestination model = mapper.Map<TSource, TDestination>(source.Model);
|
|
|
|
|
return source.ConvertTo(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static Dictionary<string, TDestination> Map<TSource, TDestination>(this IZeroMapper mapper, Dictionary<string, TSource> source)
|
|
|
|
|
{
|
|
|
|
|
Dictionary<string, TDestination> model = new();
|
|
|
|
|
|
|
|
|
|
foreach ((string key, TSource sourceItem) in source)
|
|
|
|
|
{
|
|
|
|
|
model.Add(key, sourceItem == null ? default : mapper.Map<TSource, TDestination>(sourceItem));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return model;
|
|
|
|
|
}
|
2021-12-22 00:14:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
public static IEnumerable<TDestination> Map<TSource, TDestination>(this IZeroMapper mapper, IEnumerable<TSource> source)
|
|
|
|
|
{
|
|
|
|
|
return source.Select(x => mapper.Map<TSource, TDestination>(x));
|
|
|
|
|
}
|
2021-11-29 18:25:50 +01:00
|
|
|
}
|