我正在使用 AutoMapper 映射两个对象,但它不是映射int
.
这是我正在做的事情:
Public class A
{
public int a{get;set;}
public string b{get;set;}
public List<int> ints{get;set;}
}
Public class B
{
public int a{get;set;}
public string b{get;set;}
public List<int> ints{get;set;}
}
public class MappingService
{
public object MapBToA(B b)
{
Mapper.CreateMap<B,A>();
return Mapper.Map<B,A>(b);
}
}
当我们调用映射函数时,它返回一个类型,object
但 ints 始终为空。
已编辑
真实课程的问题
public class SearchRawInput
{
public SearchRawInput()
{
PageNo = 1;
}
public string City { get; set; }
public string Segment1 { get; set; }
public string Segment2 { get; set; }
public TargetAgeGroup? TargetAge { get; set; }
public List<int> CategoryIds { get; set; }
public List<int> SubCategoryIds { get; set; }
public List<int> LocationIds { get; set; }
public List<int> LocationGroupIds { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
public decimal? StartPrice { get; set; }
public decimal? EndPrice { get; set; }
public string SearchQuery { get; set; }
public int? PageNo { get; set; }
}
public class ClassSearchInputViewModel
{
public ClassSearchInputViewModel()
{
PageSize = 12;
}
public UrlSeoProcessingResult UrlSeoProcessingResult { get; set; }
public TargetAgeGroup TargetAge { get; set; }
public List<int> CategoryIds { get; set; }
public List<int> SubCategoryIds { get; set; }
public List<int> LocationIds { get; set; }
public List<int> LocationGroupIds { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
public decimal? StartPrice { get; set; }
public decimal? EndPrice { get; set; }
public ClassDifficultyLevel DifficultyLevel { get; set; }
public string SearchQuery { get; set; }
public int PageNo { get; set; }
public int PageSize { get; set; }
}
public class SegmentToUrlSeoProcessingResultResolver : ValueResolver<SearchRawInput, UrlSeoProcessingResult>
{
protected override UrlSeoProcessingResult ResolveCore(SearchRawInput source)
{
var processor =new UrlProcessor();
if (source.Segment1 != null && source.Segment2 != null)
{
return processor.Process( (source.Segment1.Trim() + "/" + source.Segment2.Trim()).Trim(), source.City);
}
else if (source.Segment1 == null && source.Segment2 == null)
{
return processor.Process("", source.City);
}
else
{
if (source.Segment1 != null)
{
return processor.Process( (source.Segment1.Trim()).Trim(),source.City);
}
else
{
return processor.Process( source.Segment2.Trim(),source.City);
}
}
}
}
public ClassSearchInputViewModel ClassSearchRawInputToClassSearchInput(SearchRawInput classSearchRawInput)
{
var classSearchInput = Mapper.Map<SearchRawInput, ClassSearchInputViewModel>(classSearchRawInput);
return classSearchInput;
}
Mapper.CreateMap<SearchRawInput,ClassSearchInputViewModel>()
.ForMember(dest => dest.UrlSeoProcessingResult, opt => opt.ResolveUsing<SegmentToUrlSeoProcessingResultResolver>());
以上是完整的代码,当我调用 ClassSearchRawInputToClassSearchInputmethod 时,它将返回 ClassSearchInputViewModel 的对象,但它返回的 locationids 为 0 而不是项目
这是我的单元测试
[Isolated]
[TestMethod]
public void ClassSearchRawInputToClassSearchInputViewModel_Test()
{
var urlProcessor = new SegmentToUrlSeoProcessingResultResolver();
AutoMapperConfiguration.Configure();
IMapperService mapper = new MapperService();
int[] genericStringArray = {1, 2, 3};
var searchRawInput = new SearchRawInput()
{
City = "Mumbai",
Segment1 = "Dance",
Segment2 = "Salsa",
CategoryIds = genericStringArray.ToList(),
StartDate = DateTime.Now,
EndDate = DateTime.Now,
LocationGroupIds = genericStringArray.ToList(),
LocationIds = genericStringArray.ToList(),
StartPrice = genericStringArray[0],
EndPrice = 2132123,
SearchQuery = "Search Query",
SubCategoryIds = genericStringArray.ToList(),
TargetAge = TargetAgeGroup.Kids
};
Isolate.NonPublic.WhenCalled(urlProcessor, "ResolveCore", new SearchRawInput()
{
City = "Mumbai",
Segment1 = "Dance",
Segment2 = "Salsa",
CategoryIds = genericStringArray.ToList(),
StartDate = DateTime.Now,
EndDate = DateTime.Now,
LocationGroupIds = genericStringArray.ToList(),
LocationIds = genericStringArray.ToList(),
StartPrice = genericStringArray[0],
EndPrice = 2132123,
SearchQuery = "Search Query",
SubCategoryIds = genericStringArray.ToList(),
TargetAge = TargetAgeGroup.Kids
}).WillReturn(new UrlSeoProcessingResult()
{
LocationGroupId = 1,
SubCategoryId = 2,
IsError = false,
Type = SegmentType.GroupSubCategory
});
var classSearchInput = mapper.ClassSearchRawInputToClassSearchInput(searchRawInput);
Assert.AreEqual(TargetAgeGroup.Kids, classSearchInput.TargetAge, "Age is not mapped properly");
Assert.AreEqual(3,classSearchInput.LocationIds.Count,"Location not mapped properly");
Assert.AreEqual(3,classSearchInput.CategoryIds.Count,"Category is not mapped correctly");
}