简体   繁体   中英

Issue with returning mapped properties (.Net Core + Automapper Extensions 6.1)

Have Input model and Output model. Both contain a property - collection of AddressModel. After mapping - the returned result only contains values for AddressModel values, while main Output properties are blank (null). What am I doing wrong?

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 profile:

    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>();

When calling

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

here is the result from command window, where a property "AddressList" contains two fully filled items and other class properties are = null.

How make them populated properly? why are they not being mapped?

>? _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}

my work around for now is below... but whats the point using Mapper then... ugh!

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);
                });
            }
....

Remove 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;
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM