using Mapster;
namespace SlnMesnac.WCS.Library
{
public class CoreMapper
{
///
/// Map source object to destination object
///
/// The type of the destination.
/// The source object.
/// The destination object.
/// Thrown when source is null.
public static TDestination Map(object source)
{
if (source == null)
throw new ArgumentNullException("Map Error : source is null");
TypeAdapterConfig config = new TypeAdapterConfig();
config.NewConfig().MapWith(dest => string.IsNullOrEmpty(dest.Trim()) ? Guid.Empty : new Guid(dest));
config.NewConfig().MapWith(dest => string.IsNullOrEmpty(dest.Trim()) ? null : new Guid(dest));
config.NewConfig().MapWith(dest => dest.ToString());
config.NewConfig().MapWith(dest => string.IsNullOrEmpty(dest) ? DateTime.Now : Convert.ToDateTime(dest));
config.NewConfig().MapWith(dest => string.IsNullOrEmpty(dest) ? null : Convert.ToDateTime(dest));
config.NewConfig().MapWith(dest => dest.ToString());
// var c = source.Adapt(destination, config2);
config.NewConfig().MapWith(dest => dest.Trim());
// config.ForType().Map(member: guid => guid, source: @string => string.IsNullOrEmpty(@string) ? Guid.Empty : new Guid(@string));
// config.ForType().Map(member: @string => @string, source: guid => ("wode"));
config.Default.IgnoreNullValues(true);//忽略null,原本的类字段的值是null,映射出来是"",int? null
config.Default.NameMatchingStrategy(NameMatchingStrategy.IgnoreCase);//忽略大小写
var destination = source.Adapt(config);
return destination;
}
}
}