AutoMapper新增的IValueResolver如何使用?
How to use the new IValueResolver of AutoMapper?
新版AutoMapper中新的IValueResolver
界面如何使用,我一头雾水。可能是我在之前版本的AutoMapper中使用不当...
我有很多模型 classes,其中一些是使用 sqlmetal 从多个数据库服务器上的多个数据库生成的。
其中一些 classes 有一个字符串 属性、PublicationCode
,它标识订阅、报价、发票或其他任何内容属于哪个出版物。
该发布可以存在于两个系统(旧系统和新系统)中的任何一个中,因此我在目标模型 classes 上有一个布尔值 属性,它告诉我该发布是否在旧系统还是新系统。
使用AutoMapper的旧版本(<5?),我使用了一个ValueResolver<string, bool>
,它将PublicationCode
作为输入参数,并返回一个bool
指示位置出版物(旧系统或新系统)。
使用 AutoMapper 的新版本(5+?),这似乎不再可能。新的 IValueResolver 需要我拥有的每个源模型和目标模型组合的唯一实现,其中 src.PublicationCode
需要解析为 dst.IsInNewSystem
.
我只是想以错误的方式使用值解析器吗?有没有更好的办法?我想使用解析器的主要原因是我更愿意将服务注入构造函数,而不必在代码中使用 DependencyResolver
之类的东西(我正在使用 Autofac)。
目前我的使用方式如下:
// Class from Linq-to-SQL, non-related properties removed.
public class FindCustomerServiceSellOffers {
public string PublicationCode { get; set; }
}
这是我拥有的多个数据模型 class 之一,其中包含一个 PublicationCode 属性)。这个特定的 class 映射到这个视图模型:
public class SalesPitchViewModel {
public bool IsInNewSystem { get; set; }
}
这两个 classes 的映射定义是(其中 expression 是一个 IProfileExpression),删除了不相关的映射:
expression.CreateMap<FindCustomerServiceSellOffers, SalesPitchViewModel>()
.ForMember(d => d.IsInNewSystem, o => o.ResolveUsing<PublicationSystemResolver>().FromMember(s => s.PublicationCode));
解析器:
public class PublicationSystemResolver : ValueResolver<string, bool>
{
private readonly PublicationService _publicationService;
public PublicationSystemResolver(PublicationService publicationService)
{
_publicationService = publicationService;
}
protected override bool ResolveCore(string publicationCode)
{
return _publicationService.IsInNewSystem(publicationCode);
}
}
以及映射器的使用:
var result = context.FindCustomerServiceSellOffers.Where(o => someCriteria).Select(_mapper.Map<SalesPitchViewModel>).ToList();
您可以通过实现 IMemberValueResolver<object, object, string, bool>
并在您的映射配置中使用它来创建更通用的值解析器。您可以像以前一样提供源 属性 解析函数:
public class PublicationSystemResolver : IMemberValueResolver<object, object, string, bool>
{
private readonly PublicationService _publicationService;
public PublicationSystemResolver(PublicationService publicationService)
{
this._publicationService = publicationService;
}
public bool Resolve(object source, object destination, string sourceMember, bool destMember, ResolutionContext context)
{
return _publicationService.IsInNewSystem(sourceMember);
}
}
cfg.CreateMap<FindCustomerServiceSellOffers, SalesPitchViewModel>()
.ForMember(dest => dest.IsInNewSystem,
src => src.ResolveUsing<PublicationSystemResolver, string>(s => s.PublicationCode));
新版AutoMapper中新的IValueResolver
界面如何使用,我一头雾水。可能是我在之前版本的AutoMapper中使用不当...
我有很多模型 classes,其中一些是使用 sqlmetal 从多个数据库服务器上的多个数据库生成的。
其中一些 classes 有一个字符串 属性、PublicationCode
,它标识订阅、报价、发票或其他任何内容属于哪个出版物。
该发布可以存在于两个系统(旧系统和新系统)中的任何一个中,因此我在目标模型 classes 上有一个布尔值 属性,它告诉我该发布是否在旧系统还是新系统。
使用AutoMapper的旧版本(<5?),我使用了一个ValueResolver<string, bool>
,它将PublicationCode
作为输入参数,并返回一个bool
指示位置出版物(旧系统或新系统)。
使用 AutoMapper 的新版本(5+?),这似乎不再可能。新的 IValueResolver 需要我拥有的每个源模型和目标模型组合的唯一实现,其中 src.PublicationCode
需要解析为 dst.IsInNewSystem
.
我只是想以错误的方式使用值解析器吗?有没有更好的办法?我想使用解析器的主要原因是我更愿意将服务注入构造函数,而不必在代码中使用 DependencyResolver
之类的东西(我正在使用 Autofac)。
目前我的使用方式如下:
// Class from Linq-to-SQL, non-related properties removed.
public class FindCustomerServiceSellOffers {
public string PublicationCode { get; set; }
}
这是我拥有的多个数据模型 class 之一,其中包含一个 PublicationCode 属性)。这个特定的 class 映射到这个视图模型:
public class SalesPitchViewModel {
public bool IsInNewSystem { get; set; }
}
这两个 classes 的映射定义是(其中 expression 是一个 IProfileExpression),删除了不相关的映射:
expression.CreateMap<FindCustomerServiceSellOffers, SalesPitchViewModel>()
.ForMember(d => d.IsInNewSystem, o => o.ResolveUsing<PublicationSystemResolver>().FromMember(s => s.PublicationCode));
解析器:
public class PublicationSystemResolver : ValueResolver<string, bool>
{
private readonly PublicationService _publicationService;
public PublicationSystemResolver(PublicationService publicationService)
{
_publicationService = publicationService;
}
protected override bool ResolveCore(string publicationCode)
{
return _publicationService.IsInNewSystem(publicationCode);
}
}
以及映射器的使用:
var result = context.FindCustomerServiceSellOffers.Where(o => someCriteria).Select(_mapper.Map<SalesPitchViewModel>).ToList();
您可以通过实现 IMemberValueResolver<object, object, string, bool>
并在您的映射配置中使用它来创建更通用的值解析器。您可以像以前一样提供源 属性 解析函数:
public class PublicationSystemResolver : IMemberValueResolver<object, object, string, bool>
{
private readonly PublicationService _publicationService;
public PublicationSystemResolver(PublicationService publicationService)
{
this._publicationService = publicationService;
}
public bool Resolve(object source, object destination, string sourceMember, bool destMember, ResolutionContext context)
{
return _publicationService.IsInNewSystem(sourceMember);
}
}
cfg.CreateMap<FindCustomerServiceSellOffers, SalesPitchViewModel>()
.ForMember(dest => dest.IsInNewSystem,
src => src.ResolveUsing<PublicationSystemResolver, string>(s => s.PublicationCode));