[英]C# Automapper -> Conditional Mapping
I am trying to do some conditional mapping, and all the documentation and questions I have read through doesn't seem to cover this particular conditional. 我正在尝试做一些条件映射,我读过的所有文档和问题似乎都没有涵盖这个特定的条件。 I was hoping someone here would have the experience or know how on the best approach to this.
我希望有人在这里有经验或知道最好的方法。
I am mapping an object that has two properties. 我正在映射一个具有两个属性的对象。 However, I do not want to map EITHER property, if a specific property is of a value.
但是,如果特定属性是值,我不想映射EITHER属性。 To visualize this:
想象一下:
foreach(var object in objectB) {
If (object.propertyA == "SomeValue")
continue;
else
Mapper.Map<ObjectA>(object);
}
however, I want the AutoMapper
equivalent of the above statement. 但是,我希望
AutoMapper
等同于上面的语句。 So something more like: 所以更像是:
cfg.CreateMap<ObjectB, ObjectA>()
.ForMember(dest => dest.PropertyA, m => m.Condition(source => source.PropertyA != "SomeValue"))
.ForMember(dest => dest.PropertyB, m => m.Condition(source => source.PropertyA != "SomeVAlue" ? source.PropertyB : ignore))
But the above version obviously does not work. 但上面的版本显然不起作用。 Thank you in advance for your assistance.
提前感谢您的协助。
Could be achieved using conditional mapping, see Documentation( http://docs.automapper.org/en/latest/Conditional-mapping.html ) for details. 可以使用条件映射来实现,有关详细信息,请参阅文档( http://docs.automapper.org/en/latest/Conditional-mapping.html )。 To cover arrays filtering case I created a custom type converter which is a little bit tricky (see http://docs.automapper.org/en/stable/Custom-type-converters.html ).
为了覆盖数组过滤案例,我创建了一个有点棘手的自定义类型转换器(请参阅http://docs.automapper.org/en/stable/Custom-type-converters.html )。 The updated example is below
更新的示例如下
using AutoMapper;
using System;
using System.Collections.Generic;
namespace ConsoleAppTest2
{
class Program
{
static void Main(string[] args)
{
Mapper.Initialize(cfg => {
//Using specific type converter for specific arrays
cfg.CreateMap<Foo[], FooDto[]>().ConvertUsing(new ArrayFilterTypeConverter<Foo[], FooDto[], Foo, FooDto>(
(src, dest) => (src.Age > 0)
));
cfg.CreateMap<Foo, FooDto>()
.ForMember(dest => dest.Age, opt => opt.Condition(src => (src.Age >= 0)))
.ForMember(dest => dest.CurrentAddress, opt =>
{
opt.Condition(src => (src.Age >= 0));
opt.MapFrom(src => src.Address);
});
});
var foo = new Foo() { Name = "Name", Address = "Address", Age = -1 };
var fooDTO = new FooDto();
var fooArray = new Foo[] {
new Foo() { Name = "Name1", Address = "Address1", Age = -1 },
new Foo() { Name = "Name2", Address = "Address2", Age = 1 },
new Foo() { Name = "Name3", Address = "Address3", Age = 1 }
};
var fooDTOArray = Mapper.Map<Foo[], FooDto[]>(fooArray); //get 2 elements instead of 3
Mapper.Map(foo, fooDTO);
//The result is we skipped Address and Age properties becase Age is negative
Console.ReadLine();
}
}
public class ArrayFilterTypeConverter<TSourceArray, TDestArray, TSource, TDest> : ITypeConverter<TSourceArray, TDestArray>
{
private Func<TSource, TDest, bool> filter;
public ArrayFilterTypeConverter(Func<TSource, TDest, bool> filter)
{
this.filter = filter;
}
public TDestArray Convert(TSourceArray source, TDestArray destination, ResolutionContext context)
{
var sourceArray = source as TSource[];
List<TDest> destList = new List<TDest>();
var typeMap = context.ConfigurationProvider.ResolveTypeMap(typeof(TSource), typeof(TDest));
foreach (var src in sourceArray)
{
var dest = context.Mapper.Map<TSource, TDest>(src);
if (filter(src, dest))
destList.Add(dest);
}
// Little hack to cast array to TDestArray
var result = (TDestArray)(object)destList.ToArray();
return result;
}
}
internal class FooDto
{
public string Name { get; set; }
public string CurrentAddress { get; set; }
public int Age { get; set; }
}
internal class Foo
{
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.