0

Im trying to figure out to how to map a list to an object containing an identical list property

I have a view model that looks like this:

public class GenerateCodeVM
{
    public List<AnimalVM> Animal { get; set; }

    public List<RegionVM> Region { get; set; }
}

public class AnimalVM
{
    public int? AnimalID { get; set; }

    public string Name { get; set; }

    public string Code { get; set; }

    public DateTime? LastUpdated { get; set; }
}

public class RegionVM
{
    public int? RegionID { get; set; }

    public string RegionName { get; set; }

    public string Code { get; set; }
}

In the controller I have:

GenerateCodeVM generateCodeVM = new GenerateCodeVM();
AnimalRepository animalRepository = new AnimalRepository();
List<Animal> animal = animalRepository.GetAll().ToList();
RegionRepository regionRepository = new RegionRepository();
List<Region> region = regionRepository.GetAll().ToList();

generateCodeVM = Mapper.Map<List<Region>, GenerateCodeVM> (region);
generateCodeVM = Mapper.Map<List<Animal>, GenerateCodeVM>(animal);

How do I configure the mappings in automapper?

2 Answers 2

1

This first bit is fine

GenerateCodeVM generateCodeVM = new GenerateCodeVM();
AnimalRepository animalRepository = new AnimalRepository();
List<Animal> animal = animalRepository.GetAll().ToList();
RegionRepository regionRepository = new RegionRepository();
List<Region> region = regionRepository.GetAll().ToList();

These next to lines aren't.

generateCodeVM = Mapper.Map<List<Region>, GenerateCodeVM> (region);
generateCodeVM = Mapper.Map<List<Animal>, GenerateCodeVM>(animal);

I would merely map into the generateCodeVM object rather than to the whole thing.

So you need to mappings set up.

Mapper.CreateMap<Region, RegionVM>();
Mapper.CreateMap<Animal, AnimalVM>();

Then you can use the project extension method in AutoMapper. As long as the GetAll() is returning an IQueryable<>.

GenerateCodeVM generateCodeVM = new GenerateCodeVM();

AnimalRepository animalRepository = new AnimalRepository();
RegionRepository regionRepository = new RegionRepository();

List<Animal> animal = animalRepository.GetAll().Project().To<AnimalVM>().ToList();

List<Region> region = regionRepository.GetAll().Project().To<RegionVM>.ToList();

Then just assign these projected lists to the generateCodeVM

generateCodeVM.Animal = animal;
generateCodeVM.Region = region;

Hints and Tips

If you don't want to use Project you can simple do:

List<Region> region = regionRepository.GetAll().ToList();
var mappedRegions = Mapper.Map<List<RegionVM>>(region);

You don't need to change the CreateMap at all to support this, it is supported out of the box.

I would pluralise your variable names that refer to lists, so that it makes your code clearer to you and anyone else looking at it (e.g. us/me on SO).

List<Region> region = regionRepository.GetAll().ToList();

Should be

List<Region> regions = regionRepository.GetAll().ToList();
Sign up to request clarification or add additional context in comments.

Comments

0

This looks like a really odd solution, the last two lines overwrite the same variable. I suggest you'd replace the last two lines in the controller with

GenerateCodeVM generateCodeVM = new GenerateCodeVM();
generateCodeVM.Animal = Mapper.Map<List<AnimalVM>>(animal);
generateCodeVM.Region = Mapper.Map<List<RegionVM>>(region);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.