鉴于这些类,我如何映射它们的字典?
public class TestClass
{
public string Name { get; set; }
}
public class TestClassDto
{
public string Name { get; set; }
}
Mapper.CreateMap<TestClass, TestClassDto>();
Mapper.CreateMap<Dictionary<string, TestClass>,
Dictionary<string, TestClassDto>>();
var testDict = new Dictionary<string, TestClass>();
var testValue = new TestClass() {Name = "value1"};
testDict.Add("key1", testValue);
var mappedValue = Mapper.Map<TestClass, TestClassDto>(testValue);
var mappedDict = Mapper.Map<Dictionary<string, TestClass>,
Dictionary<string, TestClassDto>>(testDict);
在这种情况下映射其中一个,mappedValue,工作正常。
映射它们的字典最终没有目标对象中的条目。
我在做什么?
你遇到的问题是因为AutoMapper正在努力映射 内容 字典。在这种情况下,你必须考虑它是什么商店 KeyValuePairs。
如果您尝试为KeyValuePair组合创建一个映射器,您将很快发现您不能直接作为 Key属性没有setter。
AutoMapper通过允许您使用构造函数进行Map来解决这个问题。
/* Create the map for the base object - be explicit for good readability */
Mapper.CreateMap<TestClass, TestClassDto>()
.ForMember( x => x.Name, o => o.MapFrom( y => y.Name ) );
/* Create the map using construct using rather than ForMember */
Mapper.CreateMap<KeyValuePair<string, TestClass>, KeyValuePair<string, TestClassDto>>()
.ConstructUsing( x => new KeyValuePair<string, TestClassDto>( x.Key,
x.Value.MapTo<TestClassDto>() ) );
var testDict = new Dictionary<string, TestClass>();
var testValue = new TestClass()
{
Name = "value1"
};
testDict.Add( "key1", testValue );
/* Mapped Dict will have your new KeyValuePair in there */
var mappedDict = Mapper.Map<Dictionary<string, TestClass>,
Dictionary<string, TestClassDto>>( testDict );
你遇到的问题是因为AutoMapper正在努力映射 内容 字典。在这种情况下,你必须考虑它是什么商店 KeyValuePairs。
如果您尝试为KeyValuePair组合创建一个映射器,您将很快发现您不能直接作为 Key属性没有setter。
AutoMapper通过允许您使用构造函数进行Map来解决这个问题。
/* Create the map for the base object - be explicit for good readability */
Mapper.CreateMap<TestClass, TestClassDto>()
.ForMember( x => x.Name, o => o.MapFrom( y => y.Name ) );
/* Create the map using construct using rather than ForMember */
Mapper.CreateMap<KeyValuePair<string, TestClass>, KeyValuePair<string, TestClassDto>>()
.ConstructUsing( x => new KeyValuePair<string, TestClassDto>( x.Key,
x.Value.MapTo<TestClassDto>() ) );
var testDict = new Dictionary<string, TestClass>();
var testValue = new TestClass()
{
Name = "value1"
};
testDict.Add( "key1", testValue );
/* Mapped Dict will have your new KeyValuePair in there */
var mappedDict = Mapper.Map<Dictionary<string, TestClass>,
Dictionary<string, TestClassDto>>( testDict );