how to map multiple OBJECTS to one object using AutoMapper - asp.net mvc 3

11,638

Solution 1

In this case, are you really using multiple (types of) objects as your source? It looks like from your defined problem that your source is a list of users - judging by "i want to show List of users with their company details".

If that's the case, whilst you can't do it implicitly you can use a TypeConverter to perform the map easily enough:

Mapper.CreateMap<ICollection<User>, UserCompanyViewModel>()
      .ConvertUsing<UserCompanyViewModelConverter>();

Then define your converter as:

public class UserCompanyViewModelConverter : ITypeConverter<ICollection<User>, UserCompanyViewModel>
{
    public UserCompanyViewModel Convert(ResolutionContext context)
    {
        UserCompanyViewModel model = new UserCompanyViewModel();

        ICollection<User> sourceUsers = (ICollection<User>)context.SourceValue;

        model.Users     = sourceUsers;
        model.Companies = sourceUsers.Select(u => u.Company).Distinct().ToList();

        return model;
    }
}

Then when you want to map you just take your collection of users someUsers and map it:

UserCompanyViewModel model = Mapper.Map<ICollection<User>, UserCompanyViewModel>(someUsers);

If you really do need to map multiple source types into a single destination type, it looks like this blog post includes a short Helper class that will help you. In short, AutoMapper doesn't quite support this so you will be making a couple of Map requests to fill up your ViewModel. You will need to use another TypeConverter to make sure that the second call doesn't replace the Companies added by the first.

Solution 2

you can use modelMapper

@Autowired
private ModelMapper modelMapper;

and in function

Desination desination = modelMapper.map(obj1, Desination.class);
modelMapper.map(obj2, desination);
Share:
11,638
patel.milanb
Author by

patel.milanb

Updated on July 19, 2022

Comments

  • patel.milanb
    patel.milanb almost 2 years

    Hi All / very new to Auto-Mapper. i can map one to one objects but was wondering is it possible to map multiple objects to one object or multiple objects to multiple objects?

    consider i have a following scenario...

    User Model

    public class User
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public Company Company { get; set; }  // 1 user work in 1 company
        }
    

    Company Model

    public class Company
            {
                public string CompanyName { get; set; }
                public string Website { get; set; }
                public ICollection<User> Users { get; set; }  // 1 Company can have many users
            }
    

    UserCompanyViewModel

    i want to show List of users with their company details in one view..

    public class UserCompanyViewModel
                {
                     public ICollection<User> Users { get; set; }
                     ppublic ICollection<Company> Companies { get; set; }   
                }
    

    Now, is it possible to map in this situation and if yes, i can show in one view and when editing that view i want to map again with the updated fields back to their respective Models.

    any help would be appreciated...thx

  • patel.milanb
    patel.milanb over 12 years
    as i said, in one view i want to show a collection of users with the company details. so there will be 2 collections users, companies, so i have UserCompanyViewModel to display that.
  • Kasaku
    Kasaku over 12 years
    The ViewModel is the destination however. Your source collection, the data that you are retrieving from your store to then put into the ViewModel, right?
  • patel.milanb
    patel.milanb over 12 years
    yes right.. in this case my sources are collection of Users and Collcetion of Companies. want to put all together in destination UserCompanyVM and then display foreach user with the company details..
  • patel.milanb
    patel.milanb over 12 years
    i am trying the same example and getting this Exception ..|Missing type map configuration or unsupported mapping.|
  • Kasaku
    Kasaku over 12 years
    Make sure that CreateMap is being called? I've copied the example almost verbatim, just left out the test data I used. I've updated the answer to include a link which might help you further.