简体   繁体   English

AutoMapper将对象映射到任何继承的目标类型

[英]AutoMapper map object to any inherited destination type

lets have an DTO class: 让我们有一个DTO类:

public class UserDto {
  public int Id { get;set; }
}

And viewmodel class (base mvc project library): 和viewmodel类(基本的mvc项目库):

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: 在主要的Web MVC项目中,我已经创建了UserViewModelBase的实现,如下所示:

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 UserDto-> UserViewModel

As i understand that this was caused that i havent defined direct mapping between UserDto and UserViewModel. 据我了解,这是由于我没有在UserDto和UserViewModel之间定义直接映射而引起的。 I want to know if there is some way how to tell AutoMapper that with this map Mapper.CreateMap<UserDto, UserViewModelBase>(); 我想知道是否有某种方法可以告诉AutoMapper该地图Mapper.CreateMap<UserDto, UserViewModelBase>(); it should accept any type as destination type which inherits from UserViewModelBase . 它应该接受从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: 我的解决方法是多次构建映射(使用Mapper.CreateMap(...) ),每次都使用Mapper.CreateMap(...)目标类,例如:

in ClassLibrary1: 在ClassLibrary1中:

Mapper.CreateMap<UserDto, UserViewModel1>();

in second ClassLibrary2: 在第二个ClassLibrary2中:

Mapper.CreateMap<UserDto, UserViewModel2>();

even if UserViewModel1 and UserViewModel2 is derived from same base class UserViewModelBase . 即使UserViewModel1和UserViewModel2是从相同的基类UserViewModelBase

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 自动映射器将继承的对象映射到子对象 - Automapper map an inherited object to a child 如何使用自动映射器将对象映射到未知的目标类型? - How can I use Automapper to map an object to an unknown destination type? Automapper继承的源和目标 - Automapper Inherited Source and Destination 自动映射器:将类型X的属性从源对象映射到目标对象,并将等值属性映射为类型X - Automapper: Map property of type X from source object to destination object with equivlanet properties to type X Automapper 创建目标类型 object,其中没有任何值 - Automapper create destination type object with no values inside 如何使用AutoMapper将目标对象与源对象中的子对象进行映射? - How to use AutoMapper to map destination object with a child object in the source object? 如何使用automapper将源类型的Parent映射到目标类型的集合? - How to use automapper to map Parent of source type into a collection of destination type? AutoMapper将源对象上的单个列表映射到目标对象上的两个列表 - AutoMapper Map single list on source object into two lists on destination object A类型的自动映射器将源属性映射到目标列表 - Automapper map source property of type A to destination List<B> 仅当目标 object 在 Automapper 中没有值时,如何 map - how to map only if destination object doesn't have a value in Automapper
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM