我有以下无法正确映射的情况:
我从对 camunda api 的调用中收到以下对象的列表。
public class CamundaTask
{
public string FormKey { get; set; }
public string Id { get; set; }
public string Name { get; set; }
...
}
我想根据表单键将它映射到我的代码(ContactUpdateTask
或)中的特定任务。OrganisationAquisitionTask
以下是我的架构:
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 让我有点失望。这是我到目前为止所拥有的,但它会产生以下错误:“映射器未初始化。使用适当的配置调用初始化。”
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>
进行映射的正确方法是什么,以便我的任务列表包含ContactUpdateTask
或OrganisationAquisitionTask
取决于 formkey 的对象CamundaTask
?