Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automapper custom resolvers

I am sorry for my simple question but I can't get it to work . My problem: I want to map from domain Object to Model Object but, In domain object I have a list so my object look like:

public class Conference
 {
   public virtual  int Id{get;set;}
   public virtual int  NumberOfTables{get;set;}
   public virtual IList<People> Peoples{get;set;}
 }

public class People
 {
   public virtual  int Id{get;set;}
   public virtual string FirstName{get;set;}
   public virtual string LastName{get;set;}
   public virtual  Conference Conference{get;set;}
 }

My model :


public class Model
{
  public int Id{get;set;}
  public int NumberOfTables{get;set;}
  public string Peoples{get;set;}
}

I want People to be like :"FirstName"+"LastName"+"," for all people from list Now on mapping from Domain Obj To Model I have something like this:

Mapper.CreateMap<Conference,Model>()
     .ForMember(c => c.Id, op => op.MapFrom(v => v.Id))
     .ForMember(c => c.NumberOfTables, op => op.MapFrom(v => v.NumberOfTables))
      .ForMember(c => c.Peoples, op => op//here I want to use ResolveUsing or something like this  )



  public class CustomConvert: ValueResolver<IList<GroupOfComponentInComplexToMeal>, string>
    {
        protected override string ResolveCore(IList<People> source)
        {
            string result = "";
            if (source.Any())
            {
                for (int i = 0; i < source.Count; i++)
                {
                    var name=source[i].FirstName+source[i].LastName;
                    result += name;
                    if (i < source.Count - 1)
                    {
                        result += ",";
                    }
                }
            }
            return result;
        }
    }

I did this CustomConvert but I can't merge it with my mapper.

I really don't now if I need to use Custom converters but I want to learn how they work. Any help will be appreciated.

like image 281
Nic Avatar asked Feb 25 '14 15:02

Nic


People also ask

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 can I use instead of AutoMapper?

I've found ValueInjecter the best out of the automapper, emitmapper, and a few others on codeplex. I choose ValueInjector because it's the most flexible of them all.

Does AutoMapper map private fields?

By default, AutoMapper only recognizes public members. It can map to private setters, but will skip internal/private methods and properties if the entire property is private/internal.


1 Answers

It was really simple to get it to work but I still need some good examples to learn more about Custom resolvers.

Solution:

Mapper.CreateMap<Conference,Model>()
    .ForMember(c => c.Id, op => op.MapFrom(v => v.Id))
    .ForMember(c => c.NumberOfTables, op => op.MapFrom(v => v.NumberOfTables))
    .ForMember(c => 
        c.Peoples, op => op.ResolveUsing<CustomConvert>().FromMember(x => x.Peoples)
    );
like image 194
Nic Avatar answered Sep 22 '22 13:09

Nic