如何将源对象中的许多属性的值转换为目标对象中的单个类型? 我可以在这种情况下使用Value Resolvers吗?或者可能有更好的解决方案?
以下是来自example的documentation - 一对一转换
Mapper.CreateMap<Source, Destination>()
.ForMember(dest => dest.Total,
opt => opt.ResolveUsing<CustomResolver>().FromMember(src => src.SubTotal));
Mapper.CreateMap<OtherSource, OtherDest>()
.ForMember(dest => dest.OtherTotal,
opt => opt.ResolveUsing<CustomResolver>().FromMember(src => src.OtherSubTotal));
public class CustomResolver : ValueResolver<decimal, decimal> {
// logic here
}
我想将两个对象转换为一个(多对一转换)。例如:
public class Document
{
public int CurrencyId {get; set;}
public int ExchangeRateId {get; set;}
}
public class DocumentDto
{
public Currency Currency {get; set;}
}
public class CurrencyDetails
{
public Currency Currency {get; private set;}
public ExchangeRate ExchangeRate {get; private set;}
public CurrencyDetails(Currency currency, ExchangeRate exchangeRate)
{
Currency = currency;
ExchangeRate = exchangeRate;
}
}
我想实现类似的目标:
public class CurrencyResolver : ValueResolver<int, int, CurrencyDetails>
{
protected override Currency ResolveCore(int currencyId, int exchangeRateId)
{
var currency = new Currency(currencyId); //simplified logic
var exchangeRate = new ExchangeRate(exchangeRateId);
var currencyDetails = new CurrencyDetails(currency, exchangeRate);
return currencyDetails;
}
}
我知道我可以将整个对象作为源对象传递,但对我来说这不是解决方案:
ValueResolver<Document, Currency>
我无法使用完整对象,因为我有很多文档类型,并且我不想为每个文档创建新的解析器。 在我的情况下,也不允许忽略元素(用于手动转换)。货币转换逻辑必须由AutoMapper执行。
对我来说,转换是在后台进行的(在主要主题转换过程中)非常重要。
例如:
Document document;
var documentDto = Mapper.Map<DocumentDto>(document); // and in this moment i have proper CurrencyDetails object!
感谢您的建议。
我想出了两个解决方案,但我不喜欢它们(soooo dirty)
解决方案1 - 使用interface:
包装一个类public interface ICurrencyHolder
{
int CurrencyId {get; set;}
int ExchangeRateId {get; set;}
}
public class Document : ICurrencyHolder
{
public int CurrencyId {get; set;}
public int ExchangeRateId {get; set;}
}
并使用具有以下参数的解析器:
ValueResolver<ICurrencyHolder, Currency>
解决方案2 - 将其作为源元素对象类型并通过反射获取值
ValueResolver<object, Currency>
这太糟糕了!
答案 0 :(得分:8)
如果我理解正确,您需要执行以下映射:从(CurrencyId
,ExchangeRateId
)到Currency
。您可以使用Tuple
(it is a standard .Net class在这些情况下非常方便)来实现它:
Mapper.CreateMap<Tuple<int,int>, Currency>()
.ForMember(x => x.Currency, cfg => cfg.MapFrom(y => new Currency(y.Item1, y.Item2));
按如下方式调用映射器:
Mapper.Map<Tuple<int,int>, Currency>(Tuple.Create(doc.CurrencyId, doc.ExchangeRateId));
答案 1 :(得分:2)
也许你可以这样映射:
Mapper.CreateMap<Source, Destination>()
.ConstructUsing(s => Mapper.Map<Source, Currency>(s));
Mapper.CreateMap<Source, Currency>()
.ForMember(dst => dst.CurrencySymbol, map => map.MapFrom(src => src.DocumentDto.CurrencySymbol))
.ForMember(dst => dst.ExchangeRate , map => map.MapFrom(src => src.Document.ExchangeRate ));
也可能:
Mapper.CreateMap<Source, Destination>()
.ConstructUsing(s => Mapper.Map<Source, Currency>(s));
Mapper.CreateMap<Source, Currency>()
.ConstructUsing(s => Mapper.Map<DocumentDto, Currency>(s))
.ConstructUsing(s => Mapper.Map<Document, Currency>(s));
Mapper.CreateMap<DocumentDto, Currency>();
Mapper.CreateMap<Document, Currency>();
答案 2 :(得分:0)
如果您确定要为每种文档类型执行此操作:
Document document;
var documentDto = Mapper.Map<DocumentDto>(document);
然后你必须为每一个定义映射。所以我肯定会使用ICurrencyHolder的想法并使用这样的解析器:
解析器
public class CurrencyResolver : ValueResolver<ICurrencyHolder, Currency>
{
protected override Currency ResolveCore(ICurrencyHolder source)
{
return new Currency(source.CurrencyId, source.ExchangeRateId);
}
}
文档&#34;类型&#34;
public class Document : ICurrencyHolder
{
public int CurrencyId { get; set; }
public int ExchangeRateId { get; set; }
}
public class ExtendedDocument : ICurrencyHolder
{
public DateTime SomeDate { get; set; }
public int CurrencyId { get; set; }
public int ExchangeRateId { get; set; }
}
public interface ICurrencyHolder
{
int CurrencyId { get; set; }
int ExchangeRateId { get; set; }
}
和映射:
Mapper.CreateMap<Document, DocumentDto>().ForMember(m => m.Currency, opt => opt.ResolveUsing<CurrencyResolver>());
Mapper.CreateMap<ExtendedDocument, DocumentDto>().ForMember(m => m.Currency, opt => opt.ResolveUsing<CurrencyResolver>());
有了这个,您就可以创建这样的dto,并在映射阶段为您解决货币问题:
var dto = Mapper.Map<DocumentDto>(document);
var extendedDto = Mapper.Map<DocumentDto>(extendedDocument);