views:

281

answers:

3

Hi! Have this scenario:

Public class Base {  public string Name; }

Public Class ClassA :Base {  public int32 Number;  }

Public Class ClassB :Base { Public string Description;}

Public Class DTO {
  public string Name;
  public int32 Number;
  Public string Description;
}

I have an IList<Base> my maps are:

AutoMapper.Mapper.CreateMap<IList<Base>, IList<DTO>>()
   .ForMember(dest => dest.Number, opt => opt.Ignore())
   .ForMember(dest => dest.Description, opt => opt.Ignore());

AutoMapper.Mapper.CreateMap<ClassA, DTo>()
   .ForMember(dest => dest.Description, opt => opt.Ignore());

AutoMapper.Mapper.CreateMap<ClassB, DTO>()
   .ForMember(dest => dest.Number, opt => opt.Ignore())

Mapper.AssertConfigurationIsValid(); //Is OK!

But Properties that are in ClassA Or ClassB are not mapped when I do this :

IList<DTO>= AutoMapper.Mapper.Map<IList<Base>,IList<DTO>>(baseList);

How can I do to map properties that are defined in ClasA and ClassB

Thanks

+1  A: 

you do

Mapper.Create<ClasA, DTO>();
Mapper.Create<ClasB, DTO>();
Omu
I think you'll also need the "Ignore" method for the missing members for configuration validation to work properly as well.
Jimmy Bogard
I added the ignore methods to the maps. But I still can´t map properties that are in ClassA or ClassB
Gringo
A: 

I did this to solve the problem

IList<DTO> list1 = AutoMapper.Mapper.Map<IList<ClassA>,IList<DTO>>(baseList.OfType<ClassA>().ToList());

IList<DTO> list2 = AutoMapper.Mapper.Map<IList<ClassB>,IList<DTO>>(baseList.OfType<ClassB>().ToList());

list = list1.Union(list2);

persons.OfType<T>().ToList()

Must be a better way to do this.

Gringo
A: 

You will need to create DTO classes that match your domain classes like this:

public class DTO
{
    public string Name;
}

public class DTO_A : DTO
{
    public int Number { get; set; }
}

public class DTO_B : DTO
{
    public string Description { get; set; }
}

You then need to change your mappings to this:

        Mapper.CreateMap<Base, DTO>()
            .Include<ClassA, DTO_A>()
            .Include<ClassB, DTO_B>();

        Mapper.CreateMap<ClassA, DTO_A>();

        Mapper.CreateMap<ClassB, DTO_B>();

        Mapper.AssertConfigurationIsValid();

Once this is done, then the following will work:

        var baseList = new List<Base>
        {
            new Base {Name = "Base"},
            new ClassA {Name = "ClassA", Number = 1},
            new ClassB {Name = "ClassB", Description = "Desc"},
        };

        var test = Mapper.Map<IList<Base>,IList<DTO>>(baseList);
        Console.WriteLine(test[0].Name);
        Console.WriteLine(test[1].Name);
        Console.WriteLine(((DTO_A)test[1]).Number);
        Console.WriteLine(test[2].Name);
        Console.WriteLine(((DTO_B)test[2]).Description);
        Console.ReadLine();

Unfortunately this does mean that you have an unwanted cast, but I don't think there's much that you can do about that.

Simon
Thank you! Nice solution
Gringo
No worries Gringo, glad it helped.
Simon