如何使用automapper和constructUsing映射一个有点复杂的架构

时间:2018-04-04 07:43:11

标签: c# automapper automapper-6

我遇到以下情况,我无法正确映射:

我通过调用camunda api收到以下对象的列表。

public class CamundaTask
{
    public string FormKey { get; set; }
    public string Id { get; set; }
    public string Name { get; set; }
    ...
}  

我想将其映射到我的代码(ContactUpdateTaskOrganisationAquisitionTask)中的特定任务,具体取决于formkey。

以下是我的架构:

public class BaseTask
{
    public virtual string TaskType { get; set; }
    public string Id { get; set; }
    public int Priority { get; set; }
    public string Key { get; set; }
    ...
}

public abstract class ProcessTask<TContext> : BaseTask
{
    public TContext TaskContext { get; set; }
}

public class ContactUpdateContext
{
    public Guid PersonId { get; set; }
    public string FullName { get; set; }
}

public class OrganisationAquisitionContext
{
    public Guid OrganisationId { get; set; }
    public string Name { get; set; }
}

public class ContactUpdateTask : ProcessTask<ContactUpdateContext>
{
    public override string TaskType { get => "UpdateContact"; }
}

public class OrganisationAquisitionTask : ProcessTask<OrganisationAquisitionContext>
{
    public override string TaskType { get => "OrganisationAquisition"; } 
}

我知道如何处理简单的继承和自动化,但是整个TContext让我失望了。 这是我到目前为止,但它产生以下错误:&#34;映射器未初始化。使用适当的配置调用Initialize。&#34;

CreateMap<ContactUpdateTask, ProcessTask<ContactUpdateContext>>().ReverseMap();
CreateMap<ContactValidationTask, ProcessTask<OrganisationAquisitionTask>>().ReverseMap();
CreateMap<OrganisationAquisitionTask, ProcessTask<ContactValidationTask>>().ReverseMap();

CreateMap<BaseTask, CamundaTask>()
   .ForMember(t => t.FormKey, opt => opt.MapFrom(p => p.TaskType))
   .ForMember(t => t.Assignee, opt => opt.MapFrom(p => p.Owner))
   .ForMember(t => t.Id, opt => opt.MapFrom(p => p.Key))
   .ForAllOtherMembers(opt => opt.Ignore());

CreateMap<CamundaTask, ContactUpdateTask>()
    .ForMember(t => t.TaskType, opt => opt.MapFrom(p => p.FormKey))
    .ForMember(t => t.Owner, opt => opt.MapFrom(p => p.Assignee))
    .ForMember(t => t.Key, opt => opt.MapFrom(p => p.Id))
    .ForAllOtherMembers(opt => opt.Ignore());

CreateMap<CamundaTask, ContactValidationTask>()
    .ForMember(t => t.TaskType, opt => opt.MapFrom(p => p.FormKey))
    .ForMember(t => t.Owner, opt => opt.MapFrom(p => p.Assignee))
    .ForMember(t => t.Key, opt => opt.MapFrom(p => p.Id))
    .ForAllOtherMembers(opt => opt.Ignore());

CreateMap<CamundaTask, OrganisationAquisitionTask>()
    .ForMember(t => t.TaskType, opt => opt.MapFrom(p => p.FormKey))
    .ForMember(t => t.Owner, opt => opt.MapFrom(p => p.Assignee))
    .ForMember(t => t.Key, opt => opt.MapFrom(p => p.Id))
    .ForAllOtherMembers(opt => opt.Ignore());

CreateMap<CamundaTask, BaseTask>()
    .ConstructUsing((CamundaTask task) =>
    {
        switch (task.FormKey.ToLower())
        {
            case "updateorganization":
                return Mapper.Map<ContactUpdateTask>(task);
            case "contactValidation":
                return Mapper.Map<ContactValidationTask>(task);
            case "organizationacquisition":
                return Mapper.Map<OrganisationAquisitionTask>(task);       
        }

        return Mapper.Map<BaseTask>(task);
    })
   .ForMember(t => t.TaskType, opt => opt.MapFrom(p => p.FormKey))
   .ForMember(t => t.Owner, opt => opt.MapFrom(p => p.Assignee))
   .ForMember(t => t.Key, opt => opt.MapFrom(p => p.Id))
   .ForAllOtherMembers(opt => opt.Ignore());

我用以下代码行映射:

var tasks = _mapper.Map<IEnumerable<BaseTask>>(camundaTasks)

camundaTasks的类型为IEnumerable<CamundaTask>

执行映射的正确方法是什么,以便我的任务列表包含ContactUpdateTaskOrganisationAquisitionTask的对象,具体取决于CamundaTask的formkey?

1 个答案:

答案 0 :(得分:2)

所以这里发生的是你以非静态的方式初始化Automapper(原帖不明确,但我可以扣除,因为我的评论似乎已经解决了你的问题),但是你静态地使用Automapper在ConstructUsing方法中。

有两个解决方案:

1)静态使用Automapper

Mapper.Initialize(cfg=> cfg.CreateMap<CamundaTask, BaseTask>());

2)使用ResolutionContext

.ConstructUsing((CamundaTask task, ResolutionContext context) =>
{
    switch (task.FormKey.ToLower())
    {
        case "updateorganization":
            return context.Mapper.Map<ContactUpdateTask>(task);
        case "contactValidation":
            return context.Mapper.Map<ContactValidationTask>(task);
        case "organizationacquisition":
            return context.Mapper.Map<OrganisationAquisitionTask>(task);       
    }

    return context.Mapper.Map<BaseTask>(task);
})