返回映射属性的问题(.Net Core + Automapper Extensions 6.1)

时间:2019-05-22 18:22:16

标签: c# automapper automapping .net-core-2.1 automapper-6

具有输入模型和输出模型。两者都包含一个属性-AddressModel的集合。映射后-返回的结果仅包含AddressModel值的值,而主Output属性为空白(空)。我在做什么错了?

namespace FormsApi.Model
{
public class MessageInputModel
    {
        public Guid? ReferenceKey { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }
        public bool IsHtml { get; set; }
        public DateTime? ScheduledDate { get; set; }
        public List<AddressModel> AddressList { get; set; }
    }
}
namespace Communication.Model
{
public class AddressModel  : IAddressModel
    {
        public long PKey { get; set; }
        public int MessageId { get; set; }
        public string EmailAddress { get; set; }
        public string DisplayName { get; set; }
        public AddressSectionEnum SectionType { get; set; }
    }  
}
namespace Communication_Interfaces
{
public interface IAddressModel
    {
        long PKey { get; set; }
        int MessageId { get; set; }
        string EmailAddress { get; set; }
        string DisplayName { get; set; }
        AddressSectionEnum SectionType { get; set; }
    }
}
namespace Communication.Model
{
 public class MessageModel : BaseModel, IMessageModel
    {
        public Guid? ReferenceKey { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }
        public bool IsHtml { get; set; }
        public DateTime? ScheduledDate { get; set; }
        public IEnumerable<IAddressModel> AddressList { get; set; }
    }
}

AutoMapper配置文件:

    public class MappingCommunication : Profile
    {
        public MappingCommunication()
        {
            CreateMap<MessageInputModel, MessageModel>(); 
            //.ForMember(dest => dest.Body, opt => opt.MapFrom(src => src.Body))
            //.ForMember(dest => dest.CampaignKey, opt => opt.MapFrom(src => src.CampaignKey))
            //.ForMember(dest => dest.CampaignName, opt => opt.MapFrom(src => src.CampaignName))
            //.ForMember(dest => dest.IsHtml, opt => opt.MapFrom(src => src.IsHtml))
            //.ForMember(dest => dest.ReferenceKey, opt => opt.MapFrom(src => src.ReferenceKey))
            //.ForMember(dest => dest.ScheduledDate, opt => opt.MapFrom(src => src.ScheduledDate))
            //.ForMember(dest => dest.Subject, opt => opt.MapFrom(src => src.Subject))
            //.ForMember(dest => dest.AddressList, opt => opt.MapFrom(src => src.AddressList))
            //;
            CreateMap<MessageModel, MessageInputModel>();
            //.ForMember(dest => dest.Body, opt => opt.MapFrom(src => src.Body))
            //.ForMember(dest => dest.CampaignKey, opt => opt.MapFrom(src => src.CampaignKey))
            //.ForMember(dest => dest.CampaignName, opt => opt.MapFrom(src => src.CampaignName))
            //.ForMember(dest => dest.IsHtml, opt => opt.MapFrom(src => src.IsHtml))
            //.ForMember(dest => dest.ReferenceKey, opt => opt.MapFrom(src => src.ReferenceKey))
            //.ForMember(dest => dest.ScheduledDate, opt => opt.MapFrom(src => src.ScheduledDate))
            //.ForMember(dest => dest.Subject, opt => opt.MapFrom(src => src.Subject))
            //.ForMember(dest => dest.AddressList, opt => opt.MapFrom(src => src.AddressList))
            //;

            CreateMap<MessageModel, IMessageModel>().ConvertUsing(s => s);
            CreateMap<AddressModel, IAddressModel>().ConvertUsing(s => s);
            CreateMap<List<AddressModel>, IEnumerable<IAddressModel>>().ConvertUsing(s => s);
}
}

Startup.cs

var mappingConfig = new MapperConfiguration(mc =>
            {
                mc.Mappers.Clear();
                mc.ValidateInlineMaps = false;
                mc.AddProfile(new MappingCommunication());
                mc.AllowNullCollections = true;
                mc.AllowNullDestinationValues = true;
                mc.CreateMissingTypeMaps = true;
            });

            IMapper mapper = mappingConfig.CreateMapper();
            services.AddSingleton(mapper);
            services.AddTransient<ICommunicationProvider, CommunicationProvider>();

致电

public async Task<JsonResult> SendMessage([FromBody] MessageInputModel model)
{
.....
IMessageModel newModel = _mapper.Map<MessageModel>(model);
.....
}

这是命令窗口的结果,其中“ AddressList”属性包含两个完全填充的项目,其他类属性为null。

如何使它们正确填充?为什么不映射它们?

>? _mapper.Map<MessageModel>(model)
{Communication.Model.MessageModel}
    AddressList: Count = 2
    Body: null
    IsHtml: false
    ReferenceKey: {00000000-0000-0000-0000-000000000000}
    ScheduledDate: {5/14/2019 8:01:25 PM}
    Subject: null
    Success: false
>
>? _mapper.Map<MessageModel>(model).AddressList
Count = 2
    [0]: {Communication.Model.AddressModel}
    [1]: {Communication.Model.AddressModel}

我现在的解决方法如下...但是使用Mapper的意义何在呢...

public async Task<JsonResult> SendMessage([FromBody] MessageInputModel model)
        {


                IMessageModel newModel = new MessageModel();

// ------ start using MAPPER ONCE, as only works for AddressList property ------
                newModel.AddressList = _mapper.Map<MessageModel>(model).AddressList; 
// ------ end using MAPPER  ------

                newModel.Body = model.Body;
                newModel.IsHtml = model.IsHtml;
                newModel.ReferenceKey = model.ReferenceKey;
                newModel.ScheduledDate = model.ScheduledDate;
                newModel.Subject = model.Subject;

                SendCommandArgs args = new SendCommandArgs
                {
                    Model = newModel
                };

                await Task.Run(() => {
                    cmd.ExecuteCommand(args);
                });
            }
....

1 个答案:

答案 0 :(得分:0)

删除mc.Mappers.Clear();

var mappingConfig = new MapperConfiguration(mc =>
            {
                //mc.Mappers.Clear();
                mc.ValidateInlineMaps = false;
                mc.AddProfile(new MappingCommunication());
                mc.AllowNullCollections = true;
                mc.AllowNullDestinationValues = true;
                mc.CreateMissingTypeMaps = true;
            });