C# 将其他对象传递给CreateMap
我的对象映射要求我传递源对象和附加对象,以便能够正确映射目标对象。然而,我无法确定一种方法来实现这一目标C# 将其他对象传递给CreateMap,c#,automapper,C#,Automapper,我的对象映射要求我传递源对象和附加对象,以便能够正确映射目标对象。然而,我无法确定一种方法来实现这一目标 public class SourceDto { public string Value1 { get; set; } public string Value2 { get; set; } public string SourceValue3 { get; set; } public string SourceValue4 { get; set; } } p
public class SourceDto
{
public string Value1 { get; set; }
public string Value2 { get; set; }
public string SourceValue3 { get; set; }
public string SourceValue4 { get; set; }
}
public class AnotherSourceDto
{
public string AnotherSourceValue1 { get; set; }
public string AnotherSourceValue2 { get; set; }
public string AnotherSourceValue3 { get; set; }
public string AnotherSourceValue4 { get; set; }
}
public class Destination
{
public string Value1 { get; set; }
public string Value2 { get; set; }
public string DestValue3 { get; set; }
}
public string ConvertToDestValue3(string sourceValue3, string anotherSourceValue2)
{
// Some logic goes here
return sourceValue3 + " " + anotherSourceValue2;
}
void Main()
{
var config = new MapperConfiguration(
cfg =>cfg.CreateMap<SourceDto, Destination>()
.ForMember(dest => dest.DestValue3,
opt => opt.MapFrom(
src => ConvertToDestValue3(src.SourceValue3, "" //Here I need to pass AnotherSourceDto.AnotherSourceValue2 ))));
}
公共类SourceDto
{
公共字符串值1{get;set;}
公共字符串值2{get;set;}
公共字符串SourceValue3{get;set;}
公共字符串SourceValue4{get;set;}
}
公共类另一个源DTO
{
公共字符串AnotherSourceValue1{get;set;}
公共字符串AnotherSourceValue2{get;set;}
公共字符串AnotherSourceValue3{get;set;}
公共字符串另一个SourceValue4{get;set;}
}
公务舱目的地
{
公共字符串值1{get;set;}
公共字符串值2{get;set;}
公共字符串DestValue3{get;set;}
}
公共字符串ConvertToDestValue3(字符串sourceValue3,字符串另一个SourceValue2)
{
//这里有一些逻辑
返回sourceValue3+“”+另一个sourcevalue2;
}
void Main()
{
var config=新的MapperConfiguration(
cfg=>cfg.CreateMap()
.FormMember(dest=>dest.DestValue3,
opt=>opt.MapFrom(
src=>ConvertToDestValue3(src.SourceValue3,“//这里我需要传递另一个sourcedto.AnotherSourceValue2));
}
恐怕答案是将该属性映射到AutoMapper之外
// put inside a static class
public static Destination CustomMap(
this IMapper mapper,
SourceDto source,
AnotherSourceDto anotherSource)
{
var destination = mapper.Map<Destination>(source);
destination.DestValue3 =
source.SourceValue3 + " " + anotherSource.AnotherSourceValue2;
return destination;
}
void Main()
{
var config = new MapperConfiguration(cfg =>
cfg.CreateMap<SourceDto, Destination>()
.ForMember(dest => dest.DestValue3, opt => opt.Ignore()));
// usage
var mapper = config.CreateMapper();
var source = new SourceDto { /* add properties */ };
var anotherSource = new AnotherSourceDto { /* add properties */ };
var destination = mapper.CustomMap(source, anotherSource);
}
这里有一个例子,您可以使用扩展方法来实现这一点,因此它仍然具有使用AutoMapper完成的外观和感觉
// put inside a static class
public static Destination CustomMap(
this IMapper mapper,
SourceDto source,
AnotherSourceDto anotherSource)
{
var destination = mapper.Map<Destination>(source);
destination.DestValue3 =
source.SourceValue3 + " " + anotherSource.AnotherSourceValue2;
return destination;
}
void Main()
{
var config = new MapperConfiguration(cfg =>
cfg.CreateMap<SourceDto, Destination>()
.ForMember(dest => dest.DestValue3, opt => opt.Ignore()));
// usage
var mapper = config.CreateMapper();
var source = new SourceDto { /* add properties */ };
var anotherSource = new AnotherSourceDto { /* add properties */ };
var destination = mapper.CustomMap(source, anotherSource);
}
//放入静态类中
公共静态目的地自定义地图(
这个IMapper映射器,
数据源到数据源,
另一个来源(从另一个来源到另一个来源)
{
var destination=mapper.Map(源);
目的地3=
source.SourceValue3+“”+anotherSource.AnotherSourceValue2;
返回目的地;
}
void Main()
{
var config=new-MapperConfiguration(cfg=>
cfg.CreateMap()
.FormMember(dest=>dest.DestValue3,opt=>opt.Ignore());
//用法
var mapper=config.CreateMapper();
var source=new SourceDto{/*添加属性*/};
var anotherSource=new AnotherSourceDto{/*添加属性*/};
var destination=mapper.CustomMap(源,另一个源);
}
此选项看起来更干净。实际上,我已经把它作为一个普通的方法来实现了,我将使用这个想法。非常感谢。