如何使用Automapper映射此 [英] how do I map this using Automapper

查看:146
本文介绍了如何使用Automapper映射此的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要绘制以下场景.

public class Customer
{
    public string CustomerJson { get; set; }
}

public class CustomerTO
{
     public object CustomerJson { get; set; }
}

从DAL中获得如下的CustomerJson值.

From DAL I get CustomerJson value as below.

Customer.CustomerJson = {
    "name": "Ram",
    "city": "India"
}

我需要反序列化此字符串.所以我在映射时尝试了以下内容.

I am in need to Deserialize this string. so I tried the below stuff while mapping.

var config = new MapperConfiguration(cfg =>
                    {
                        cfg.CreateMap<Customer, CustomerTO>()
                        .ForMember(dest => dest.CustName, opt => opt.MapFrom(src => JsonConvert.DeserializeObject(src.CustName)));
                    });

但这给了我运行时错误.

But this gives me run time error.

未处理的异常:AutoMapper.AutoMapperMappingException:错误映射类型.

Unhandled Exception: AutoMapper.AutoMapperMappingException: Error mapping types.

所以我保持简单.

var config = new MapperConfiguration(cfg =>
                    {
                        cfg.CreateMap<Customer, CustomerTO>()
                        .ForMember(dest => dest.CustName, opt => opt.MapFrom(src => (src.CustName));
                    });

我尝试在消费时反序列化它.但这会导致编译时错误.

And I tried to deserialize it while consuming. But this give compile time error.

var custJson = JsonConvert.DeserializeObject(customerTO.CustomerJson );

错误2'Newtonsoft.Json.JsonConvert.DeserializeObject(string)'的最佳重载方法匹配具有一些无效参数

Error 2 The best overloaded method match for 'Newtonsoft.Json.JsonConvert.DeserializeObject(string)' has some invalid arguments

我知道customerTO.CustomerJson不是字符串,但是我应该如何进行所需的映射?

I know customerTO.CustomerJson is not string but how do should I do the required mapping?

谢谢.

推荐答案

基于您的

Based on your previous question and given the information above you seem to be confusing what you're trying to do here.

因此,我将合并两者的数据,以解决问题.

So I'm going to amalgamate the data from both in an attempt to solve the issues.

实体类:

public class Customer
{
    public int CustomerId {get; set; }
    public string CustomerName { get; set; }
    public string CustomerJson { get; set; }
}

public class CustomerTO
{
    public int CustId { get; set; }
    public object CustData { get; set; }
    public object CustomerJson { get; set; }
}

AppMapper类:

public static class AppMapper
{
    public static MapperConfiguration Mapping()
    {
        return new MapperConfiguration(cfg =>
            {
                cfg.CreateMap<Customer, CustomerTO>()
                .ForMember(dest => dest.CustId, opt => opt.MapFrom(src => src.CustomerId))
                .ForMember(dest => dest.CustData, opt => opt.MapFrom(src => src.CustName))
                .ForMember(dest => dest.CustomerJson, opt => opt.MapFrom(src => JsonConvert.DeserializeObject(src.CustomerJson));
            });
    }
}

主要

public class Program
{
    static void Main(string[] args)
    {
        var config = AppMapper.Mapping();
        var mapper = config.CreateMapper();

        // From Previous question get list of Customer objects
        var customers = AddCustomers();
        var mappedCustomers = mapper.Map<IEnumerable<CustomerTO>>(customers);
    }
}

需要指出的几件事 我不确定CustomerTOCustData的目的是什么.它似乎在复制CustomerJson,如果是的话,将其删除并删除关联的映射.

A couple of things to point out I'm not sure what the purpose of CustData is in CustomerTO. It seems to be duplicating CustomerJson and if so remove it and the associated mapping.

此外,您永远不会提到从DTO映射回实体的情况,但是对于JsonObject,您只需要配置它即可将序列化的字符串映射到适当的Property.

Also, you never mention mapping from DTO back to entity, but for the JsonObject you just need to configure it to map the serialized string to the appropriate Property.

这篇关于如何使用Automapper映射此的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆