使用 Automapper 检查条件
Checking conditions using Automapper
我正在使用 Automapper。在那里,我将 DTO 映射到数据库 table。在那一个中,我需要检查一个条件然后取值。
CreatedBy = mapper.Map<UserProperties>((from createdByUser in context.persons.Where(x => x.IsActive && x.Id == notes.CreatedBy) select createdByUser).FirstOrDefault())
这是我的代码。
用户属性Class:
public string DisplayName { get; set; }
public int Id { get; set; }
public bool IsUser { get; set; }
public int NotesCount {get;set;}
人
public string DisplayName { get; set; }
public int Id { get; set; }
public int RoleId{ get; set; }
public int NotesCount {get;set;}
public string Notes{get;set;}
public string Comments {get;set;}
下面的代码是启动文件中的automapper配置。
映射配置文件Class
在人中,有字段roleId
。我需要通过检查 Persons 中的 RoleId
字段等于 2.
等条件来为用户属性 class 中的 IsUser
字段分配值
如何使用自动映射器检查条件?
Automapper 版本:9.0.0
您需要向映射中添加一个 ForMember
子句以添加条件 - 这是一个有效的示例(花费的时间比应有的时间长,因为您 post 编辑了代码的图像而不是实际代码。这就是为什么在 SO 上你应该总是 post 代码,而不是图像。)
void Main()
{
var mapperConfig =
new MapperConfiguration(mc => mc.AddProfile<MappingProfile>());
var mapper = mapperConfig.CreateMapper();
var notAUser = new Persons { RoleId = 1};
var isAUser = new Persons { RoleId = 2};
var shouldBeNotAUser = mapper.Map<UserProperties>(notAUser);
var shouldBeAUser = mapper.Map<UserProperties>(isAUser);
Console.WriteLine(shouldBeNotAUser.IsUser);
Console.WriteLine(shouldBeAUser.IsUser);
}
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<Persons, UserProperties>()
.ForMember(destination => destination.IsUser,
options => options.MapFrom(src => src.RoleId == 2));
}
}
class UserProperties
{
public string DisplayName { get; set; }
public int Id { get; set; }
public bool IsUser { get; set; }
public int NotesCount { get; set; }
}
class Persons
{
public string DisplayName { get; set; }
public int Id { get; set; }
public int RoleId { get; set; }
public int NotesCount { get; set; }
public string Notes { get; set; }
public string Comments { get; set; }
}
输出:
False
True
然而
您的映射配置代码不应该 'know' 关于 RoleID 指示用户的内容。你的 Person
class 应该是保存知识的地方,所以它应该有一个 IsUser()
方法或一个只获取 IsUser
属性 的方法(带有 NotMapped
属性)其中 returns RoleId == 2
:在前一种情况下你仍然需要 ForMember
但在后一种情况下你不需要,尽管如果你 从 UserProperties
映射回 Persons
你需要一些东西来处理它 - 同样,这应该在 Persons
class 而不是映射器中配置。也许 SetAsUser()
设置 RoleId。
我正在使用 Automapper。在那里,我将 DTO 映射到数据库 table。在那一个中,我需要检查一个条件然后取值。
CreatedBy = mapper.Map<UserProperties>((from createdByUser in context.persons.Where(x => x.IsActive && x.Id == notes.CreatedBy) select createdByUser).FirstOrDefault())
这是我的代码。
用户属性Class:
public string DisplayName { get; set; }
public int Id { get; set; }
public bool IsUser { get; set; }
public int NotesCount {get;set;}
人
public string DisplayName { get; set; }
public int Id { get; set; }
public int RoleId{ get; set; }
public int NotesCount {get;set;}
public string Notes{get;set;}
public string Comments {get;set;}
下面的代码是启动文件中的automapper配置。
映射配置文件Class
在人中,有字段roleId
。我需要通过检查 Persons 中的 RoleId
字段等于 2.
IsUser
字段分配值
如何使用自动映射器检查条件?
Automapper 版本:9.0.0
您需要向映射中添加一个 ForMember
子句以添加条件 - 这是一个有效的示例(花费的时间比应有的时间长,因为您 post 编辑了代码的图像而不是实际代码。这就是为什么在 SO 上你应该总是 post 代码,而不是图像。)
void Main()
{
var mapperConfig =
new MapperConfiguration(mc => mc.AddProfile<MappingProfile>());
var mapper = mapperConfig.CreateMapper();
var notAUser = new Persons { RoleId = 1};
var isAUser = new Persons { RoleId = 2};
var shouldBeNotAUser = mapper.Map<UserProperties>(notAUser);
var shouldBeAUser = mapper.Map<UserProperties>(isAUser);
Console.WriteLine(shouldBeNotAUser.IsUser);
Console.WriteLine(shouldBeAUser.IsUser);
}
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<Persons, UserProperties>()
.ForMember(destination => destination.IsUser,
options => options.MapFrom(src => src.RoleId == 2));
}
}
class UserProperties
{
public string DisplayName { get; set; }
public int Id { get; set; }
public bool IsUser { get; set; }
public int NotesCount { get; set; }
}
class Persons
{
public string DisplayName { get; set; }
public int Id { get; set; }
public int RoleId { get; set; }
public int NotesCount { get; set; }
public string Notes { get; set; }
public string Comments { get; set; }
}
输出:
False
True
然而
您的映射配置代码不应该 'know' 关于 RoleID 指示用户的内容。你的 Person
class 应该是保存知识的地方,所以它应该有一个 IsUser()
方法或一个只获取 IsUser
属性 的方法(带有 NotMapped
属性)其中 returns RoleId == 2
:在前一种情况下你仍然需要 ForMember
但在后一种情况下你不需要,尽管如果你 从 UserProperties
映射回 Persons
你需要一些东西来处理它 - 同样,这应该在 Persons
class 而不是映射器中配置。也许 SetAsUser()
设置 RoleId。