我正在尝试将请求对象映射到域,我可以阅读英语,但是我绝对不能在自动映射器文档中找到解决方案。
问题:我如何基于ContainedEvent对象中的EventType属性将ContainedEvent对象从源映射到目标对象中的派生类,因为Event是抽象类。因此,假设源对象中的EventType == 1,则应将Event属性转换为其派生类之一。我也不想映射null属性,但是我已经处理了。
这是请求对象
public class CreatePostRequest
{
public long EventTime { get; set; }
public List<IFormFile>? Pictures { get; set; }
public ContainedEvent Event { get; set; }
public virtual List<string>? Tags { get; set; }
}
public class ContainedEvent
{
public string Description { get; set; }
#nullable enable
public string? Requirements { get; set; }
public int? Slots { get; set; }
public double? EntrancePrice { get; set; }
public int EventType { get; set; }
}
这是域对象
public class Post
{
public int Id { get; set; }
public DateTime EventTime { get; set; }
public Location Location { get; set; }
public int LocationId { get; set; }
public AppUser User { get; set; }
public string UserId { get; set; }
public Event Event { get; set; }
public int EventId { get; set; }
#nullable enable
public IEnumerable<string>? Pictures { get; set; }
#nullable enable
public virtual List<PostTags>? Tags { get; set; }
}
public abstract class Event
{
public int Id { get; set; }
public string Description{ get; set; }
public string? Requirements { get; set; }
public Post? Post { get; set; }
}
这就是我所坚持的。.
public class RequestToDomainProfile : Profile
{
public RequestToDomainProfile()
{
CreateMap<CreatePostRequest, Post>()
.ForAllMembers(opt => opt.Condition((src, dest, srcMember) => srcMember != null));
}
}