10

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

2 Answers 2

13

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
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?
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..
i am trying the same example and getting this Exception ..|Missing type map configuration or unsupported mapping.|
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.
1

you can use modelMapper

@Autowired
private ModelMapper modelMapper;

and in function

Desination desination = modelMapper.map(obj1, Desination.class);
modelMapper.map(obj2, desination);

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.