lets have an DTO class:
public class UserDto {
public int Id { get;set; }
}
And viewmodel class (base mvc project library):
public abstract class UserViewModelBase {
public int Id { get;set; }
}
now when i create mapping for automapper like this:
Mapper.CreateMap<UserDto, UserViewModelBase>();
in main web mvc project i have concreate implementation of UserViewModelBase, like this:
public class UserViewModel: UserViewModelBase {
}
Now when i call this:
Mapper.Map<UserDto, UserViewModel>()
it fails with such error:
Missing type map configuration or unsupported mapping.
UserDto -> UserViewModel
As i understand that this was caused that i havent defined direct mapping between UserDto and UserViewModel.
I want to know if there is some way how to tell AutoMapper that with this map Mapper.CreateMap<UserDto, UserViewModelBase>();
it should accept any type as destination type which inherits from UserViewModelBase
.
Seems no one knows how to achieve this so i made workaround for this.
My workaround was to build mappings (using Mapper.CreateMap(...)
) multiple times each with concreate target class like:
in ClassLibrary1:
Mapper.CreateMap<UserDto, UserViewModel1>();
in second ClassLibrary2:
Mapper.CreateMap<UserDto, UserViewModel2>();
even if UserViewModel1 and UserViewModel2 is derived from same base class UserViewModelBase
.