【问题标题】:Fill with new data along with remain existing data map with Automapper使用 Automapper 填充新数据并保留现有数据映射
【发布时间】:2022-12-18 12:58:57
【问题描述】:

我正在实施更新用户端点。在更新期间,我正在获取原始实体。

public class UserEntity
{
    public Guid Id { get; set; }

    public Dictionary<string, IEnumerable<string>> Attributes { get; set; }

}
var user = await _userService.GetByIdAsync(request.Id);

然后我有一个更新请求

public class UpdateUserRequest
{
    public string? MiddleName { get; set; }
    public string? Phone { get; set; }
}

MiddleNamePhone 应作为属性存储在此处。

_mapper.Map(request, user);

CreateMap<UpdateUserRequest, UserEntity>().ForMember(dst => dst.Attributes, opt => opt.UseDestinationValue());

我正在使用目标值来保留原始属性,因为如果不这样做,它们将被清空。

我保留原始属性并放置新属性的解决方案有点脏,我的问题是:我可以在 Automapper 的帮助下处理它吗?

我的解决方案:

void SetAttributes()
        {
            if(!string.IsNullOrEmpty(request.MiddleName))
            {
                if(!user.Attributes.TryGetValue(Constants.Attributes.MiddleName, out var middleName))
                {
                    user.Attributes.Add(Constants.Attributes.MiddleName, new string[]
                    {
                        request.MiddleName
                    });
                }
                else
                {
                    user.Attributes[Constants.Attributes.MiddleName] = new string[]
                    {
                        request.MiddleName
                    };
                }
            }

            if(!string.IsNullOrEmpty(request.Phone))
            {
                if(!user.Attributes.TryGetValue(Constants.Attributes.PhoneNumber, out var firstName))
                {
                    user.Attributes.Add(Constants.Attributes.PhoneNumber, new string[]
                    {
                        request.Phone
                    });
                }
                else
                {
                    user.Attributes[Constants.Attributes.PhoneNumber] = new string[]
                    {
                        request.Phone
                    };
                }
            }
        }

【问题讨论】:

    标签: c# mapping automapper automapper-12


    【解决方案1】:

    AfterMap() automapper 方法解决了这个问题。

     .AfterMap((request, user) =>
                                                        {
                                                            if(!string.IsNullOrEmpty(request.FirstName))
                                                            {
                                                                user.FirstName = request.FirstName;
                                                            }
    
                                                            if(!string.IsNullOrEmpty(request.LastName))
                                                            {
                                                                user.LastName = request.LastName;
                                                            }
    
                                                            if(!string.IsNullOrEmpty(request.Email))
                                                            {
                                                                user.Email = request.Email;
                                                            }
    
                                                            if(!string.IsNullOrEmpty(request.MiddleName))
                                                            {
                                                                if(!user.Attributes.TryGetValue(KeycloakConstants.Attributes.MiddleName, out _))
                                                                {
                                                                    user.Attributes.Add(KeycloakConstants.Attributes.MiddleName, new string[]
                                                                    {
                                                                        request.MiddleName
                                                                    });
                                                                }
                                                                else
                                                                {
                                                                    user.Attributes[KeycloakConstants.Attributes.MiddleName] = new string[]
                                                                    {
                                                                        request.MiddleName
                                                                    };
                                                                }
                                                            }
    
                                                            if(!string.IsNullOrEmpty(request.Phone))
                                                            {
                                                                if(!user.Attributes.TryGetValue(KeycloakConstants.Attributes.PhoneNumber,
                                                                                                out _))
                                                                {
                                                                    user.Attributes.Add(KeycloakConstants.Attributes.PhoneNumber, new string[]
                                                                    {
                                                                        request.Phone
                                                                    });
                                                                }
                                                                else
                                                                {
                                                                    user.Attributes[KeycloakConstants.Attributes.PhoneNumber] = new string[]
                                                                    {
                                                                        request.Phone
                                                                    };
                                                                }
                                                            }
                                                        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-14
      • 2019-09-17
      • 1970-01-01
      • 2011-04-18
      • 1970-01-01
      • 2014-10-21
      • 1970-01-01
      相关资源
      最近更新 更多