2017-02-11 153 views
1

我有以下代碼:AutoMapper映射接口,而忽略列

接口

namespace Core.Interfaces 
{ 
    public interface ILoanApplicationBase 
    { 
     string ContactName { get; set; } 
     string Email { get; set; } 
    } 
} 

namespace App1.Core.Interfaces 
{ 
    public interface ILoanApplication : ILoanApplicationBase 
    { 
     Guid? Id { get; set; } 

     List<ILoanApplicationDebt> LoanApplicationDebts { get; set; } 

     ILoanApplicationStatus LoanApplicationStatus { get; set; } 

     IReadOnlyCollection<IBusinessBorrower> BusinessBorrowers { get; set; } 
    } 
} 

namespace App2.Core.Interfaces 
{ 
    public interface ILoanApplication : IDomainModel, ILoanApplicationBase 
    { 
     int? Id { get; set; } 

     IReadOnlyCollection<ILoanApplicationDebt> LoanApplicationDebts { get; set; } 

     ILoanApplicationStatus LoanApplicationStatus { get; set; } 

     IReadOnlyCollection<IBusinessBorrower> BusinessBorrowers { get; set; } 
    } 
} 

對象

namespace App1.Domain 
{ 
    [Serializable] 
    public class LoanApplication : ILoanApplication 
    { 
     public Guid? Id { get; set; } 

     public List<ILoanApplicationDebt> LoanApplicationDebts { get; set; } 

     public LoanApplicationStatus LoanApplicationStatus { get; set; } 

     public IReadOnlyCollection<IBusinessBorrower> BusinessBorrowers { get; set; } 

    } 
} 

namespace App2.Domain 
{ 
    [Serializable] 
    public class LoanApplication : ILoanApplication 
    { 
     public override int? Id { get; set; } 

     public int? LoanApplicationStatusId { get; set; } 

     public virtual LoanApplicationStatus LoanApplicationStatus { get; set; } 

     ILoanApplicationStatus ILoanApplication.LoanApplicationStatus 
     { 
      get 
      { 
       return (ILoanApplicationStatus)LoanApplicationStatus; 
      } 
      set 
      { 
       LoanApplicationStatus = (LoanApplicationStatus)value; 
      } 
     } 

     public virtual ICollection<LoanApplicationDebt> LoanApplicationDebts { get; set; } 

     IReadOnlyCollection<ILoanApplicationDebt> ILoanApplication.LoanApplicationDebts 
     { 
      get 
      { 
       List<ILoanApplicationDebt> loanApplicationDebts = new List<ILoanApplicationDebt>(); 
       foreach (ILoanApplicationDebt loanApplicationDebt in this.LoanApplicationDebts) 
       { 
        loanApplicationDebts.Add(loanApplicationDebt); 
       } 
       return loanApplicationDebts; 
      } 
      set 
      { 
       foreach (var item in value) 
       { 
        this.LoanApplicationDebts.Add((LoanApplicationDebt)item); 
       } 
      } 
     } 

     public ICollection<BusinessBorrower> BusinessBorrowers { get; set; } 

     IReadOnlyCollection<IBusinessBorrower> ILoanApplication.BusinessBorrowers 
     { 
      get 
      { 
       List<IBusinessBorrower> businessBorrowers = new List<IBusinessBorrower>(); 
       foreach(BusinessBorrower businessBorrower in BusinessBorrowers) 
       { 
        businessBorrowers.Add((IBusinessBorrower)businessBorrower); 
       } 
       return new ReadOnlyCollection<IBusinessBorrower>(businessBorrowers); 

      } 
      set 
      { 

       foreach (IBusinessBorrower businessBorrower in value) 
       { 
        BusinessBorrowers.Add((BusinessBorrower)businessBorrower); 
       } 
      } 
     } 
    } 
} 

我的目標是使用Automapper在共同複製LoanApplication的兩個版本之間的屬性。我有以下工作:

Mapper.Initialize(cfg => cfg.CreateMap<App1.Domain.LoanApplication, App2.Domain.LoanApplication>() 
    .ForMember(x => x.Id, opt => opt.Ignore()) 
    .ForMember(x => x.LoanApplicationStatus, opt => opt.Ignore()) 
    .ForMember(x => x.BusinessBorrowers, opt => opt.Ignore()) 
    .ForMember(x => x.LoanApplicationDebts, opt => opt.Ignore())); 

app2LoanApplication = Mapper.Map<LoanApplication>(app1LoanApplication); 

這將正確複製所有列,但我仍然必須手動更新忽略的屬性。

ID的類型是不同的,所以我總是想忽略。但想知道的是我也可以映射LoanApplicationStatus,BusinessBorrowers和LoanApplicationDebts。我沒有發佈這些定義來減少空間,但就像LoanApplicaiton一樣,App1版本使用Guid,而App2使用Int for Ids。每個版本共享相同的基類,但添加了一些不同的列。

回答

0

我想通了,我需要每個對象添加額外的映射:

Mapper.Initialize(cfg => 
{ 
    cfg.CreateMap<App1.Domain.LoanApplication, App2.Domain.LoanApplication>() 
     .ForMember(x => x.Id, opt => opt.Ignore()) 

    cfg.CreateMap<App1.Domain.BusinessBorrower, App2.Domain.BusinessBorrower>() 
     .ForMember(x => x.Id, opt => opt.Ignore()) 

    cfg.CreateMap<App1.Domain.LoanApplicationDebt, App2.Domain.LoanApplicationDebt>() 
     .ForMember(x => x.Id, opt => opt.Ignore()); 
});