我正在测试automapper以查看它是否可以在其他项目中执行我需要的操作,因此我在控制台项目中创建了一些对象,简化但应该具备我需要的对象:
public class Person
{
public Person(List<Banco> bancos)
{
this._Bancos = bancos;
}
public int Id { get; set; }
public string Nombre { get; set; }
public DNI Dni { get; set; }
private List<Banco> _Bancos;
public List<Banco> Bancos
{
get { return _Bancos; }
}
}
public class DNI
{
private DNI() { }
public DNI(DNI2 letra)
{
this._Letra = letra;
Console.WriteLine("****DNI CONSTRUCTOR****");
}
public int Id { get; set; }
public int PersonId { get; set; }
public string number { get; set; }
private DNI2 _Letra;
public DNI2 Letra
{
get { return _Letra; }
}
}
public class DNI2
{
public int Id { get; set; }
public int DNIId { get; set; }
public string letra { get; set; }
}
public class Banco
{
public int Id { get; set; }
public string number { get; set; }
}
如您所见,对象Person
和DNI
有一个自定义的cosntructor,我将最后一个默认的无参数构造函数设置为private。
现在,我需要使用动态对象中的自定义构造函数映射嵌套对象,因此我在同一控制台项目中进行了此操作:
class Program
{
static void Main(string[] args)
{
//OJO TODO: ¿Si un objeto dinamico tiene propiedades de varios tipos, es capaz de diferenciar a qué tipo pertenece cada prop?
//¿qué pasa si un valor del dinamico es del tipo equivocado?
var bancoConfig = new MapperConfiguration(cfg => cfg.CreateMissingTypeMaps = true);
var DNI2Config = new MapperConfiguration(cfg => cfg.CreateMissingTypeMaps = true);
DNI2Config.AssertConfigurationIsValid();
var bancoMapper = bancoConfig.CreateMapper();
var dni2Mapper = DNI2Config.CreateMapper();
var dni2DBConfig = new MapperConfiguration(cfg =>
{
cfg.CreateMissingTypeMaps = true;
cfg.RecognizePrefixes("DNI2");
});
var dni2DBMapper = dni2DBConfig.CreateMapper();
var DNIConfig = new MapperConfiguration(cfg =>
{
cfg.CreateMap<dynamic, DNI>()
.ConstructUsing(dyn => new DNI(dni2DBMapper.Map<DNI2>(dyn)));
/*.ForMember(dest => dest.Id, opts => opts.MapFrom<int>(src => (int)(src as IDictionary<string, object>)["Id"]))
.ForMember(dest => dest.number, opts => opts.MapFrom<string>(src => (string)(src as IDictionary<string, object>)["number"]))
.ForMember(dest => dest.PersonId, opts => opts.MapFrom<int>(src => (int)(src as IDictionary<string, object>)["PersonId"]));*/
/*cfg.CreateMap<IDictionary<string, object>, DNI>()
.ConstructUsing(dyn => new DNI(dni2DBMapper.Map<DNI2>(dyn as dynamic)))
.ForMember(dest => dest.Id, opts => opts.MapFrom<int>(src => (int)(src["Id"])))
.ForMember(dest => dest.number, opts => opts.MapFrom<string>(src => (string)(src["number"])))
.ForMember(dest => dest.PersonId, opts => opts.MapFrom<int>(src => (int)(src["PersonId"])));*/
cfg.CreateMissingTypeMaps = true;
});
var dniMapper = DNIConfig.CreateMapper();
dynamic d = new { Id = 0, number = "1234" };
Banco banco = bancoMapper.Map<Banco>(d);
Console.WriteLine("Banco: " + Environment.NewLine);
Console.WriteLine(banco.Id + Environment.NewLine + banco.number);
Console.ReadLine();
d = new { Id = 1, DNIId = 0, letra = "a" };
DNI2 dni2 = dni2Mapper.Map<DNI2>(d);
Console.WriteLine("DNI2: " + Environment.NewLine);
Console.WriteLine(dni2.Id + Environment.NewLine + dni2.DNIId + Environment.NewLine + dni2.letra);
Console.ReadLine();
d = new { Id = 2, PersonId = 5, number = "5678", DNI2Id = 2, DNIId = 2, letra = "b" };
DNI2 d2 = dni2DBMapper.Map<DNI2>(d);
DNI dni = dniMapper.Map<DNI>(d);
Console.WriteLine("DNI: " + Environment.NewLine);
Console.WriteLine(dni.Id + Environment.NewLine + dni.PersonId + Environment.NewLine + dni.number + Environment.NewLine);
Console.WriteLine("dni.dni2: "+dni.Letra.Id + Environment.NewLine + dni.Letra.DNIId + Environment.NewLine + dni.Letra.letra);
Console.ReadLine();
}
}
这会在控制台中显示此结果:
Banco:
0
1234
DNI2:
1
0
a
****DNI CONSTRUCTOR****
DNI:
2
5
5678
dni.dni2: 0
0
因此,它只是创建DNI
对象并设置成员,它通过公共构造函数...但即使它通过构造函数它也没有设置DNI2
属性。
如果我在DNI
配置中修改动态线:
var DNIConfig = new MapperConfiguration(cfg =>
{
cfg.CreateMap<dynamic, DNI>()
.ConstructUsing(dyn => new DNI(dni2DBMapper.Map<DNI2>(dyn)));
/*.ForMember(dest => dest.Id, opts => opts.MapFrom<int>(src => (int)(src as IDictionary<string, object>)["Id"]))
.ForMember(dest => dest.number, opts => opts.MapFrom<string>(src => (string)(src as IDictionary<string, object>)["number"]))
.ForMember(dest => dest.PersonId, opts => opts.MapFrom<int>(src => (int)(src as IDictionary<string, object>)["PersonId"]));*/
/*cfg.CreateMap<IDictionary<string, object>, DNI>()
.ConstructUsing(dyn => new DNI(dni2DBMapper.Map<DNI2>(dyn as dynamic)))
.ForMember(dest => dest.Id, opts => opts.MapFrom<int>(src => (int)(src["Id"])))
.ForMember(dest => dest.number, opts => opts.MapFrom<string>(src => (string)(src["number"])))
.ForMember(dest => dest.PersonId, opts => opts.MapFrom<int>(src => (int)(src["PersonId"])));*/
//cfg.CreateMissingTypeMaps = true;
});
...所以它没有配置使其动态化,结果是:
Banco:
0
1234
DNI2:
1
0
a
****DNI CONSTRUCTOR****
DNI:
0
0
dni.dni2: 2
2
b
配置中的注释行只是我尝试过的一部分,包括在这里搜索两天和谷歌。
似乎该程序告诉我:你使用自定义构造函数从动态或映射它,你不能同时执行它们,吮吸它。
对我来说最有趣的部分是:使用第一个配置,它不是映射DNI2
对象,而是使用构造函数映射DNI
对象(该行在控制台),但它没有参数所需的对象,所以它使用的构造函数没有必需的参数 O_o
老实说,我知道我是一个业余爱好者,但我总是认为即使使用反射你也不能使用构造函数(没有任何其他方法)而没有任何必需的参数。
所以:有没有办法用动态对象使用自定义构造函数映射嵌套对象?
答案 0 :(得分:0)
我找到了解决方法:
var DNIDynConfig = new MapperConfiguration(cfg => cfg.CreateMissingTypeMaps = true);
var dniDynMapper = DNIDynConfig.CreateMapper();
var DNIConfig = new MapperConfiguration(cfg =>
{
cfg.CreateMap<dynamic, DNI>()
.ConstructUsing(dyn => new DNI(dni2DBMapper.Map<DNI2>(dyn)))
.AfterMap((src, dest) =>
{
dniDynMapper.Map(src, dest, typeof(object), typeof(DNI));
});
});
我真的不喜欢它,因为它似乎必须将对象映射两次,但它有效。如果有人带来更好的东西,我会觉得很有意思。