AutoMapper 如何在 ForMember 方法中将一个 class 映射到另一个 class
AutoMapper How to map one class to another class in a ForMember method
所以我的项目得到了这个 class 结构:
public class SomeType {
public int Id {get; set;}
public List<Customer> Customers {get; set;}
}
public class Customer {
public int Id {get; set;}
public string FirstName {get; set;}
}
public class CustomerDto {
public int Id {get; set;}
public string FirstName {get; set;}
}
public class SomeTypeDto {
public int Id {get; set;}
public List<object> SomeValues {get; set;}
}
在我的项目中的某个时刻,我这样称呼它:
var result = _mapper.Map<SomeTypeDto>(someType);
在使用 AutoMapper 将 SomeType class 映射到 SomeTypeDto 后,是否可以将 Customer 类型的列表转换为 CustomerDto 类型的列表?我记得之前有一种方法在创建类型映射时在 MapFrom 方法中调用静态 Mapper.Map<>() 方法,但它似乎不适用于较新的版本。
您的 MapperConfiguration
/(映射)Profile
应如下所示:
MapperConfiguration
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Customer, CustomerDto>();
cfg.CreateMap<SomeType, SomeTypeDto>()
.ForMember(dest => dest.SomeValues, opt => opt.MapFrom(src => src.Customers));
});
MappingProfile
public class SomeTypeProfile : Profile
{
public SomeTypeProfile()
{
CreateMap<Customer, CustomerDto>();
CreateMap<SomeType, SomeTypeDto>()
.ForMember(dest => dest.SomeValues, opt => opt.MapFrom(src => src.Customers));
}
}
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Customer, CustomerDto>();
cfg.CreateMap<SomeType, SomeTypeDto>()
.ForMember(dest => dest.SomeValues, opt => opt.MapFrom(src => src.Customers));
});
IMapper mapper = config.CreateMapper();
var someType = new SomeType
{
Id = 1,
Customers = new List<Customer>
{
new Customer { Id = 1, FirstName = "Alfred" } ,
new Customer { Id = 2, FirstName = "Max" }
}
};
var someTypeDto = mapper.Map<SomeType, SomeTypeDto>(someType);
所以我的项目得到了这个 class 结构:
public class SomeType {
public int Id {get; set;}
public List<Customer> Customers {get; set;}
}
public class Customer {
public int Id {get; set;}
public string FirstName {get; set;}
}
public class CustomerDto {
public int Id {get; set;}
public string FirstName {get; set;}
}
public class SomeTypeDto {
public int Id {get; set;}
public List<object> SomeValues {get; set;}
}
在我的项目中的某个时刻,我这样称呼它:
var result = _mapper.Map<SomeTypeDto>(someType);
在使用 AutoMapper 将 SomeType class 映射到 SomeTypeDto 后,是否可以将 Customer 类型的列表转换为 CustomerDto 类型的列表?我记得之前有一种方法在创建类型映射时在 MapFrom 方法中调用静态 Mapper.Map<>() 方法,但它似乎不适用于较新的版本。
您的 MapperConfiguration
/(映射)Profile
应如下所示:
MapperConfiguration
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Customer, CustomerDto>();
cfg.CreateMap<SomeType, SomeTypeDto>()
.ForMember(dest => dest.SomeValues, opt => opt.MapFrom(src => src.Customers));
});
MappingProfile
public class SomeTypeProfile : Profile
{
public SomeTypeProfile()
{
CreateMap<Customer, CustomerDto>();
CreateMap<SomeType, SomeTypeDto>()
.ForMember(dest => dest.SomeValues, opt => opt.MapFrom(src => src.Customers));
}
}
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Customer, CustomerDto>();
cfg.CreateMap<SomeType, SomeTypeDto>()
.ForMember(dest => dest.SomeValues, opt => opt.MapFrom(src => src.Customers));
});
IMapper mapper = config.CreateMapper();
var someType = new SomeType
{
Id = 1,
Customers = new List<Customer>
{
new Customer { Id = 1, FirstName = "Alfred" } ,
new Customer { Id = 2, FirstName = "Max" }
}
};
var someTypeDto = mapper.Map<SomeType, SomeTypeDto>(someType);