[英]Automapper Composite DTO to ViewModel conversion
In some cases there is a need to return composite DTOs from our repository, where the DTO just has a few properties that are Model properties and the function of the DTO is just to be a simple composite object (returning a Queryable is not enough because there is more information than T) 在某些情况下,有必要从我们的存储库中返回复合DTO,其中DTO仅具有一些属性,这些属性是Model属性,而DTO的功能仅仅是一个简单的复合对象(仅返回Queryable是不够的,因为比T多的信息)
For example: 例如:
Model: 模型:
public class Job
{
int Id { get; set; }
//more properties
}
public class JobApplication
{
int Id { get; set; }
//more properties
}
Repository: IQueryable<JobAndUserApplication> GetJobAndMatchingUserApplication(int userId)
: 存储库: IQueryable<JobAndUserApplication> GetJobAndMatchingUserApplication(int userId)
:
public class JobAndUserApplication
{
public Job Job { get; set; }
public JobApplication JobApplication { get; set; }
}
Now - Id like to simply do (Project and To are Automapper functionality): 现在-我想简单地做一下(Project和To是Automapper功能):
//this allows one efficient query to bring in the subproperties of the composite DTO
var jobVmList = jobRepository.GetAllJobsAndMatchingJobApplicationByUser(userId)
.Project()
.To<JobVM>()
.ToList();
So I need a mapping kind of like this: 所以我需要这样的映射:
Mapper.CreateMap<JobAndUserApplication, JobVM>()
.ForMember(jvm => jvm, opt => opt.ResolveUsing(src => src.Job));
//many other .ForMembers that are not relevant right now
I am attempting to map the Job property of the DTO directly on to the JobVM (which shares many of the same properties). 我试图将DTO的Job属性直接映射到JobVM(共享许多相同的属性)。
My mapping throws the following exception: 我的映射抛出以下异常:
Custom configuration for members is only supported for top-level individual members on a type. 类型的顶级单个成员仅支持成员的自定义配置。
What am I doing wrong and how can I accomplish the mapping form the Job property of the DTO on the the JobVM itself? 我在做什么错,如何在JobVM本身上完成DTO的Job属性的映射?
Thanks 谢谢
Automapper is telling you that you can only define custom actions on a member (property) of a class, not on the class itself. Automapper告诉您只能在类的成员(属性)上定义自定义操作,而不能在类本身上定义自定义操作。 What you need to do is first create a Job to JobVM map: 您需要做的是首先创建一个Job to JobVM映射:
Mapper.CreateMap<Job, JobVM>()
and 和
Mapper.CreateMap<JobAndUserApplication, JobVM>()
being sure to ignore and set any duplicate properties across the two types. 确保忽略并设置这两种类型之间的所有重复属性。 Then run automapper twice, first from the child object: 然后运行automapper两次,首先从子对象开始:
var jobVM = Mapper.Map<Job, JobVM>(jobAndUserApplication.job);
then from the parent object 然后从父对象
Mapper.Map<JobAndUserApplication, JobVM>(jobAndUserApplication, jobVM );
Or the other way around, depending on how your properties are laid out. 或反之亦然,这取决于属性的布局方式。
Quick side note: I have a feeling you might be mixing concerns, and my code smell alarm is going off. 快速旁注:我觉得您可能会担心一些问题,并且我的代码异味警报正在关闭。 I'd take a second look at either your viewmodel or domain model, as this is not a typical issue I see come up. 我将再次查看您的视图模型或域模型,因为这不是我看到的典型问题。 (just my $0.02 :-) (只是我的$ 0.02 :-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.