简体   繁体   中英

How to map a somewhat complex architecture with automapper and constructUsing

I have the following situation which I cannot get mapped properly:

I receive a list of following objects from a call to the camunda api.

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

And I would like to map it to specific tasks in my code ( ContactUpdateTask or OrganisationAquisitionTask ) depending on the formkey.

Following is my architecture:

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

I know how to deal with simple inheritance and automapper but the whole TContext throws me off a little. This is what I have so far but it produces following error: "Mapper not initialized. Call Initialize with appropriate configuration."

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

I map with the following line of code:

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

Where camundaTasks is of type IEnumerable<CamundaTask>

What is the proper way to do the mapping so that my tasks list contains objects of ContactUpdateTask or OrganisationAquisitionTask depending on the formkey of the CamundaTask ?

So what happens here is that you initialize Automapper in a non-static way (isn't clear from your original post, but I could deduct that because my comment seemed to have solved your problem), but you are using Automapper statically within the ConstructUsing method.

There's two solutions for this:

1) Use Automapper statically

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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