2016-05-02 71 views
0

我正在爲我的DAL使用實體框架並希望將實體對象轉換爲業務對象,反之亦然。這發生在我的BLL項目中。我希望能夠在我的BLL項目中設置automapper ... let say CustomerFs auto由EF生成並將其轉換爲CustomerWithDifferentDetail.cs(我的業務對象)Automapper配置設置

我試圖在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。如果映射,它應該給我帳戶= 1234和總數= 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回報正是我所期望的。

+0

您可以在創建後把.Reverse()地圖。不需要做第二條「反轉」線。 –

+0

謝謝。我知道這一點。我想知道@詹姆斯和埃裏克是否指的是作爲映射不止一次...... – NKD

+0

只要注意在使用.ForMember和.Reverse時正確映射特定成員。 –

回答

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); 
+0

謝謝你的建議。我有個問題。如何訪問「config」以從CustomerService.cs中執行config.Map (source)?你是說在我的CustomerService.cs類中,我需要創建一個配置文件的實例嗎?至於第二個建議。我喜歡它看起來超級乾淨整潔,簡單地做一個PersonBusinessObject.MapFrom(data),但映射會分散在我的每個業務對象中。我有一噸obj地圖。你有什麼建議將他們分組在一個地方,類似於做個人資料嗎? – NKD

+0

AutoMapper的作者不推薦多次創建地圖,這是一個昂貴的過程。而是,靜態地存儲你的'IMapper'實例(由MapperConfiguration.CreateMapper()'創建),或者使用依賴注入,如下所示:http://stackoverflow.com/questions/36619635/when-migrating-to-automapper-4 -2-5-0-should-i-store-an-imapper-instance-or-the/36691581#36691581 – James

+0

正如@James所說,我的代碼只使用靜態構造函數創建一次映射。我猜你可以爲你的CustomerService對象做同樣的事情。 –