简体   繁体   English

如何为复杂的类正确定义AutoMapper配置?

[英]How to properly define AutoMapper config for complex class?

I'm working with Entity Framework, and getting ready to redo a project that is currently using EF->BDO->DTO->client with manual conversions for each class along the way. 我正在使用Entity Framework,并准备重做一个当前正在使用EF-> BDO-> DTO-> client的项目,并在此过程中为每个类进行手动转换。

I came across AutoMapper and think this would be a better solution overall. 我遇到了AutoMapper,并认为总体上这将是一个更好的解决方案。 I have had no issues mapping CustomerEF -> CustomerDto, OfficeEF -> OfficeDto, etc, but I am now working on a more complex class, and struggling to get everything in place. 我在映射CustomerEF-> CustomerDto,OfficeEF-> OfficeDto等方面没有任何问题,但是我现在正在研究一个更复杂的类,并且努力使所有内容都准备就绪。

I feel I am close, and that something has to happen in reverse, but have not been able to identify what I'm missing. 我感到自己很亲密,某些事情必须反向进行,但无法识别我所缺少的东西。

    public class CaseDto
{
    public int CaseId { get; set; }
    public string CaseReason { get; set; }
    public CustomersDto _customer { get; set; }
    public OfficeDto _office { get; set; }
    public CaseNotesDto[] _caseNotes { get; set; }
}

public class CustomersDto
{
    public int CustomerId { get; set; }
    public string CustomerName { get; set; }
}

public class OfficeDto
{
    public int OfficeId { get; set; }
    public string OfficeName { get; set; }
}

public class CaseNotesDto
{
    public int CaseNotesId { get; set; }
    public int CaseId { get; set; }
    public string CaseNote { get; set; }
}

// EF objects

public class CaseEF
{
    public int CaseId { get; set; }
    public string CaseReason { get; set; }
    public int CustomerId { get; set; }
    public int OfficeId { get; set; }
}

public class CustomerEF
{
    public int CustomerId { get; set; }
    public string CustomerName { get; set; }
}

public class OfficeEF
{
    public int OfficeId { get; set; }
    public string OfficeName { get; set; }
}

public class CaseNotesEF
{
    public int CaseNotesId { get; set; }
    public int CaseId { get; set; }
    public string CaseNote { get; set; }
}

// execution classes

public class CaseFramework
{
    // set 'ef' variables
    private readonly OfficeEF _officeEf = new OfficeEF { OfficeId = 1, OfficeName = "Washington" };
    private readonly CustomerEF _customerEf = new CustomerEF { CustomerId = 1, CustomerName = "Blips and Chitz" };

    private readonly CaseNotesEF[] _caseNotesEf = 
    {
        new CaseNotesEF {CaseNotesId = 1, CaseNote = "Case 1", CaseId = 1000},
        new CaseNotesEF {CaseNotesId = 2, CaseNote = "Case 2", CaseId = 1000}
    };

    private readonly CaseEF _case =
        new CaseEF { CaseId = 1000, CaseReason = "Roy is back!", CustomerId = 1, OfficeId = 1 };

    public CaseDto GetCase(int caseId)
    {
        Mapper.Initialize(cfg =>
        {
            cfg.CreateMap<OfficeEF, OfficeDto>();
            cfg.CreateMap<CustomerEF, CustomersDto>();
            cfg.CreateMap<CaseNotesEF, CaseNotesDto>();
            cfg.CreateMap<CaseEF, CaseDto>()
                .ForMember(dest => dest._customer, opt => opt.MapFrom(src => src.CustomerId))
                .ForMember(dest => dest._office, opt => opt.MapFrom(src => src.OfficeId))
                .ForMember(dest => dest._caseNotes,
                    opt => opt.MapFrom(src => _caseNotesEf.Where(x => x.CaseId == caseId)));
        });

        Mapper.AssertConfigurationIsValid();

        var source = new CaseEF { CaseId = caseId };
        var destination = Mapper.Map<CaseEF, CaseDto>(source);


        return destination;
    }
}

To run this I am doing: 为了运行这个,我正在做:

var b = new CaseFramework();
var result = b.GetCase(1000);

The results are populating the CaseId (set manually) and the CaseNotesDto, but nothing else. 结果将填充CaseId(手动设置)和CaseNotesDto,但仅此而已。

Having the first 3 cfg.CreateMap items in the Mapper.Initialize section makes no difference if they are there or not. 在Mapper.Initialize部分中具有前3个cfg.CreateMap项无论是否存在都没有区别。

Thanks in advance for any guidance. 在此先感谢您的指导。

Appreciate the responses. 赞赏答复。

@MickyD, I was a little confused on the POCO comment, as the EF already generated a tt, and the Canonical schema/model looks like something I'll have to research more. @MickyD,我对POCO的评论有些困惑,因为EF已经生成了tt,并且规范模式/模型看起来像是我需要进一步研究的东西。

@Steve I checked my navigation properties from my EF database and we need to do some fixes to the relationships before making the Include method work, but I think that will ultimately be the solution. @Steve我从EF数据库中检查了我的导航属性,在使Include方法起作用之前,我们需要对关系进行一些修复,但是我认为最终将是解决方案。 In the meantime I was able to accomplish the original goal with this: 在此期间,我能够达到以下目标:

            Mapper.AssertConfigurationIsValid();

            var destination = Mapper.Map<CaseDto>(_case);
            destination.Customers = Mapper.Map<CustomerEF, CustomersDto>(_customerEf);
            destination.Office = Mapper.Map<OfficeEF, OfficeDto>(_officeEf);

            return destination;

Thanks. 谢谢。

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

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