namespace Dpz.Core.Service;
public class ServiceMapper
{
private static readonly Lazy<ServiceMapper> LazyMapper = new(() => new ServiceMapper());
private ServiceMapper()
{
Mapper = ConfigGlobalMapper();
}
public IMapper Mapper { get; }
public static ServiceMapper GetInstance()
{
return LazyMapper.Value;
}
public static ServiceMapper Instance => LazyMapper.Value;
private IMapper ConfigGlobalMapper()
{
var types = Assembly.Load("Dpz.Core.Public.ViewModel").GetExportedTypes();
var config = new MapperConfigurationExpression();
// 默认规则Mapper
var maps = from x in types
from y in x.GetInterfaces()
let faceType = y.GetTypeInfo()
where faceType.IsGenericType && y.GetGenericTypeDefinition() == typeof(IMapFrom<>) &&
!x.IsAbstract && !x.IsInterface
select new
{
Source = y.GetGenericArguments()[0],
Destination = x
};
foreach (var item in maps)
{
config.CreateMap(item.Source, item.Destination);
config.CreateMap(item.Destination, item.Source);
}
// 自定义Mapper
var customMaps = from x in types
from y in x.GetInterfaces()
where typeof(IHaveCustomMapping).IsAssignableFrom(x) &&
!x.GetTypeInfo().IsAbstract &&
!x.GetTypeInfo().IsInterface
let instance = Activator.CreateInstance(x) as IHaveCustomMapping
where instance != null
select instance;
foreach (var item in customMaps)
{
item.CreateMappings(config);
}
config.CreateMap<string, ObjectId>().ConstructUsing((x, y) =>
!string.IsNullOrEmpty(x) && ObjectId.TryParse(x, out var oid) ? oid : ObjectId.Empty);
// config.CreateMap<DateTime, DateTime>().ConvertUsing((utc, local, context) =>
// {
// if (local == new DateTime())
// {
// if (utc.Kind == DateTimeKind.Utc)
// return utc.ToLocalTime();
// return utc;
// }
// if (local.Kind == DateTimeKind.Local)
// return utc.ToUniversalTime();
// return local;
// });
var cfg = new MapperConfiguration(config);
IMapper mapper = new Mapper(cfg);
return mapper;
}
}