Mapping generally from an IEnumerable<Entity> to items property in summary DTO?

30 views
Skip to first unread message

jetm...@gmail.com

unread,
Aug 2, 2019, 6:10:18 PM8/2/19
to AutoMapper-users
So let's see if this makes sense.  I've got a Web API project which has services and repositories.  A service queries a repository for entities, and the repository returns a list of entities.  That list is then mapped onto Detail DTO's and set as the items property of a Summary DTO which is then returned by the service (which was called by a controller or whatever other code might be using it).

I'd like to create a mapping that can tell AutoMapper how to take a given list of entities of any type, and map that list onto the items property of that outgoing DTO.  All the entities are inheriting the common class ModifiableEntitiy, all the Summary DTO's implement the interface IEntitySummaryDTO<IEntityDetailDTO>, and all the Detail DTO's implement IEntityDetailDTO.  The way I have my DTO's working is, single entities are placed into a Detail DTO for that entity, and a list of entities is converted to Detail DTO's and then set as the items property of a Summary DTO which currently just contains the list of Details but could be extended to include other meta info.  Here's the mapping I tried:

CreateMap<IEnumerable<ModifiableEntity>, IEntitySummaryDTO<IEntityDetailDTO>>()
                .ForMember(dest => dest.items, opt => opt.MapFrom(src => src));


This compiles and everything but doesn't appear to actually achieve the mapping that I'm hoping for.  If I write the mapping out with the actual type names for a given entity and its accompanying Detail and Summary DTO's, then the mapping works.  But I'd like to avoid having to write that mapping for every entity.  I'm certain it can be done generally, I just don't know what I'm doing wrong.  Thanks for any help you can provide.
Reply all
Reply to author
Forward
0 new messages