当源上的 属性 为假时是否可以映射到 null?
Is it possible to map to null when a property on source is false?
我正在定义源 class 和目标 class 之间的映射:
Mapper.CreateMap<Source, Target>();
如果 Source
上的某个 属性 设置为特定值,我希望映射到 return null
:
// If Source.IsValid = false I want the mapping to return null
Source s = new Source { IsValid = false };
Target t = Mapper.Map<Target>(s);
Assert.IsNull(t);
如何配置 AutoMapper 来实现?
编辑
如果你想让你的地图输出一个空对象,我建议使用两个级别的映射:第一级决定是否 return 一个空对象通过接管映射行为 ConvertUsing
方法,如果需要映射,它将它推迟到内部映射引擎:
static void Main(string[] args)
{
var innerConfigurationStore = new ConfigurationStore(new TypeMapFactory(), MapperRegistry.Mappers);
innerConfigurationStore.CreateMap<From, To>()
.ForMember(to => to.DestinationName, opt => opt.MapFrom(from => from.Active ? from.Name : (string)null));
var innerMappingEngine = new MappingEngine(innerConfigurationStore);
Mapper.CreateMap<From, To>()
.ConvertUsing(from => from.Active ? innerMappingEngine.Map<To>(from) : null)
;
WriteMappedFrom(new From() {Active = true, Name = "ActiveFrom"});
WriteMappedFrom(new From() {Active = false, Name = "InactiveFrom"});
}
static void WriteMappedFrom(From from)
{
Console.WriteLine("Mapping from " + (from.Active ? "active" : "inactive") + " " + from.Name);
var to = Mapper.Map<To>(from);
Console.WriteLine("To -> " + (to == null ? "null" : "not null"));
}
初始答案
在Automapper中使用条件映射非常简单,这里是一些示例代码
public class From
{
public bool Active { get; set; }
public string Name { get; set; }
}
public class To
{
public string DestinationName { get; set; }
}
static void Main(string[] args)
{
Mapper.CreateMap<From, To>()
.ForMember(to => to.DestinationName, opt => opt.MapFrom(from => from.Active ? from.Name : (string)null));
WriteMappedFrom(new From() {Active = true, Name = "ActiveFrom"});
WriteMappedFrom(new From() {Active = false, Name = "InactiveFrom"});
}
static void WriteMappedFrom(From from)
{
Console.WriteLine("Mapping from " + (from.Active ? "active" : "inactive") + " " + from.Name);
var to = Mapper.Map<To>(from);
Console.WriteLine("To -> " + (to.DestinationName ?? "null"));
}
automapper 中还有其他 conditional mappings,但它们似乎只适用于基于约定的映射。但是,您可以进一步查看文档以确认
您可以这样定义您的映射:
Mapper.CreateMap<Source, Target>().TypeMap
.SetCondition(r => ((Source)r.SourceValue).IsValid);
我正在定义源 class 和目标 class 之间的映射:
Mapper.CreateMap<Source, Target>();
如果 Source
上的某个 属性 设置为特定值,我希望映射到 return null
:
// If Source.IsValid = false I want the mapping to return null
Source s = new Source { IsValid = false };
Target t = Mapper.Map<Target>(s);
Assert.IsNull(t);
如何配置 AutoMapper 来实现?
编辑
如果你想让你的地图输出一个空对象,我建议使用两个级别的映射:第一级决定是否 return 一个空对象通过接管映射行为 ConvertUsing
方法,如果需要映射,它将它推迟到内部映射引擎:
static void Main(string[] args)
{
var innerConfigurationStore = new ConfigurationStore(new TypeMapFactory(), MapperRegistry.Mappers);
innerConfigurationStore.CreateMap<From, To>()
.ForMember(to => to.DestinationName, opt => opt.MapFrom(from => from.Active ? from.Name : (string)null));
var innerMappingEngine = new MappingEngine(innerConfigurationStore);
Mapper.CreateMap<From, To>()
.ConvertUsing(from => from.Active ? innerMappingEngine.Map<To>(from) : null)
;
WriteMappedFrom(new From() {Active = true, Name = "ActiveFrom"});
WriteMappedFrom(new From() {Active = false, Name = "InactiveFrom"});
}
static void WriteMappedFrom(From from)
{
Console.WriteLine("Mapping from " + (from.Active ? "active" : "inactive") + " " + from.Name);
var to = Mapper.Map<To>(from);
Console.WriteLine("To -> " + (to == null ? "null" : "not null"));
}
初始答案
在Automapper中使用条件映射非常简单,这里是一些示例代码
public class From
{
public bool Active { get; set; }
public string Name { get; set; }
}
public class To
{
public string DestinationName { get; set; }
}
static void Main(string[] args)
{
Mapper.CreateMap<From, To>()
.ForMember(to => to.DestinationName, opt => opt.MapFrom(from => from.Active ? from.Name : (string)null));
WriteMappedFrom(new From() {Active = true, Name = "ActiveFrom"});
WriteMappedFrom(new From() {Active = false, Name = "InactiveFrom"});
}
static void WriteMappedFrom(From from)
{
Console.WriteLine("Mapping from " + (from.Active ? "active" : "inactive") + " " + from.Name);
var to = Mapper.Map<To>(from);
Console.WriteLine("To -> " + (to.DestinationName ?? "null"));
}
automapper 中还有其他 conditional mappings,但它们似乎只适用于基于约定的映射。但是,您可以进一步查看文档以确认
您可以这样定义您的映射:
Mapper.CreateMap<Source, Target>().TypeMap
.SetCondition(r => ((Source)r.SourceValue).IsValid);