如何使用 AfterMap 映射集合的属性 属性
How to use AfterMap to map properties on collection property
我有两个实体和两个 DTO。我正在将实体映射到 DTO。 DTO 的简化版本如下所示:
public class FooDto {
// Other properties removed for clarity.
public string Description { get; set; }
public decimal Total { get; set; }
public ICollection<BarDto> Bars { get; set; }
}
public class BarDto {
// Other properties removed for clarity.
public decimal Total { get; set; }
}
Foo
和Bar
类是:
public class Foo {
public ICollection<Bar> Bars { get; set; }
}
public class Bar {
// Unimportant properties
}
映射
我在方法中将其映射为:
public FooDto Map(IMapper mapper, Foo foo) {
// _fooTotalService and _barTotalService injected elsewhere by DI.
return mapper.Map<Foo, FooDto>(foo, opt =>
{
opt.AfterMap((src, dest) =>
{
dest.Total = _fooTotalService.GetTotal(src);
dest.Bars.Total = ?????? // Needs to use _barTotalService.CalculateTotal(bar)
});
});
}
AutoMapper 已经配置了 Foo 到 FooDto 和 Bar 到 BarDto 的映射,工作正常。
我需要使用服务更新 FooDto 中的每个 BarDto(原因太长,无法进入 - 足以说明它需要以这种方式发生)。
我需要在 AfterMap
中使用什么语法来使用 _barTotalService.CalculateTotal(bar)
方法映射 BarDto
的每个 Total
属性,其中 bar
是有问题的 Bar
吗?
请注意,_barTotalService.CalculateTotal
方法采用 Bar
而非 BarDto
的实例。
这应该有效 -
AutoMapper.Mapper.CreateMap<Foo, FooDto>()
.AfterMap((src, dest) =>
{
dest.Total = 8;//service call here
for (var i = 0; i < dest.Bars.Count; i++)
{
dest.Bars.ElementAt(i).Total = 9;//service call with src.Bars.ElementAt(i)
}
});
AutoMapper.Mapper.CreateMap<Bar, BarDto>();
var t = AutoMapper.Mapper.Map<FooDto>(new Foo
{
Bars = new List<Bar> { new Bar { } }
});
您可以考虑使用 TypeConverter
,这些 类 可以在其构造函数中进行依赖注入。所以你在那里注入服务,并在 CreateMap
.
之后使用 ConstructUsing
之类的东西
我有两个实体和两个 DTO。我正在将实体映射到 DTO。 DTO 的简化版本如下所示:
public class FooDto {
// Other properties removed for clarity.
public string Description { get; set; }
public decimal Total { get; set; }
public ICollection<BarDto> Bars { get; set; }
}
public class BarDto {
// Other properties removed for clarity.
public decimal Total { get; set; }
}
Foo
和Bar
类是:
public class Foo {
public ICollection<Bar> Bars { get; set; }
}
public class Bar {
// Unimportant properties
}
映射
我在方法中将其映射为:
public FooDto Map(IMapper mapper, Foo foo) {
// _fooTotalService and _barTotalService injected elsewhere by DI.
return mapper.Map<Foo, FooDto>(foo, opt =>
{
opt.AfterMap((src, dest) =>
{
dest.Total = _fooTotalService.GetTotal(src);
dest.Bars.Total = ?????? // Needs to use _barTotalService.CalculateTotal(bar)
});
});
}
AutoMapper 已经配置了 Foo 到 FooDto 和 Bar 到 BarDto 的映射,工作正常。
我需要使用服务更新 FooDto 中的每个 BarDto(原因太长,无法进入 - 足以说明它需要以这种方式发生)。
我需要在 AfterMap
中使用什么语法来使用 _barTotalService.CalculateTotal(bar)
方法映射 BarDto
的每个 Total
属性,其中 bar
是有问题的 Bar
吗?
请注意,_barTotalService.CalculateTotal
方法采用 Bar
而非 BarDto
的实例。
这应该有效 -
AutoMapper.Mapper.CreateMap<Foo, FooDto>()
.AfterMap((src, dest) =>
{
dest.Total = 8;//service call here
for (var i = 0; i < dest.Bars.Count; i++)
{
dest.Bars.ElementAt(i).Total = 9;//service call with src.Bars.ElementAt(i)
}
});
AutoMapper.Mapper.CreateMap<Bar, BarDto>();
var t = AutoMapper.Mapper.Map<FooDto>(new Foo
{
Bars = new List<Bar> { new Bar { } }
});
您可以考虑使用 TypeConverter
,这些 类 可以在其构造函数中进行依赖注入。所以你在那里注入服务,并在 CreateMap
.
ConstructUsing
之类的东西