解析 ctor 参数时如何使用值解析器
我有一个没有属性设置器但具有参数化构造函数的实体:
public class Unit
{
public int Id { get; }
public Player Owner { get; }
public Unit(int id, Player owner)
{
Id = id;
Owner = owner;
}
}
我还有一个用于自动应用程序的自定义值解析器,该解析器通过其ID找到播放器:
public class UnitOwnerResolver : IValueResolver<UnitDto, Unit, Player>
{
private readonly IPlayerService m_playerService;
public UnitOwnerResolver(IPlayerService playerService)
{
m_playerService = playerService;
}
public Player Resolve(UnitDto source, Unit destination, Player destinationMember, ResolutionContext context)
{
return m_playerService.GetPlayer(source.OwnerId);
}
}
问题是,我无法为该实体创建适当的映射配置文件。这就是我要做的:
CreateMap<UnitDto, Unit>()
.ForCtorParam("id", options => options.MapFrom(unit => unit.Id))
.ForCtorParam("owner", options => options.MapFrom<UnitOwnerResolver>();
第三行会产生错误,因为iCtorparamConfigurationepration.mapfrom
将值分解器:
没有用于“ mapfrom”的超负荷取0参数
我期望它能像formember
方法一样工作,其中有这样的过载:
有人可以建议如何建议您如何建议您如何建议我可以使用AutoMapper,CTOR映射和价值解析器创建实体实例?当然,我可以创建一个工厂,但是如果可能的话,我想坚持映射以在整个应用程序中保留一种方法。
谢谢。
I have an entity which has no property setters but has a parameterized constructor:
public class Unit
{
public int Id { get; }
public Player Owner { get; }
public Unit(int id, Player owner)
{
Id = id;
Owner = owner;
}
}
I also have a custom value resolver for AutoMapper which finds a player by its Id:
public class UnitOwnerResolver : IValueResolver<UnitDto, Unit, Player>
{
private readonly IPlayerService m_playerService;
public UnitOwnerResolver(IPlayerService playerService)
{
m_playerService = playerService;
}
public Player Resolve(UnitDto source, Unit destination, Player destinationMember, ResolutionContext context)
{
return m_playerService.GetPlayer(source.OwnerId);
}
}
Problem is, I can not create a proper mapping profile for this entity. This is what I'm trying to do:
CreateMap<UnitDto, Unit>()
.ForCtorParam("id", options => options.MapFrom(unit => unit.Id))
.ForCtorParam("owner", options => options.MapFrom<UnitOwnerResolver>();
The third line produces an error, as there is no overload for the ICtorParamConfigurationExpression.MapFrom
method taking the value resolver:
No overload for method 'MapFrom' takes 0 arguments
I'm expecting it to work like it does with the ForMember
method where there IS such overload:
Can someone please suggest how I can create an instance of the entity using AutoMapper, ctor mapping and value resolvers? I can create a factory, of course, but if it is possible, I would like to stick to mapping to preserve a single approach throughout the application.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新:解决到Automapper 11的解决方案。必要的
选项
属性已在Automapper 12中私有化。迁移指南。您可以用一些黑客攻击。您必须使用
servicector
func解决自己的服务,可以通过分辨率上下文options
:UPDATE: Solution working up to AutoMapper 11. Required
Options
property has been made private in AutoMapper 12. Migration guide.You can do it with a little bit of hacking. You'll have to resolve to service yourself using
ServiceCtor
func that can be accessed via resolution contextOptions
: