开发者

automapper, Can I auto assign Guid to UserId of my UserDTO?

开发者 https://www.devze.com 2023-01-27 12:58 出处:网络
I want to assign Guid to UserId of my UserDTO if UserId is Null. Is it possible to do this in the CreateMap or creating any formatter?. I am using automapper as an attribute on my Actions in controlle

I want to assign Guid to UserId of my UserDTO if UserId is Null. Is it possible to do this in the CreateMap or creating any formatter?. I am using automapper as an attribute on my Actions in controller.

protected override void Configure()
        {
            Mapper.CreateMap<User, UserDT开发者_开发问答O>()
                .ForMember(d => d.FullName, o => o.MapFrom(s => s.FirstName + " " + s.LastName));
        }


Why not just:

public class UserDTO
{
    string _userId;
    public string UserId
    {
        get
        {
            if (_userId == null)
                _userId = new Guid().ToString();

            return _userId;
        }
        set { _userId = value; }

    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消