简体   繁体   中英

How to map single object properties to array of object in complex type using automapper

I got class structure like follows, and I've created mapping structure, but it is not working as expected.

public class ResponseHeader
{
    public ResHeader ResHeader { get; set; }  
}

public class ResHeader
{
    public ServiceResStatus ServiceResStatus { get; set; }

    public Error[] Errors { get; set; }
}

public class Error
{
    public string ErrorCode { get; set; }

    public string ErrorDesc { get; set; }

    public string Source { get; set; }
}

public class ServiceResStatus
{
    public string ServiceResCode { get; set; }

    public string ServiceResDesc { get; set; }

    public System.DateTime ServiceRespDateTime { get; set; }

    public string ServiceUniqueRefNo { get; set; }
}


public class ExceptionInformation
{
    public string Code { get; set; }
    public string Description { get; set; }
    public string Source { get; set; }  
}

I want to map ExceptionInformation properties to ResponseHeader. I always get ResponseHeader dto is null.

var config = new MapperConfiguration(cfg =>
        {

            cfg.CreateMap<ExceptionInformation, ResponseHeader>();


            cfg.CreateMap<ExceptionInformation, ResHeader>()
                .ForMember(d => d.Errors, opts => opts.ResolveUsing(src => new Error() {ErrorCode = src.Code}))
                .ForMember(d => d.Errors, opts => opts.ResolveUsing(src => new Error() {ErrorDesc = src.Description}))
                .ForMember(d => d.Errors, opts => opts.ResolveUsing(src => new Error() {Source = src.Code}))
                .ForMember(d => d.ServiceResStatus, opts => opts.Ignore());

            cfg.CreateMap<ExceptionInformation, ServiceResStatus>()
                .ForMember(d => d.ServiceResCode, opts => opts.MapFrom(src => src.Code))
                .ForMember(d => d.ServiceResDesc, opts => opts.MapFrom(src => src.Description))
                .ForMember(d => d.ServiceRespDateTime, opts => opts.MapFrom(src => DateTime.Now))
                .ForMember(d => d.ServiceUniqueRefNo, opts => opts.Ignore());

        });


        var mapper = config.CreateMapper();

        var info = new ExceptionInformation()
        {
            Code = "ERR01",
            Description = "Error",
            Source = "Oven"

        };


        ResponseHeader dto = mapper.Map<ResponseHeader>(info);

any working solution appreciated.

I came up with this solution to resolve your issue. Here is the code snippet

public class ResponseHeader
{
    public ResHeader ResHeader { get; set; }
}

public class ResHeader
{
    public ServiceResStatus ServiceResStatus { get; set; }

    public Error[] Errors { get; set; }
}

public class Error
{
    public string Code { get; set; }

    public string Description { get; set; }

    public string Source { get; set; }
}

public class ServiceResStatus
{
    public string ServiceResCode { get; set; }

    public string ServiceResDesc { get; set; }

    public System.DateTime ServiceRespDateTime { get; set; }

    public string ServiceUniqueRefNo { get; set; }
}


public class ExceptionInformation
{
    public string Code { get; set; }
    public string Description { get; set; }
    public string Source { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<ExceptionInformation, Error>();

            cfg.CreateMap<ExceptionInformation, ResHeader>()
                .ForMember(d => d.Errors, opts => opts.MapFrom(src => new[] {src}))
                .ForMember(d => d.ServiceResStatus, opts => opts.Ignore());

            cfg.CreateMap<ExceptionInformation, ServiceResStatus>()
                .ForMember(d => d.ServiceResCode, opts => opts.MapFrom(src => src.Code))
                .ForMember(d => d.ServiceResDesc, opts => opts.MapFrom(src => src.Description))
                .ForMember(d => d.ServiceRespDateTime, opts => opts.MapFrom(src => DateTime.Now))
                .ForMember(d => d.ServiceUniqueRefNo, opts => opts.Ignore());

            cfg.CreateMap<ExceptionInformation, ServiceResStatus>()
                .ForMember(d => d.ServiceResCode, opts => opts.MapFrom(src => src.Code))
                .ForMember(d => d.ServiceResDesc, opts => opts.MapFrom(src => src.Description))
                .ForMember(d => d.ServiceRespDateTime, opts => opts.MapFrom(src => DateTime.Now))
                .ForMember(d => d.ServiceUniqueRefNo, opts => opts.Ignore());

            cfg.CreateMap<ExceptionInformation, ResponseHeader>()
                .ForMember(des => des.ResHeader, opt => opt.MapFrom(src => new ResHeader()
                {
                    Errors = new Error[1] { new Error() {Code = src.Code, Description = src.Description, Source = src.Source }},
                    ServiceResStatus = new ServiceResStatus() { ServiceResCode = src.Code, ServiceResDesc = src.Description, ServiceRespDateTime = DateTime.Now, ServiceUniqueRefNo = null}
                }));
        });


        var mapper = config.CreateMapper();

        var info = new ExceptionInformation()
        {
            Code = "ERR01",
            Description = "Error",
            Source = "Oven"

        };

        var dto1 = mapper.Map<ExceptionInformation, Error>(info);
        var dto2 = mapper.Map<ExceptionInformation, ResHeader>(info);
        var dto3 = mapper.Map<ExceptionInformation, ServiceResStatus>(info); 
        var dto4 = mapper.Map<ExceptionInformation, ResponseHeader>(info);

        //Put a check point in cole readline and verify objects dto1, dto2, dto3 and dto4
        Console.ReadLine();
    }
}

}`

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