Combining multiple sources into a single output

9,988 views
Skip to first unread message

David Strickland

unread,
Nov 16, 2010, 1:07:27 PM11/16/10
to AutoMapper-users
I have a Flat destination class and a complex source containing
multiple child objects
Currently I create multiple maps with the same output type but
different input types.

ie
Mapper.CreateMap(Of destinationType, Source1Type)
Mapper.CreateMap(Of destinationType, Source1Type.SubClass1)
Mapper.CreateMap(Of destinationType, Source1Type.SubClass2)

Then in code I call each sequentially. wrapped in a if not source is
null.

The instantiation of the destination requires all 3 source objects so
I'd like to be able to create 1 map that ensures all 3 are called.

I tried some different options but I typically hit a wall when
SourceType1.Subclass1 == null.

any suggestions

Thanks
Dave

Jimmy Bogard

unread,
Nov 21, 2010, 6:15:16 PM11/21/10
to automapp...@googlegroups.com
AutoMapper is designed to map a single source object to a single destination object.  If you're trying to do something different, it probably won't work.  Your best bet is to shape the input into a single object.  We've tried looking at this feature, but the problem is that collisions are possible, defeating the purpose.

Thanks,

Jimmy


--
You received this message because you are subscribed to the Google Groups "AutoMapper-users" group.
To post to this group, send email to automapp...@googlegroups.com.
To unsubscribe from this group, send email to automapper-use...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/automapper-users?hl=en.


jdsteve...@gmail.com

unread,
Aug 22, 2017, 2:44:10 PM8/22/17
to AutoMapper-users
So what is the recommended practice for a situation where a ViewModel needs to support a drop down?

AddressViewModel
string Street1
string City
int StateID
List<StateViewModel> AvailableStates

mapped from:

Address
string Street1
string City
int StateID

and a separate call to a repository to get a list of available states? Should I just map the returned States collection to a StateViewModels colelction and then assign the AvailableStates property directly? I just want to be sure there's nothing fluent built in.

Jimmy Bogard

unread,
Aug 25, 2017, 2:14:29 PM8/25/17
to automapper-users
Ohhhh good question!

What is the nature of the list of items? Where does it come from?

To unsubscribe from this group and stop receiving emails from it, send an email to automapper-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jdsteve...@gmail.com

unread,
Aug 25, 2017, 3:09:17 PM8/25/17
to AutoMapper-users
Well in my case, it comes from a repository that could either be retrieving it from cache or ultimately a local database. It could potentially also be retrieved from a web service by a repository. Here's what I have currently and I really have no complaints with it:

        public static PersonEditVM GetAutoMappedPersonEditVM(Guid personID)
        {
            IMapper mapper = MapperConfig.EntityWebMapper;

            Person person = personID == Guid.Empty ? new Person() : CrmRepository.GetPersonWithContactInfo(personID);


            PersonEditVM vm = mapper.Map<PersonEditVM>(person);

            vm.AvailableStates = mapper.Map<List<StateModel>>(CrmRepository.GetStates())
                                    .OrderBy(s => s.Name)
                                    .ToList();

            vm.AvailablePhoneTypes = mapper.Map<List<PhoneTypeModel>>(CrmRepository.GetPhoneTypes())
                                        .OrderBy(pt => pt.Name)
                                        .ToList();

            return vm;
        }

I'm brand new to AutoMapper...I had my own very basic implementation going and then realized I kept hearing about yours (which looks awesome!)...so apologies if I'm wasting your time on the basics. Thanks for your help!
To unsubscribe from this group and stop receiving emails from it, send an email to automapper-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Powell

unread,
Aug 25, 2017, 6:53:04 PM8/25/17
to AutoMapper-users


On Tuesday, November 16, 2010 at 1:07:27 PM UTC-5, David Strickland wrote:
I have a Flat destination class and a complex source containing
multiple child objects
Currently I create multiple maps with the same output type but
different input types.

AFAIK, and I could be wrong since 4.2.x, but IMapper.Map<...>(...) supports not only creating the destination object, but also supports operating on an existing instance. So in the case of mapping multiple sources, I would follow that paradigm. I've done that in a couple of places.

Now, mind you, one thing to be cautious of is what to do over collisions if you happen to have overlapping or competing source/destination scenarios. But then, perhaps conditions may work well in your case to decide what/when to allow the mapping to continue.

HTH
Reply all
Reply to author
Forward
0 new messages