I currently manually do my DTO => ViewModel transformations in my MVC project. So, the code looks like this:
var model = new LandingModel {
FamilyName = token.FamilyName,
LoggedInUser = token.DisplayName,
TimeZoneName = token.TimeZoneName,
CurrentDateTime = Common.SessionManager.GetSessionDate().ToString(SharedLib.Constants.FMT_DATE_AND_TIME_LONG)
};
A LandingModel looks like this:
public class LandingModel
{
public string FamilyName { get; set; }
public string LoggedInUser { get; set; }
public string TimeZoneName { get; set; }
public string CurrentDateTime { get; set; }
}
How do I handle then CurrentDateTime? It's a string in the model, and is handled via by getting the users timezone date time from a session variable, and applying a string format.
How can I do this using Mapper.Map<SessionToken>(model));
Note, .GetSessionDate() simply takes UTC date, and adds an offset from the users timezone, to give the current date according to them.
You can handle this scenario in your MapperConfiguration
as indicated below:
var config = new MapperConfiguration(
cfg =>
{
cfg.CreateMap<SessionToken, LandingModel>()
.AfterMap((src, dest) => dest.CurrentDateTime = Common.SessionManager.GetSessionDate().ToString(SharedLib.Constants.FMT_DATE_AND_TIME_LONG));
});
REFERENCE:
AutoMapper - Before and after map actions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With