将一个对象映射到另一个对象的最佳做法 [英] Best Practices for mapping one object to another
问题描述
这里是显示我需要发生的示例代码:
class Program
{
static void Main (string [] args)
{
var dto = new Dto();
dto.Items = new object [] {1.00m,true,Three};
dto.ItemsNames = new [] {One,Two,Three};
var model = GetModel(dto);
Console.WriteLine(One:{0},model.One);
Console.WriteLine(Two:{0},model.Two);
Console.WriteLine(Three:{0},model.Three);
Console.ReadLine();
}
private static模型GetModel(Dto dto)
{
var result = new Model();
result.One = Convert.ToDecimal(dto.Items [Array.IndexOf(dto.ItemsNames,One)]);
result.Two = Convert.ToBoolean(dto.Items [Array.IndexOf(dto.ItemsNames,Two)]);
result.Three = dto.Items [Array.IndexOf(dto.ItemsNames,Three)] ToString();
返回结果;
}
}
class Dto
{
public object [] Items {get;组; }
public string [] ItemsNames {get;组; }
}
class模型
{
public decimal One {get;组; }
public bool Two {get;组; }
public string Three {get;组; }
}
我认为如果我有一些mapper类这将在模型对象propertyInfo中,我想要转换的类型,以及我要拉出的itemname。有没有人有任何建议使这个更清洁?
谢谢!
p>我会选择 AutoMapper ,一个开放源代码和免费的映射库,可以将一种类型映射到另一种类型,基于约定(即映射具有相同名称和相同/派生/可转换类型的公共属性,以及许多其他智能那些的)。很容易使用,可以让你实现这样的目的:
模型模型= Mapper.Map< Model>(dto);
不确定您的具体要求,但AutoMapper还支持自定义值解析器,这应该可以帮助您编写一个单一的,通用的特定映射器实现。
My question is, what is the best way I can map one object to another in the most maintainable manner. I cannot change the way the Dto object that we are getting is setup to be more normalized so I need to create a way to map this to our implementation of their object.
Here is example code to show what I need to happen:
class Program
{
static void Main(string[] args)
{
var dto = new Dto();
dto.Items = new object[] { 1.00m, true, "Three" };
dto.ItemsNames = new[] { "One", "Two", "Three" };
var model = GetModel(dto);
Console.WriteLine("One: {0}", model.One);
Console.WriteLine("Two: {0}", model.Two);
Console.WriteLine("Three: {0}", model.Three);
Console.ReadLine();
}
private static Model GetModel(Dto dto)
{
var result = new Model();
result.One = Convert.ToDecimal(dto.Items[Array.IndexOf(dto.ItemsNames, "One")]);
result.Two = Convert.ToBoolean(dto.Items[Array.IndexOf(dto.ItemsNames, "Two")]);
result.Three = dto.Items[Array.IndexOf(dto.ItemsNames, "Three")].ToString();
return result;
}
}
class Dto
{
public object[] Items { get; set; }
public string[] ItemsNames { get; set; }
}
class Model
{
public decimal One { get; set; }
public bool Two { get; set; }
public string Three { get; set; }
}
I think what would be great is if I had some sort of mapper class that would take in the model objects propertyInfo, the type I want to convert to, and the "itemname" I want to pull out. Does anyone have any suggestions to make this cleaner?
Thanks!
I would opt for AutoMapper, an open source and free mapping library which allows to map one type into another, based on conventions (i.e. map public properties with the same names and same/derived/convertible types, along with many other smart ones). Very easy to use, will let you achieve something like this:
Model model = Mapper.Map<Model>(dto);
Not sure about your specific requirements, but AutoMapper also supports custom value resolvers, which should help you writing a single, generic implementation of your particular mapper.
这篇关于将一个对象映射到另一个对象的最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!