AutoMapper - 将两个外部 API 结果映射到一个 DTO 对象中
AutoMapper - Map two external API results into one DTO object
我有两个外部端点,因此我需要合并到一个 DTO 对象中。
我在某个时候迷路了,因为我试图找出一种方法,当 Album UserId[= 之间只有匹配项时,我如何才能注入专辑集合29=] 和 UserDetailDTO Id.
端点 1 - https://jsonplaceholder.typicode.com/users
端点 2 - https://jsonplaceholder.typicode.com/albums
下面你可以找到我的 类:
public class UserDetailDTO
{
public int Id { get; set; }
public string Name { get; set; }
public string Username { get; set; }
public List<Album> Albums { get; set; }
}
public class User
{
[JsonProperty("id", NullValueHandling = NullValueHandling.Ignore)]
public int Id { get; set; }
[JsonProperty("name", NullValueHandling = NullValueHandling.Ignore)]
public string Name { get; set; }
[JsonProperty("username", NullValueHandling = NullValueHandling.Ignore)]
public string Username { get; set; }
}
public class Album
{
[JsonProperty("userId", NullValueHandling = NullValueHandling.Ignore)]
public int UserId { get; set; }
[JsonProperty("id", NullValueHandling = NullValueHandling.Ignore)]
public int Id { get; set; }
[JsonProperty("title", NullValueHandling = NullValueHandling.Ignore)]
public string Title { get; set; }
}
public MappingProfile()
{
var userMap = CreateMap<User, UserDetailDTO>();
var albumMap = CreateMap<Album, UserDetailDTO>();
}
在服务部分,我能够从这些端点获得 json 结果,但我在 AutoMapper 部分失败了。
var users = JsonConvert.DeserializeObject<List<User>>(usersResponseContent);
var albums = JsonConvert.DeserializeObject<List<Album>>(albumsResponseContent);
var mappedUsers = _mapper.Map(albums, _mapper.Map<List<User>, List<UserDetailDTO>>(users));
除了使用映射器将相册分配给 DTO,您还可以执行以下操作
var users = JsonConvert.DeserializeObject<List<User>>(usersResponseContent);
var albums = JsonConvert.DeserializeObject<List<Album>>(albumsResponseContent);
var mappedUsers = _mapper.Map<List<User>, List<UserDetailDTO>>(users);
foreach (var user in mappedUsers)
user.Albums = albums.Where(x => x.UserId == user.Id);
我有两个外部端点,因此我需要合并到一个 DTO 对象中。 我在某个时候迷路了,因为我试图找出一种方法,当 Album UserId[= 之间只有匹配项时,我如何才能注入专辑集合29=] 和 UserDetailDTO Id.
端点 1 - https://jsonplaceholder.typicode.com/users
端点 2 - https://jsonplaceholder.typicode.com/albums
下面你可以找到我的 类:
public class UserDetailDTO
{
public int Id { get; set; }
public string Name { get; set; }
public string Username { get; set; }
public List<Album> Albums { get; set; }
}
public class User
{
[JsonProperty("id", NullValueHandling = NullValueHandling.Ignore)]
public int Id { get; set; }
[JsonProperty("name", NullValueHandling = NullValueHandling.Ignore)]
public string Name { get; set; }
[JsonProperty("username", NullValueHandling = NullValueHandling.Ignore)]
public string Username { get; set; }
}
public class Album
{
[JsonProperty("userId", NullValueHandling = NullValueHandling.Ignore)]
public int UserId { get; set; }
[JsonProperty("id", NullValueHandling = NullValueHandling.Ignore)]
public int Id { get; set; }
[JsonProperty("title", NullValueHandling = NullValueHandling.Ignore)]
public string Title { get; set; }
}
public MappingProfile()
{
var userMap = CreateMap<User, UserDetailDTO>();
var albumMap = CreateMap<Album, UserDetailDTO>();
}
在服务部分,我能够从这些端点获得 json 结果,但我在 AutoMapper 部分失败了。
var users = JsonConvert.DeserializeObject<List<User>>(usersResponseContent);
var albums = JsonConvert.DeserializeObject<List<Album>>(albumsResponseContent);
var mappedUsers = _mapper.Map(albums, _mapper.Map<List<User>, List<UserDetailDTO>>(users));
除了使用映射器将相册分配给 DTO,您还可以执行以下操作
var users = JsonConvert.DeserializeObject<List<User>>(usersResponseContent);
var albums = JsonConvert.DeserializeObject<List<Album>>(albumsResponseContent);
var mappedUsers = _mapper.Map<List<User>, List<UserDetailDTO>>(users);
foreach (var user in mappedUsers)
user.Albums = albums.Where(x => x.UserId == user.Id);