自动配置设置

时间:2016-05-02 19:24:49

标签: entity-framework automapper

我正在为我的DAL使用实体框架,并希望将实体对象转换为业务对象,反之亦然。这发生在我的BLL项目中。我希望在我的BLL项目中设置automapper ...让我们说自动生成的Customer.cs并将其转换为CustomerWithDifferentDetail.cs(我的业务对象)

我尝试使用以下代码在BLL项目下创建AutoMapperBLLConfig.cs:

 public static void Configure()
    {
        Mapper.Initialize(cfg =>
        {
            cfg.AddProfile(new CustomerProfile());
        });
    }


public class CustomerProfile : Profile
{
    protected override void Configure()
    {
        var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<Customer, CustomerWithDifferentDetail>();
            cfg.CreateMap<CustomerWithDifferentDetail, Customer>();
        });
    }
}

然后我使用以下代码在BLL项目下创建了CustermerService.cs以测试它是否正常工作:

 public void CustomerToCustomerWithDifferentDetail()
 {
     AutoMapperBLLConfiguration.Configure();
     Customer source = new Customer
        {
            Account = 1234,
            Purchase_Quantity = 100,
            Date = "05/05/2016",
            Total = 500
        };

     Models.CustomerWithDifferentDetail testCustomerDTO = Mapper.Map<Customer, Models.CustomerWithDifferentDetail>(source)
 }

我收到此错误: 缺少类型映射配置或不支持的映射。

我不确定我做错了什么。我没有start_up或global.aspx。这是一个类库。我不确定我错过了什么或做错了什么。

我有一个单独的项目调用模型,它包含所有业务对象,包括CustomerWithDifferentDetail.cs。在这种情况下,CustomerWithDifferentDetail只有两个属性:Account和Total。如果映射,它应该给我Account = 1234和Total = 500 - 基本上与实体对象的数据相同,只是形状不同。

=======================更新======================= ========== AutoMapperBLLConfig.cs - 保持与上面提到的相同

CustomerProfile.cs

public class CustomerProfile : Profile
{
    protected override void Configure()
    {
        var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<Customer, CustomerWithDifferentDetail>().ReverseMap(); //cut it down to one line with ReverseMap
        });
    }
    CreateMap<Customer, CustomerWithDifferentDetail>().ReverseMap(); //missed this one line before; hence, the error
}

CustomerService.cs

static CustomerService()
    {
        AutoMapperBLLConfiguration.Configure(); //per @Erik Philips suggestion, move this call to a static constructor 
    }

 public void CustomerToCustomerWithDifferentDetail()
 {
     Customer source = new Customer
        {
            Account = 1234,
            Purchase_Quantity = 100,
            Date = "05/05/2016",
            Total = 500
        };

     Models.CustomerWithDifferentDetail testCustomerDTO = Mapper.Map<Customer, Models.CustomerWithDifferentDetail>(source);
 }

结果:我的testCustomerDTO完全返回我的预期。

1 个答案:

答案 0 :(得分:0)

由于您使用的是AutoMapper的实例方法:

var config = new MapperConfiguration(cfg =>
{
  cfg.CreateMap<Customer, CustomerWithDifferentDetail>();
  cfg.CreateMap<CustomerWithDifferentDetail, Customer>();
});

然后你需要使用实例进行映射:

Models.CustomerWithDifferentDetail testCustomerDTO = 
  config.Map<Customer, Models.CustomerWithDifferentDetail>(source)

我个人在我的应用程序中并没有真正想到这一点(I need to move to the instance method instead of the static method)。 (Migrating from status API)。

关闭袖口,根据您的代码,我可能会做类似的事情:

public class PersonDataObject
{
  public string Name { get; set; }
}

public class PersonBusinessObject
{
  private readonly MapperConfiguration _mapper;

  public string Name { get; set; }

  PersonBusinessObject()
  {
    _mapper = new MapperConfiguration(cfg =>
    {
      cfg.CreateMap<PersonDataObject,PersonBusinessObject>();
    });
  }

  public static PersonBusinessObject MapFrom(PersonDataObject data)
  {
    return _mapper.Map<PersonBusinessObject>(data);
  }
}

然后你可以简单地说:

PersonDataObject data = new PersonDataObject();

PersonBusinessObject business = PersonBusinessObject.MapFrom(data);