6

我开始实现AutoMapper,首先我设法将它与我已经在使用的 Castle.Windsor 集成。现在我有一个Post实体,我想映射到 aLinkPostModel或 a ImagePostModel。两者都继承自PostModel

1)这是我到目前为止所拥有的:

public class PostModelFromPostEntityConverter : ITypeConverter<Post, PostModel>
{
    private readonly IPostService postService;

    public PostModelFromPostEntityConverter(IPostService postService)
    {
        if (postService == null)
        {
            throw new ArgumentNullException("postService");
        }
        this.postService = postService;
    }

    public PostModel Convert(ResolutionContext context)
    {
        Post post = (Post)context.SourceValue;
        Link link = post.Link;
        if (link.Type == LinkType.Html)
        {
            return new LinkPostModel
            {
                Description = link.Description,
                PictureUrl = link.Picture,
                PostId = post.Id,
                PostSlug = postService.GetTitleSlug(post),
                Timestamp = post.Created,
                Title = link.Title,
                UserMessage = post.UserMessage,
                UserDisplayName = post.User.DisplayName
            };
        }
        else if (link.Type == LinkType.Image)
        {
            return new ImagePostModel
            {
                PictureUrl = link.Picture,
                PostId = post.Id,
                PostSlug = postService.GetTitleSlug(post),
                Timestamp = post.Created,
                UserMessage = post.UserMessage,
                UserDisplayName = post.User.DisplayName
            };
        }
        return null;
    }
}

显然,实现AutoMapper的重点是删除这样的重复代码,那么在添加自定义规则(例如 if 子句)之前,我应该如何映射常见的东西

理想情况下,我希望它是这样的:

public class PostModelFromPostEntityConverter : ITypeConverter<Post, PostModel>
{
    [...]

    public PostModel Convert(ResolutionContext context)
    {
        Post post = (Post)context.SourceValue;
        Link link = post.Link;
        if (link.Type == LinkType.Html)
        {
            return Mapper.Map<Post, LinkPostModel>(post);
            // and a few ForMember calls?
        }
        else if (link.Type == LinkType.Image)
        {
            return Mapper.Map<Post, ImagePostModel>(post);
            // and a few ForMember calls?
        }
        return null;
    }
}

2)映射完成后。我有一个“父”映射,我需要在其中映射IEnumerable<Post>以下模型:

public class PostListModel : IHasOpenGraphMetadata
{
    public OpenGraphModel OpenGraph { get; set; } // og:model just describes the latest post
    public IList<PostModel> Posts { get; set; }
}

所以基本上我需要另一个TypeConverter (对吗?),它允许我首先映射帖子列表,然后创建og:model

我有这个,但感觉有点笨拙,我觉得它可能会更好:

public class PostListModelFromPostEntityEnumerableConverter : ITypeConverter<IEnumerable<Post>, PostListModel>
{
    public PostListModel Convert(ResolutionContext context)
    {
        IEnumerable<Post> posts = (IEnumerable<Post>)context.SourceValue;
        PostListModel result = new PostListModel
        {
            Posts = posts.Select(Mapper.Map<Post, PostModel>).ToList()
        };
        Post first = posts.FirstOrDefault();
        result.OpenGraph = Mapper.Map<Post, OpenGraphModel>(first);
        return result;
    }
}

3)我还没有真正运行代码,所以我想到了另一个问题,那就是为什么在转换器中没有强类型化映射?

IEnumerable<Post> posts = (IEnumerable<Post>)context.SourceValue;

它实际上可能在哪里

IEnumerable<Post> posts = context.SourceValue;
4

1 回答 1

1

试图获得死灵法师徽章。
如今,通过使用功能特定字段可以更轻松地解决此任务,ConstructUsing应在提供的操作中填写,但所有公共字段都将用于ForMember执行映射。在这种情况下,集合不需要任何额外的逻辑/映射配置。也具有类型集合属性的类。

cfg.CreateMap<Post, PostModel>()
    .ConstructUsing(p =>
    {
        switch (p.Type)
        {
            case LinkType.Html: return new LinkPostModel
            {
                Title = p.Description
                // other specific fields
            };
            case LinkType.Image: return new ImagePostModel
            {
                // other specific fields
            };
        }
        return null;
    })
    .ForMember(x => x.PostId, m => m.MapFrom(p => p.Id));
cfg.CreateMap<PostList, PostListModel>();
于 2017-09-24T19:31:41.037 回答