简体   繁体   English

AutoMapper:将 Object 的集合映射到字符串的集合

[英]AutoMapper: Mapping a collection of Object to a collection of strings

I need help with a special mapping with AutoMapper.我需要使用 AutoMapper 进行特殊映射的帮助。 I want to map a collection of objects to a collection of strings.我想将 map 对象集合转换为字符串集合。

So i have a Tag classd所以我有一个标签分类

public class Tag
{
    public Guid Id { get; set; }
    public string Name {get; set; }
}

Than in a model i have a IList of this class.比在 model 中,我有这个 class 的 IList。 Now i want to map the name's to a collection of strings.现在我想将 map 的名称添加到字符串集合中。

Thats how I define the mapping rule:这就是我定义映射规则的方式:

.ForMember(dest => dest.Tags, opt => opt.ResolveUsing<TagNameResolver>())

And here is my ValueResolver:这是我的ValueResolver:

protected override string ResolveCore(Tag source)
{
    return source.Name;
}

But u know.. it dont work;-) So maybe someone know how to do it right and can help me.但是你知道..它不起作用;-)所以也许有人知道如何正确地做它并且可以帮助我。

thx alot多谢

Jan

Sooo.. u wanted more details.. here u get it.. but i have shorten it;) Sooo ..你想要更多细节..在这里你明白了..但我缩短了它;)

So the Model:所以Model:

public class Artocle
{
    public Guid Id { get; set; }
    public string Title {get; set; }
    public string Text { get; set; }
    public IList<Tag> Tags { get; set; }
}

And the Tag model u can see above.您可以在上面看到标签 model。

I want to map it to a ArticleView... i need the tag model only for some business context, not for tthe output.我想将 map 放到 ArticleView... 我需要标签 model 仅用于某些业务环境,不适用于 output。

So here is the ViewModel i need to map to:所以这是我需要 map 到的 ViewModel:

public class ArticleView
{
    public Guid Id { get; set; }
    public string Title { get; set; }
    public string Text { get; set; }
    public IList<string> Tags { get; set; } // The mapping problem :-)
}

So I have a BootStrapper for the mappings.所以我有一个用于映射的 BootStrapper。 My Mapping looks like this:我的映射如下所示:

Mapper.CreateMap<Article, ArticleView>()
.ForMember(dest => dest.Tags, opt => opt.ResolveUsing<TagNameResolver>())

And I map it manuelly with a special method而我 map 它用特殊的方法手动

    public static ArticleView ConvertToArticleView(this Article article)
    {
        return Mapper.Map<Article, ArticleView>(article);
    }

A unit test validated the following would map from IList <Tag> to IList <string>单元测试验证了从 IList <Tag>到 IList <string>的 map

  private class TagNameResolver : ValueResolver<IList<Tag>, IList<string>>
        {
            protected override IList<string> ResolveCore(IList<Tag> source)
            {
                var tags = new List<string>();
                foreach (var tag in source)
                {
                    tags.Add(tag.Name);
                }
                return tags;
            } 
        }

This is a shorter way of creating the map:这是创建 map 的更短的方法:

.ForMember(dest => dest.Tags, opt => opt.MapFrom(so => so.Tags.Select(t=>t.Name).ToList()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM