简体   繁体   English

AutoMapper映射接口和忽略列

[英]AutoMapper mapping interface and ignoring column

I have the following code: 我有以下代码:

Interfaces 接口

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

Objects 对象

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

My goal is to use Automapper to copy over the common properties from between the two versions of LoanApplication. 我的目标是使用Automapper从两个版本的LoanApplication之间复制公共属性。 I have the following working: 我有以下工作:

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

This copies all the columns over correctly, but I still have to manually update the ignored properties. 这会正确复制所有列,但我仍然需要手动更新忽略的属性。

The types for ID are different, so I always want to ignore. ID的类型不同,所以我总是想忽略。 But wanted to know is how I can also map LoanApplicationStatus, BusinessBorrowers and LoanApplicationDebts. 但想知道我是如何映射LoanApplicationStatus,BusinessBorrowers和LoanApplicationDebts的。 I didn't post those definitions to cut down on space, but just like LoanApplicaiton, the App1 version uses Guid and App2 uses Int for Ids. 我没有发布这些定义以减少空间,但就像LoanApplicaiton一样,App1版本使用Guid而App2则使用Int for Ids。 Each version share the same base class, but have a few different columns added. 每个版本共享相同的基类,但添加了几个不同的列。

I figured it out, I needed to add additional mappings per object: 我想通了,我需要为每个对象添加额外的映射:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM