Automapper - 在目的地分配排名 object
Automapper - Assign Ranking in the Destination object
我有以下来源和目的地 objects:
public class Source
{
public string Value1{ get; set; }
}
public class Destination
{
public string Value1{ get; set; }
public int Ranking { get; set; }
}
我正在尝试将源的 collection object 映射到目标的 Collection object。我的来源collection是这样的:
var source = new List<Source>();
source.Add(new Source(){ Value1= "test1" });
source.Add(new Source(){ Value1= "test2" });
如果目标 object 中的排名属性应对应于源 collection 中的项目索引,我该如何编写映射器?
即映射后 Source
和 Value1 = "test1"
的目的地排名将是 1
,下一个将是 2
。
在地图动作前后使用(参见the docs):
var source = new List<Source>();
source.Add(new Source() { Value1 = "test1" });
source.Add(new Source() { Value1 = "test2" });
Mapper.Initialize(config =>
{
config.CreateMap<Source, Destination>();
});
var destinations = source.Select(x => Mapper.Map<Source, Destination>(x, opt => {
opt.AfterMap((src, dest) => dest.Ranking = source.IndexOf(x) + 1);
}));
您可以通过几种方式完成,但如果您希望转换器单元可测试,我建议使用继承自 ITyperConverter<TSource, TDestination>
.
的 class
转换 Class:
public class SourceToDestinationConverter : ITypeConverter<Source, Destination>
{
private int currentRank;
public SourceToDestinationConverter(int rank)
{
currentRank = rank;
}
public Destination Convert(Source source, Destination destination, ResolutionContext context)
{
destination = new Destination
{
Value1 = source.Value1,
Ranking = currentRank
};
return destination;
}
}
正在将您的 class 设置为映射器配置:
Mapper.Initialize(config =>
{
config.CreateMap<Source, Destination>().ConvertUsing<SourceToDestinationConverter>();
});
Mapper.AssertConfigurationIsValid();
调用方法:
var sources = new List<Source>{
new Source() { Value1 = "test1" },
new Source() { Value1 = "test2" }
};
var destinationsRanked = sources.Select(s => Mapper.Map<Source, Destination>(s, options => options.ConstructServicesUsing(
type => new SourceToDestinationConverter((source.IndexOf(s) + 1))
)));
结果是。
Value1 | Ranking
test1 | 1
test2 | 2
我有以下来源和目的地 objects:
public class Source
{
public string Value1{ get; set; }
}
public class Destination
{
public string Value1{ get; set; }
public int Ranking { get; set; }
}
我正在尝试将源的 collection object 映射到目标的 Collection object。我的来源collection是这样的:
var source = new List<Source>();
source.Add(new Source(){ Value1= "test1" });
source.Add(new Source(){ Value1= "test2" });
如果目标 object 中的排名属性应对应于源 collection 中的项目索引,我该如何编写映射器?
即映射后 Source
和 Value1 = "test1"
的目的地排名将是 1
,下一个将是 2
。
在地图动作前后使用(参见the docs):
var source = new List<Source>();
source.Add(new Source() { Value1 = "test1" });
source.Add(new Source() { Value1 = "test2" });
Mapper.Initialize(config =>
{
config.CreateMap<Source, Destination>();
});
var destinations = source.Select(x => Mapper.Map<Source, Destination>(x, opt => {
opt.AfterMap((src, dest) => dest.Ranking = source.IndexOf(x) + 1);
}));
您可以通过几种方式完成,但如果您希望转换器单元可测试,我建议使用继承自 ITyperConverter<TSource, TDestination>
.
转换 Class:
public class SourceToDestinationConverter : ITypeConverter<Source, Destination>
{
private int currentRank;
public SourceToDestinationConverter(int rank)
{
currentRank = rank;
}
public Destination Convert(Source source, Destination destination, ResolutionContext context)
{
destination = new Destination
{
Value1 = source.Value1,
Ranking = currentRank
};
return destination;
}
}
正在将您的 class 设置为映射器配置:
Mapper.Initialize(config =>
{
config.CreateMap<Source, Destination>().ConvertUsing<SourceToDestinationConverter>();
});
Mapper.AssertConfigurationIsValid();
调用方法:
var sources = new List<Source>{
new Source() { Value1 = "test1" },
new Source() { Value1 = "test2" }
};
var destinationsRanked = sources.Select(s => Mapper.Map<Source, Destination>(s, options => options.ConstructServicesUsing(
type => new SourceToDestinationConverter((source.IndexOf(s) + 1))
)));
结果是。
Value1 | Ranking
test1 | 1
test2 | 2