Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practices for mapping one object to another

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!

like image 705
Alex Avatar asked Apr 20 '13 07:04

Alex


People also ask

Is AutoMapper fast?

Automapper is slower when mapping a single object and collections but there is one exception. Automapper is considerably faster when mapping a List<T> of objects on . NET Core (It's still slower on full . NET Framework).

What is a mapping object?

A mapping object maps values of one type (the key type) to arbitrary objects. Mappings are mutable objects. There is currently only one mapping type, the dictionary . A dictionary's keys are almost arbitrary values.

Why do we use mapping in C#?

AutoMapper in C# is a library used to map data from one object to another. It acts as a mapper between two objects and transforms one object type into another. It converts the input object of one type to the output object of another type until the latter type follows or maintains the conventions of AutoMapper.


1 Answers

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.

like image 115
Efran Cobisi Avatar answered Sep 22 '22 23:09

Efran Cobisi