My POCO classes contain DateOnly properties. My ViewModel hs these same named columns but as DateTime? which are needed for editing. Currently I create deal with this like this:
TypeAdapterConfig<Model, ViewModel>.NewConfig()
.Map(vm => vm.ActiveDate, m => m.ActiveDate.ToDateTime());
TypeAdapterConfig<ViewModel, Model>.NewConfig()
.Map(p => p.ActiveDate, vm => vm.ActiveDate.ToDateOnly())
where .ToDateTime() and .ToDateOnly() are my extension methods to convert the types.
I have this in several different class/ViewModel pairs and would like to have one global way of giving Mapter a way to Convert between DateOnly to DateTime.
I also tried this:
TypeAdapterConfig<DateOnly, DateTime>.NewConfig().MapWith(src => src.ToDateTime());
TypeAdapterConfig<DateTime, DateOnly>.NewConfig().MapWith(src => src.ToDateOnly());
But that did not work - I get an exception when using the Adapt<src,target>:
InvalidOperationException: Cannot convert immutable type, please consider using 'MapWith' method to create mapping
In following Progman's suggestion to create a reproducible example I found a better understanding of my problem and the solution.
My attempt at using .MapWith did not deal with nullability differences. My code was converting between DateOnly and DateTime? but the .MapWith defined a way to convert DateOnly to DateTime