- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我不知道如何使用新的 IValueResolver
新版AutoMapper中的界面。可能是我在之前版本的AutoMapper中使用不当...
我有很多模型类,其中一些是使用 sqlmetal 从多个数据库服务器上的多个数据库生成的。
其中一些类有一个字符串属性,PublicationCode
,它标识订阅、报价、发票或其他任何内容属于哪个出版物。
发布可以存在于两个系统(旧系统和新系统)中的任何一个中,因此我在目标模型类上有一个 bool 属性,它告诉发布是在旧系统还是新系统中。
使用旧版本(<5?)的 AutoMapper,我使用了 ValueResolver<string, bool>
花了PublicationCode
作为输入参数,并返回一个 bool
指示发布位置(旧系统或新系统)。
随着 AutoMapper 的新版本(5+?),这似乎不再可能了。新的 IValueResolver 需要对我拥有的每个源模型和目标模型的组合进行独特的实现,其中 src.PublicationCode
需要解析为 dst.IsInNewSystem
.
我只是想以错误的方式使用值解析器吗?有没有更好的办法?我想使用解析器的主要原因是我更愿意将服务注入(inject)构造函数,而不必使用 DependencyResolver
以及代码中的类似内容(我正在使用 Autofac)。
目前我的使用方式如下:
// Class from Linq-to-SQL, non-related properties removed.
public class FindCustomerServiceSellOffers {
public string PublicationCode { get; set; }
}
这是我拥有的几个数据模型类之一,其中包含一个 PublicationCode 属性)。这个特定的类映射到这个 View 模型:
public class SalesPitchViewModel {
public bool IsInNewSystem { get; set; }
}
这两个类的映射定义是(其中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));
关于c# - AutoMapper新增的IValueResolver如何使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38306461/
我正在尝试在两个对象列表之间进行映射。源类型具有类型为 A 的复杂属性。 ;目标类型是类型 A 的扁平子集加上源类型中的附加标量属性。 public class A { public int
Automapper 是否可以将平面对象映射到复杂的对象图? Mapper.CreateMap() 将 PersonDto.BirthCertificateFatherName 映射到 Person
我有类似 this. 的问题但是这个答案对我不起作用。 我正在做一个小项目。我有两个域模型(Post,Source): public class Post { public Post()
假设我在“消息”类上有一个“评论”属性。我还有 2 个类属性,它们具有“Body”属性。如果该类设置了任一类属性,我希望 AutoMapper 将 Body 属性投影到模型的 comment 属性中,
我是 AutoMapper 的新手,有一个我正在尝试解决的问题。 如果我有这样的源类: public class Membership { public int MembershipId {
我有一个场景,我想忽略基类中定义的类的某些属性。 我有一个这样的初始映射 Mapper.CreateMap() .Include()
如何覆盖 AutoMapper 用于给定属性的类型转换器? 例如,如果我有: public class Foo { public string Name { get; set; } } pub
我正在尝试使用 AutoMapper 将三个实体模型映射到一个 View 模型中。最终输出应该是一个递归类别树,其中包含类别中的产品。类别树正在运行,但 View 模型的 Products 属性为 n
我有一个 Student目的: public class Student { public int Id { get; set; } public string FirstName {
我有一个复杂的对象,如: public class BusinessUnit { public TradingDesk TradingDesk { get; } pub
假设我有这个类: public class Account { public int AccountID { get; set; } public Enterprise Enterpr
从概念上我发现 Automapper 非常有趣。然而,我正试图在上面烧伤(或加热)我的手指。有人可以帮我开始吗?我还不明白我可以从哪里开始。我会喜欢从头开始写一些代码(而不是使用其他人的样本)然后去做
我一直在尝试创建一个自动映射器自定义值解析器,但我似乎错过了一些设置步骤,因为它似乎永远找不到 public abstract class ValueResolver : IValueResolve
我正在尝试让 AutoMapper 为我们在 View 模型上本地化所有 DateTime 属性。我们在系统中的任何地方都使用 UTC 并将所有内容以 UTC 存储在数据库中,但我们希望自动将其转换为
我知道是 AutoMapper而不是 AutoMerge(r),而是…… 我已经开始使用 AutoMapper 并且需要映射 A -> B,并从 C 添加一些属性,以便 B 成为一种 A + C 的平
鉴于这些类,我如何映射它们的字典? public class TestClass { public string Name { get; set; } } public class TestC
我想自定义 AutoMapper 转换类型的方式,而不丢失 AutoMapper 已实现的功能。 我可以创建一个自定义 ITypeConverter实例,但我不知道如何调用默认行为。 Mapper.C
从存储过程返回的数据有 3 列重复数据: Name | Address | PhoneNumber | UniqueCol1 | UniqueCol2 理想情况下,我希望我的模型通过仅存储一次值并收集
当我使用 List 属性映射对象时,默认情况下 Automapper 会将目标对象的 list 属性设置为源对象的实例。 有没有办法让自动映射器创建一个新列表并复制项目但不复制列表实例? 我希望通过以
如果我有这组源类怎么办: namespace Source { class CA { public CB B { get; set; } } class
我是一名优秀的程序员,十分优秀!