AutoMapper无法按预期工作?

时间:2014-08-17 15:23:13

标签: c# asp.net asp.net-mvc automapper

我的Domain层有一个类如下:

 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);
        }

然后我在MVC 5应用程序中有以下viewModel类:

 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; }
    }

然后我在我的控制器和viewModel构建器中映射,将我的viewmodel映射到我的域模型和域模型回到视图模型,我试图使用AutoMapper。

使用下面的手动映射:

// 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

现在在Setup方法的AutoMapperBootstrapper类中,我有以下内容:

  Mapper.CreateMap<Domain.Models.Employee, Employee>();
  Mapper.CreateMap<Employee, Domain.Models.Employee>();

然后我在我的控制器和viewmodel构建器中使用它:

//set domain from viewModel
Mapper.Map(employee, model.Employee);

// set viewmodel from domain
Mapper.Map(viewModel.Employee, employee);

当我在从域到viewModel的映射中遇到断点时,我看到model.Employee带有来自屏幕的值,但是当我跨过这一行时,值不会映射到域员工,然后模型.Emplyee值被重置为字符串为null,为double为0.0,等等。

我的配置或使用automapper有什么不对吗?

2 个答案:

答案 0 :(得分:2)

您传递错误的参数。如果要使用现有实例,请确保第一个参数是源,第二个参数是目标。

尝试切换参数。

//set domain from viewModel (viewModel is the source)
Mapper.Map(model.Employee, employee);
              ^^^^^         ^^^^^
              source      destination 

// set viewmodel from domain (domain is the source)
Mapper.Map(employee, viewModel.Employee);
            ^^^^^           ^^^^^
            source      destination 

答案 1 :(得分:0)

你的地图代码实际上不应该是这样吗?您必须使用Map()的实例,看起来您正在使用Types

//set domain from viewModel
Mapper.Map(viewEmployee, new model.Employee());

// set viewmodel from domain
Mapper.Map( employee, new viewmodel.Employee());

我希望您使用它:

var viewModel = Mapper.Map<ViewModel.Employee>(domainModel);  <-- this creates a ViewModel for a Domain Model

反之亦然:

var domainModel  = Mapper.Map<Domain.Employee>(viewModel);