Nullable datetime to datetime converter automapper

I have a situation where my DTOs require DateTime properties, but my POCOs use values ​​with a null value. To avoid creating ForMember for each property with this condition, I created ITypeConverter<DateTime?, DateTime> . The problem I am facing is that both DTO and POCO are NULL, which allows a null value that is called by this converter. DestinationType has a DateTime value, although the property is a datetime value of zero. Any idea how I could make this converter work only for actual zero dates?

 public class FooDTO { public DateTime? FooDate { get; set; } } public class FooPoco { public DateTime? FooDate { get; set; } } class Program { static void Main(string[] args) { Mapper.CreateMap<FooDTO, FooPoco>(); Mapper.CreateMap<DateTime?, DateTime>() .ConvertUsing<NullableDateTimeConverter>(); var poco = new FooPoco(); Mapper.Map(new FooDTO() { FooDate = null }, poco); if (poco.FooDate.HasValue) Console.WriteLine( "This should be null : {0}", poco.FooDate.Value.ToString()); //Value is always set else Console.WriteLine("Mapping worked"); } } public class NullableDateTimeConverter : ITypeConverter<DateTime?, DateTime> { // Since both are nullable date times and this handles converting // nullable to datetime I would not expect this to be called. public DateTime Convert(ResolutionContext context) { var sourceDate = context.SourceValue as DateTime?; if (sourceDate.HasValue) return sourceDate.Value; else return default(DateTime); } } 

I found this AutoMapper TypeConverter post matching a type with a null value to a type not nullable, but that didn't help much.

+4
source share
1 answer

Without looking, I suspect that he called your TypeCoverter , because it TypeCoverter best for converted types.

If you create another TypeConverter with the correct types, it should work fine. For instance:

 public class DateTimeConverter : ITypeConverter<DateTime?, DateTime> { public DateTime Convert(ResolutionContext context) { var sourceDate = context.SourceValue as DateTime?; if (sourceDate.HasValue) return sourceDate.Value; else return default(DateTime); } } public class NullableDateTimeConverter : ITypeConverter<DateTime?, DateTime?> { public DateTime? Convert(ResolutionContext context) { var sourceDate = context.SourceValue as DateTime?; if (sourceDate.HasValue) return sourceDate.Value; else return default(DateTime?); } } 

Please note that if you want to simplify them to

 public class DateTimeConverter : TypeConverter<DateTime?, DateTime> { protected override DateTime ConvertCore(DateTime? source) { if (source.HasValue) return source.Value; else return default(DateTime); } } public class NullableDateTimeConverter : TypeConverter<DateTime?, DateTime?> { protected override DateTime? ConvertCore(DateTime? source) { return source; } } 

Then simply initialize both converters:

 Mapper.CreateMap<DateTime?, DateTime>().ConvertUsing<DateTimeConverter>(); Mapper.CreateMap<DateTime?, DateTime?>().ConvertUsing<NullableDateTimeConverter>(); Mapper.AssertConfigurationIsValid(); 
+6
source

All Articles