Mapping two classes in C #

I have two classes

public class foo1 { public int id; public string image_link; public string sale_price; } 

and

 public class foo2 { public int Id; public string ImageLink; public string SalePrice } 

Property values ​​differ only in underlining and cases. I need to match these two classes.

At the moment I'm trying something like this and its work:

 //var b = object of foo2 var a = new foo1{ a.id = b.Id, a.image_link = b.ImageLink, a.sale_price = b.SalePrice } 

I heard about AutoMapper, but I don’t have a clear idea of ​​how I will use it or where cases or underscores in it can be ignored. or is there a better solution?

0
source share
2 answers

Your code is fine and works as expected.

I would advise you not to use automapper. There are many explanations about why on the Internet, for example, one: http://www.uglybugger.org/software/post/friends_dont_let_friends_use_automapper

Basically, the main problem is that your code will run foo1 at runtime if you rename a property to your foo1 object without changing your foo2 object.

+2
source

As @ ken2k's answer, I suggest you not use the mapper object.

If you want to save the code, you can simply create a new method (or directly in the constructor) for display.

 public class foo1 { public int id; public string image_link; public string sale_price; public void map(foo2 obj) { this.id = obj.Id; this.image_link = obj.ImageLink; this.sale_price = obj.SalePrice; } } 

Then

 //var b = object of foo2 var a = new foo1(); a.map(b); 
+1
source

Source: https://habr.com/ru/post/1234708/


All Articles