0

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
}

The property values differ only by underscore and cases. I need to map these two classes.

For now I am trying something like this and its working:

//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 of AutoMapper, but I din't have clear idea of how i am going to use that or where is the option of ignoring cases or underscores in it. or is there any better solution to it?

Kamlesh Arya
  • 4,864
  • 3
  • 21
  • 28
  • 2
    You could read a tutorial on AutoMapper or read the documentation, it's quite straight forward and for more complex examples, the documentation even shows how to accomplish more advanced configuration. – Ric Sep 08 '15 at 13:24
  • 3
    What exactly is your problem with your current approach? It works, doesn´t it? However an alternative would also be to create some kind of conversion-method in any of your classes or to define a user-cast. – MakePeaceGreatAgain Sep 08 '15 at 13:25
  • Are you doing any of this with ASP.NET ? It's quite simple then. – Antoine Pelletier Sep 08 '15 at 13:25
  • Honestly I don't know anything about Automapper but I just remembered that I once saw this article, maybe it helps you decided for/against AutoMapper: http://www.uglybugger.org/software/post/friends_dont_let_friends_use_automapper – Verena Haunschmid Sep 08 '15 at 13:27
  • @HimBromBeere.yeah my current approach is working. But can we do that in a better way.? as I can have many fields in these classes – Kamlesh Arya Sep 08 '15 at 13:29
  • I did automapping with Visual Studio. It works fine and i never ever want to do it another way now that i know how. – Antoine Pelletier Sep 08 '15 at 13:32
  • 1
    And here is a global way of mapping with underscores: http://stackoverflow.com/questions/1630012/automapper-mapping (globally not per profile) – Ric Sep 08 '15 at 13:33

2 Answers2

3

Your code is fine and works as expected.

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

Basically the major issue is that your code will silently fail at runtime if you rename some property on your foo1 object without modifying your foo2 object.

ken2k
  • 48,145
  • 10
  • 116
  • 176
  • 2
    Yeah _"Don't use AutoMapper, it's easy to get wrong"_ is a bit of a stretch. Using one way or the other you need unit tests to test mapping both ways anyway, as with the manual way it's just as easy to forget a property or make a copy-paste error. – CodeCaster Sep 08 '15 at 13:46
  • @CodeCaster Fair point, but I would say that's personal preferences. I personally always prefer (if possible) my code to not compile at all if some refactoring gone wrong, instead of expecting some unit test to fail (this doesn't prevent from writing unit tests, but I prefer my compilation to fail before even calling them). – ken2k Sep 08 '15 at 13:56
  • If you forgot to map the property that you now renamed, it'll still compile. :) – CodeCaster Sep 08 '15 at 13:56
  • 1
    @CodeCaster Sure, that's why unit tests are still important as you noted :) But failing to rename both properties would still compile with automapper, in the other hand it wouldn't compile with manual mapping. – ken2k Sep 08 '15 at 13:58
  • thanks @ken2k for your suggestion. i decided to continue with my approach .:) – Kamlesh Arya Sep 09 '15 at 19:07
1

As @ken2k answer, I suggest you to don't use an object mapper.

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

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);
Nicolas HENAUX
  • 1,656
  • 1
  • 14
  • 18