[英]How to do these mappings by using valueInjecter instead of AutoMapper
I have used Automapper in one of my project before, now I want to use ValueInjecter for the other one. 我以前在一个项目中使用过Automapper,现在我想在另一个项目中使用ValueInjecter。
These are ViewModels 这些是ViewModel
public class ApplicantListViewModel : BaseViewModel
{
public int Id {get; set;}
public string Name {get;set;}
public string CourseName {get;set;}
}
public class CourseListViewModel : BaseViewModel
{
public int Id {get; set;}
public string Name {get;set;}
public int Applicants {get;set;}
}
These are my DTOs 这些是我的DTO
public class Course : BaseEntity
{
public string Name { get; set; }
public virtual IList<Applicant> Applicants { get; set; }
}
public class Applicant : BaseEntity
{
public string Name { get; set; }
public int CourseId { get; set; }
public virtual Course Course { get; set; }
}
I can map DTOs to ViewModels sth like below with Automapper 我可以使用Automapper将DTO映射到ViewModels,如下所示
AutoMapper.Mapper.CreateMap<Applicant, ApplicantListViewModel>();
AutoMapper.Mapper.CreateMap<Course, CourseListViewModel>()
.ForMember(s => s.Applicants, d => d.MapFrom(s => s.Applicants.Count));
Then it is automatically map Course to CourseListViewModel and Applicant to ApplicantListViewModel. 然后,它将自动将课程映射到CourseListViewModel,并将申请人映射到ApplicantListViewModel。 When it maps I can see that ApplicantListViewModel's CourseName' property gets its value from Course.Name without any special configuration by using AutoMapper but ValueInjector doesnt map(CourseName become null) it.
当它映射时,我可以看到ApplicantListViewModel的CourseName属性通过使用AutoMapper从Course.Name获取其值,而无需任何特殊配置,但ValueInjector不会映射它(CourseName变为null)。 Also CourseListViewModel's Applicants property gets its value from Applicants.Count with little configuration.
CourseListViewModel的Applicants属性也可以通过很少的配置从Applicants.Count获取其值。
How to do these mappings by ValueInjecter easier ? 如何更轻松地通过ValueInjecter进行这些映射?
Take a look at flattening , unflattening 看看压扁 , unflattening
Here a example of your code: 这是您的代码示例:
[Test]
public void Mapper()
{
var course = new Course { Name = "just a simple name" };
var applicantList = new List<Applicant>()
{
new Applicant {Course = course, CourseId = 1, Name = "Applicant Course 1"},
new Applicant {Course = course, CourseId = 2, Name = "Applicant Course 2"}
};
course.Applicants = applicantList;
var courseView = new CourseListViewModel();
courseView.InjectFrom<FlatLoopValueInjection>(course);
//just set other props here, like you did with AutoMapper.
courseView.Applicants = course.Applicants.Count;
var applicantViewList = applicantList.Select(s =>
{
var applicantView = new ApplicantListViewModel();
applicantView.InjectFrom<FlatLoopValueInjection>(s);
return applicantView;
}).ToList();
Assert.AreEqual(course.Name, courseView.Name);
Assert.AreEqual(course.Applicants.Count, courseView.Applicants);
Assert.AreEqual(applicantList[0].Name, applicantViewList[0].Name);
Assert.AreEqual(applicantList[0].Course.Name, applicantViewList[0].CourseName);
}
Hope it Helps! 希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.