I have a class at my Domain layer something as below:
public class Employee : AuditableEntity { public string FirstName { get; set; } public string Surname { get; set; } public double Salary{ get; set; } public int PhoneNo { get; set; } public double Bonus { get { return Salary + EmployeeAdditionals.Sum(e => e.Value); } } // virtual allow lazy loading public virtual ReadOnlyCollection<EmployeeAdditional> EmployeeAdditionals { get; private set; } // Paramterless constructor for EF public Employee() { EmployeeAdditionals = new List<EmployeeAdditional>(); } public void AddAdditionalInfo(EmployeeAdditional additionalInfo) { additionalInfo.Employee = this; additionalInfo.EmployeeId = Id; ((Collection<EmployeeAdditional>) EmployeeAdditionals).Add(additionalInfo); } I then have the following viewModel class in my MVC 5 Application:
public class EmployeeDetailsViewModel : BaseViewModel { public EmployeeDetailsViewModel() { Employee = new Employee(); } public Employee Employee { get; set; } //Other fields } public class Employee { [DisplayName("Employee First Name")] public string FirstName { get; set; } [DisplayName("Employee Surname")] public string Surname{ get; set; } public double Salary{ get; set; } [RegEx for Phone Number and Error Mesage] public int PhoneNo{ get; set; } public double Bonus { get; set; } } I have mapping then in my controllers and viewModel builder to map my viewmodel to my domain model and domain model back to view model and I am attempting to use AutoMapper for this.
Using manual mapping something like below:
// set domain from viewModel employee.FirstName= model.Employee.FirstName; //etc for rest of fields // set view model from domain viewModel.Employee.FirstName = employee.FirstName; //etc for rest of fields Now in my AutoMapperBootstrapper class in Setup method I have the following:
Mapper.CreateMap<Domain.Models.Employee, Employee>(); Mapper.CreateMap<Employee, Domain.Models.Employee>(); Then I am using this is following in my controller and viewmodel builder:
//set domain from viewModel Mapper.Map(employee, model.Employee); // set viewmodel from domain Mapper.Map(viewModel.Employee, employee); When I hit a breakpoint on mapping from domain to viewModel I see the model.Employee with the values from screen but when I step over this line the values do not get mapped to domain employee and then the model.Emplyee values are reset to null for the strings, 0.0 for double, etc.
Is there something incorrect with my configuration or use of automapper?
Mapper.AssertConfigurationIsValid();)Mapper.Map(employee, viewModel.Employee).ForMember(e => e.CreateDate, x => x.Ignore());