47 lines
2.0 KiB
C#
47 lines
2.0 KiB
C#
using AutoMapper;
|
|
using AutoMapper.Configuration;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace OnlyPrompt.Backend.Utils
|
|
{
|
|
public static class AutomapperExtensions
|
|
{
|
|
|
|
public static IMappingExpression<TSource, TDestination> CreateUpdateMap<TSource, TDestination>(this IMapperConfigurationExpression cfg, MemberList memberList = MemberList.Source)
|
|
{
|
|
return cfg.CreateMap<TSource, TDestination>(memberList)
|
|
.IgnoreNullMembers();
|
|
}
|
|
|
|
public static IMappingExpression<TSource, TDestination> MapMemberFrom<TSource, TDestination, TMember, TSourceMember>(this IMappingExpression<TSource, TDestination> mapping, Expression<Func<TDestination, TMember>> destinationMember, Expression<Func<TSource, TSourceMember>> sourceMember)
|
|
{
|
|
mapping.ForMember(destinationMember, x => x.MapFrom(sourceMember));
|
|
return mapping;
|
|
}
|
|
|
|
public static IMappingExpression<TSource, TDestination> IgnoreNullMembers<TSource, TDestination>(this IMappingExpression<TSource, TDestination> mapping)
|
|
{
|
|
mapping.ForAllMembers(opts => opts.Condition((src, dest, member) => src != null));
|
|
return mapping;
|
|
}
|
|
|
|
public static IMappingExpression<TSource, TDestination> MapCtorParamFrom<TSource, TDestination, TMember, TSourceMember>(this IMappingExpression<TSource, TDestination> mapping, Expression<Func<TDestination, TMember>> destinationMember, Expression<Func<TSource, TSourceMember>> sourceMember)
|
|
{
|
|
mapping.ForCtorParam(destinationMember, x => x.MapFrom(sourceMember));
|
|
return mapping;
|
|
}
|
|
|
|
public static IMappingExpression<TSource, TDestination> ForCtorParam<TSource, TDestination, DValue>(this IMappingExpression<TSource, TDestination> mapping, Expression<Func<TDestination, DValue>> paramSelector, Action<ICtorParamConfigurationExpression<TSource>> configure)
|
|
{
|
|
var ctorParamName = ((MemberExpression)paramSelector.Body).Member.Name;
|
|
mapping.ForCtorParam(ctorParamName, configure);
|
|
return mapping;
|
|
}
|
|
}
|
|
}
|