I have an ASP.NET MVC 5 app and I map properties between model and viewmodel and vice versa.
I could use automapper to do this but since I don't need mapping rules to convince me to use it as I mostly do simple 1 to 1 mapping the performance overhead that automapper will add is not worthwhile. If I have mapping differences I'll map it manually where I reference it in my controller.
Here is my code
public class Dog
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime? BirthDate { get; set; }
public int? CreatedBy{ get; set; }
public DateTime? DateCreated { get; set; }
}
public class DogViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime? BirthDate { get; set; }
public static implicit operator DogViewModel(Dog dm)
{
var vm= new DogViewModel{
Id = dm.Id,
Name = dm.Name,
BirthDate = dm.BirthDate
};
return vm;
}
public static implicit operator Dog(DogViewModel vm)
{
var dm = new Dog
{
Id = vm.Id,
Name = vm.Name,
BirthDate = vm.BirthDate
};
return dm;
}
}
Question: Is there a way to use a common mapping method in my ViewModel class or some other way to so that I don't have to duplicate code for copying properties?