AutoMapper: Mapping a collection of Object to a collection of strings

Viewed 14105

I need help with a special mapping with AutoMapper. I want to map a collection of objects to a collection of strings.

So I have a Tag class

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

Than in a model I have a IList of this class. Now I want to map the name's to a collection of strings.

Thats how I define the mapping rule:

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

And here is my ValueResolver:

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

But you know.. it doesn't work ;-) So maybe someone know how to do it right and can help me.

thanks a lot

Update to Jan

Sooo.. you wanted more details.. here you got it.. but I have shorten it ;)

So the 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 you can see above.

I want to map it to a ArticleView... I need the tag model only for some business context, not for the output.

So here is the ViewModel I need to map to:

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. 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

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