解析 ctor 参数时如何使用值解析器

发布于 2025-01-20 22:09:56 字数 1465 浏览 0 评论 0原文

我有一个没有属性设置器但具有参数化构造函数的实体:

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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那请放手 2025-01-27 22:09:56

更新:解决到Automapper 11的解决方案。必要的选项属性已在Automapper 12中私有化。迁移指南。


您可以用一些黑客攻击。您必须使用servicector func解决自己的服务,可以通过分辨率上下文options

config.CreateMap<UnitDto, Unit>()
    .ForCtorParam("id", options => options.MapFrom(unit => unit.OwnerId))
    .ForCtorParam("owner", options => options.MapFrom((unitDto, context) =>
    {
        var playerService = context.Options.ServiceCtor(typeof(IPlayerService)) as IPlayerService;

        return playerService.GetPlayer(unitDto.OwnerId);
    }));

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 context Options:

config.CreateMap<UnitDto, Unit>()
    .ForCtorParam("id", options => options.MapFrom(unit => unit.OwnerId))
    .ForCtorParam("owner", options => options.MapFrom((unitDto, context) =>
    {
        var playerService = context.Options.ServiceCtor(typeof(IPlayerService)) as IPlayerService;

        return playerService.GetPlayer(unitDto.OwnerId);
    }));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文