我正在构建一个通用的漂亮打印方法.我想分开处理的一种特殊类型是KeyValuePair<TK,TV>.为了将对象缩小为已知类型,我想我会将每个映射KeyValuePair<TK,TV>到a KeyValuePair<object, object>.
下面的代码总是产生在2个空值Key,Value的性能proxy.
Mapper.CreateMap(o.GetType(), typeof(KeyValuePair<object, object>));
var proxy = Mapper.Map<KeyValuePair<object, object>>(o);
Run Code Online (Sandbox Code Playgroud)
另一方面,这个非通用版本按预期工作:
Mapper.CreateMap(o.GetType(), typeof(DictionaryEntry));
var proxy = Mapper.Map<DictionaryEntry>(o);
Run Code Online (Sandbox Code Playgroud)
为什么?
o在这个阶段已被测试为KeyValuePair<,>.
我在.NET 4.0上使用AutoMapper 3.2.1.0.
嗨,我正在尝试将AutoMapper添加到我的应用程序中,但我似乎在eintegrationg中遇到了一些问题.这是我到目前为止所拥有的.
为了不创建与Automapper的直接依赖关系,我为它的最基本功能创建了一个简单的maping:
public class AutoMapper : IAutoMapper
{
public void CreateMap<TFrom, TTo>()
{
Mapper.CreateMap<TFrom, TTo>();
}
public TTo Map<TFrom, TTo>(TFrom data)
{
return Mapper.Map<TFrom, TTo>(data);
}
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个配置文件:
public class AutoMapperConfig
{
private readonly IAutoMapper mapper;
public AutoMapperConfig(IAutoMapper mapper)
{
this.mapper = mapper;
}
public void RegisterMappings()
{
mapper.CreateMap<ProductDTO , ProductDataContract>();
}
}
Run Code Online (Sandbox Code Playgroud)
并在我的Global.Asax中添加了一个调用:
new AutoMapperConfig(new AutoMapper()).RegisterMappings();
Run Code Online (Sandbox Code Playgroud)
我有这两个对象beetwen我想创建一个映射:
public class ProductDTO
{
public int ProductId { get; set; }
public int CategoryId { get; set; }
public int SubcategoryId { …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我需要在不同的对象对之间进行许多映射(域对象,DTO,ViewModels等)。我AutoMapper为此大量使用。
因此,我有一个通用的Mapper类,该类具有映射不同对象的方法。作为映射在应用程序中坚持,我尽我CreateMap()的static constructor,这样的映射创建只有一次,是由我可能会使用他们的时间准备。这个班级看起来像这样
public class SomeMapper
{
static SomeMapper()
{
Mapper.CreateMap<SomeType1, SomeOtherType1>();
Mapper.CreateMap<SomeType2, SomeOtherType2>();
Mapper.CreateMap<SomeType3, SomeOtherType3>();
//...
}
public SomeOtherType1 MapSomeHierarchy1(SomeType1 source)
{ //... }
public SomeOtherType2 MapSomeHierarchy2(SomeType2 source)
{ //... }
}
Run Code Online (Sandbox Code Playgroud)
问题1:什么是创建映射的更好方法?(以任何方式更好-性能,语义,标准惯例等)
问题2:此代码也用于Console应用程序中。在特定的运行中,它不需要所有映射。因此,与其急切地创建所有映射,不可以在运行时创建映射(如果尚不存在)?就像是
public SomeOtherTypeN MapSomeHierarchyN(SomeTypeN source)
{
if (!AlreadyMapped(SomeTypeN, SomeOtherTypeN))
Mapper.CreateMap<SomeTypeN, SomeOtherTypeN>();
return Mapper.Map<SomeOtherTypeN>(source);
}
Run Code Online (Sandbox Code Playgroud)
有没有简单的方法来实现该方法AlreadyMapped()?
我有以下与列表相关的实体:
国家 - >地区 - >市政 - >街
当我将Country对象映射到DTO时,AutoMapper会自动将整个图形投影到Streets,这是一个很好的默认设置.在一个特定的用例中,我想只映射根对象(Country)及其直接子对象(Regions).这些区域应该具有空的市政列表(或null).
实现此目的的一种方法是创建这样的地图:
Mapper.CreateMap<Data.Country, Dto.Country>();
Mapper.CreateMap<Data.Region, Dto.Region>()
.ForMember(dest => dest.Municipalities, opt => opt.Ignore())
Run Code Online (Sandbox Code Playgroud)
这意味着当将Region作为根对象进行投影时,其城市列表将被忽略.解决方法是为每个可能的根对象创建单独的ConfigurationStore对象,但这会导致许多不同的ConfigurationStore.有没有办法告诉AutoMapper只映射到对象图中的某个深度?
我不确定我是否在思考这个问题,但我无法解决这个问题.
我在这里有一个名为Template的父对象
public Template()
{
public long Id { get; set; }
public Scoring SubProperty { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的Scoring对象,它是Template的子属性
public enum MyEnum : short
{
Basic = 0
}
public Scoring()
{
public MyEnum Type { get; set; }
public string Text { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我有一个像我这样定义的TemplateModel,我想转换为
public TemplateModel()
{
public long Id { get; set; }
public string Type { get; set; }
public string Text { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在我的AutoMapper配置文件中,我已经设置了这样,将模板转换为TemplateModel.
public class TemplateProfile : …Run Code Online (Sandbox Code Playgroud)