My question is: what is the best way I can map one object to another in the most convenient way. I cannot change the way in which the Dto object we get is more normalized, so I need to create a way to map this to our implementation of their object.
Here is a sample code to show what I need:
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 it would be great if I had some kind of matching class that would take in the model propertyInfo objects, the type I want to convert, and the "itemname" I want to pull out. Does anyone have any suggestions to make this cleaner?
Thank!
c # design-patterns mapping
Alex Apr 20 '13 at 7:57 2013-04-20 07:57
source share