2017-02-11 3 views
1

Ich habe den folgenden Code:AutoMapper Mapping-Schnittstelle und ignorieren Spalte

Schnittstellen

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

Objekte

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

Mein Ziel ist es AutoMapper zu verwenden, um über die gemeinsame zu kopieren Eigenschaften zwischen den beiden Versionen von LoanApplication. Ich habe folgende Funktion:

Dies kopiert alle Spalten korrekt, aber ich muss immer noch manuell die ignorierten Eigenschaften aktualisieren.

Die Typen für ID sind unterschiedlich, also möchte ich immer ignorieren. Aber ich wollte wissen, wie ich auch LoanApplicationStatus, BusinessBrowners und LoanApplicationDebts abbilden kann. Ich habe diese Definitionen nicht veröffentlicht, um Speicherplatz zu sparen, aber genau wie bei LoanApplication, verwendet die App1-Version Guid und App2 verwendet Int für IDs. Jede Version hat dieselbe Basisklasse, aber einige verschiedene Spalten wurden hinzugefügt.

Antwort

0

ich es herausgefunden, ich brauchte zusätzliche Zuordnungen pro Objekt hinzuzufügen:

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