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);
}
Polymorphic element types in collectionsAutoMapper supports polymorphic arrays and collections, such that derived source/destination types are used if found.
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.
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.
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.
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()));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With