Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
    }
like image 425
Smokefoot Avatar asked Jul 21 '11 19:07

Smokefoot


People also ask

Can AutoMapper map collections?

Polymorphic element types in collectionsAutoMapper supports polymorphic arrays and collections, such that derived source/destination types are used if found.

How do I use AutoMapper to list a map?

Once you have your types, and a reference to AutoMapper, you can create a map for the two types. Mapper. CreateMap<Order, OrderDto>(); The type on the left is the source type, and the type on the right is the destination type.

When should you not use AutoMapper?

If you have to do complex mapping behavior, it might be better to avoid using AutoMapper for that scenario. Reverse mapping can get very complicated very quickly, and unless it's very simple, you can have business logic showing up in mapping configuration.

What is the use of AutoMapper in MVC?

AutoMapper is an object-object mapper that allows you to solve the problem of manually mapping each property of a class with the same properties of another class. Before AutoMapper was introduced if we wanted to assign one object property to another object property then we were following a long procedure.


1 Answers

A unit test validated the following would map from IList<Tag> to IList<string>

  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:

.ForMember(dest => dest.Tags, opt => opt.MapFrom(so => so.Tags.Select(t=>t.Name).ToList()));
like image 73
Ed Charbeneau Avatar answered Sep 18 '22 15:09

Ed Charbeneau