简体   繁体   English

Automapper如果source为null,则设置目标对象属性

[英]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.2AutoMapper 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.

相关问题 AutoMapper:如果指定类型的源对象为null,则将目标对象的所有属性设置为默认值 - AutoMapper: Set all properties of destination object to default if source object is null for specified types AutoMapper 根据源属性将目标设置为 null - AutoMapper set destination to null on condition of source property AutoMapper将属性设置为目标对象上的null - AutoMapper settings properties to null on destination object AutoMapper - 当源属性不可用时,将目标属性设置为 null - AutoMapper - set a destination property to null when source property is not available 如何使用自动映射器有条件地将目标对象设置为null - How do I conditionally set the destination object to null using automapper 自动映射器:将类型X的属性从源对象映射到目标对象,并将等值属性映射为类型X - Automapper: Map property of type X from source object to destination object with equivlanet properties to type X 当源为 null 时,如何设置 Automapper 全局选项以允许目标值为 null - How to set up Automapper global options to allow destination values to be null when source is null AutoMapper将一个源对象转换为多个目标对象 - AutoMapper one source object to multiple destination objects Automapper - 多对象源和一个目标 - Automapper - Multi object source and one destination 如何告诉Automapper检查所有源属性是否都具有目标属性 - How to tell Automapper to check if all source properties have destination properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM