[英]Automapper If source is null, set destination object properties
I have the following case: 我有以下情况:
Source : 资料来源 :
public class AccountLogin
{
public int UserID { get; set; }
public bool AccountVerified { get; set; }
}
This object is returned from the database, so if no user is found it could be null 该对象是从数据库返回的,因此如果找不到用户,则该对象可能为null
Destination : 目的地 :
public class LoginUserResponseModel
{
public bool AccountExists { get; set; }
public bool AccountVerified { get; set; }
public string Status { get; set; }
public string Token { get; set; }
}
What do I need? 我需要什么?
If the source is null, I need an instance of the destination object with the following parameters: 如果源为null,则需要具有以下参数的目标对象的实例:
{
"AccountExists": false,
"AccountVerified": false
"Status": "Error"
"Token": null
}
The AutoMapper
code I put into my MappingProfile.cs
file is as follows: 我放入
MappingProfile.cs
文件中的AutoMapper
代码如下:
CreateMap<AccountLogin, LoginUserResponseModel>()
.ForMember(dest => dest.AccountExists, opt => opt.MapFrom(src => src == null ? false : true))
.ForMember(dest => dest.Status, opt => opt.MapFrom(src => src == null ? "Error" : "Ok"));
This code works fine when the object is not null, it sets Status = "Ok"
and AccountExists = false
当对象不为null时,此代码可以正常工作,它设置
Status = "Ok"
且AccountExists = false
When I have received existing user from the database, my source, AccountLogin
, eg: 当我从数据库接收到现有用户时,我的源
AccountLogin
,例如:
So this maps correctly like this: 因此,这正确映射如下:
The problem is when the source is null, the destination is also null: 问题是当源为空时,目标也为空:
I have a lot of workarounds, but I am really curious if this could be made with AutoMapper
. 我有很多解决方法,但是我真的很好奇是否可以使用
AutoMapper
。
The project I am using is ASP Net Core 2.2
and AutoMapper v6.1.1
我正在使用的项目是
ASP Net Core 2.2
和AutoMapper v6.1.1
In response to your comments I would suggest using a CustomTypeConverter: http://docs.automapper.org/en/stable/Custom-type-converters.html 为了回应您的意见,我建议使用CustomTypeConverter: http ://docs.automapper.org/en/stable/Custom-type-converters.html
public class AccountLoginConverter : ITypeConverter<AccountLogin, LoginUserResponseModel>
{
public LoginUserResponseModel Convert(AccountLogin source, LoginUserResponseModel destination, ResolutionContext context)
{
if(source == null)
{
return new LoginUserResponseModel { AccountExists = false, Status = "Error" }
}
// You can have more complex logic here
return new LoginUserResponseModel
{
AccountExists = true,
AccountVerified = true, // Or more logic
Status = "Ok"
};
}
}
In order to set up with your newly designed converter: 为了设置新设计的转换器:
cfg.CreateMap<AccountLogin, LoginUserResponseModel>().ConvertUsing(new AccountLoginConverter());
or 要么
cfg.CreateMap<AccountLogin, LoginUserResponseModel>().ConvertUsing<AccountLoginConverter>();
You could try ConvertUsing with a custom ITypeConverter: 您可以尝试使用自定义ITypeConverter进行ConvertUsing:
public class MyConverter : ITypeConverter<AccountLogin, LoginUserResponseModel >
{
public int Convert(AccountLogin source, LoginUserResponseModel destination, ResolutionContext context)
{
return new LoginUserResponseModel{
AccountExists = source == null ? false : true,
AccountVerified = false,
Status = source == null ? "Error" : "Ok",
"Token": null
};
}
}
Then map it with: 然后用:
CreateMap<AccountLogin, LoginUserResponseModel>().ConvertUsing(new MyConverter());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.