Common: add converter

This commit is contained in:
Maksim Chegulov 2022-02-18 16:00:02 +03:00
parent 48d2685e67
commit fee7a2ad72
2 changed files with 27 additions and 1 deletions

View File

@ -23,11 +23,17 @@
*
*/
using ASC.Common.Mapping.PrimitiveTypeConverters;
namespace ASC.Common.Mapping;
public class MappingProfile : Profile
{
public MappingProfile() => Array.ForEach(AppDomain.CurrentDomain.GetAssemblies(), a => ApplyMappingsFromAssembly(a));
public MappingProfile()
{
Array.ForEach(AppDomain.CurrentDomain.GetAssemblies(), a => ApplyMappingsFromAssembly(a));
ApplyPrimitiveMappers();
}
private void ApplyMappingsFromAssembly(Assembly assembly)
{
@ -50,4 +56,10 @@ public class MappingProfile : Profile
methodInfo?.Invoke(instance, new object[] { this });
}
}
private void ApplyPrimitiveMappers()
{
CreateMap<long, DateTime>().ReverseMap()
.ConvertUsing<TimeConverter>();
}
}

View File

@ -0,0 +1,14 @@
namespace ASC.Common.Mapping.PrimitiveTypeConverters;
public class TimeConverter : ITypeConverter<long, DateTime>, ITypeConverter<DateTime, long>
{
public DateTime Convert(long source, DateTime destination, ResolutionContext context)
{
return new DateTime(source);
}
public long Convert(DateTime source, long destination, ResolutionContext context)
{
return source.Ticks;
}
}