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?

7
  • 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. Commented Sep 8, 2015 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. Commented Sep 8, 2015 at 13:25
  • Are you doing any of this with ASP.NET ? It's quite simple then. Commented Sep 8, 2015 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: uglybugger.org/software/post/… Commented Sep 8, 2015 at 13:27
  • 1
    And here is a global way of mapping with underscores: stackoverflow.com/questions/1630012/automapper-mapping (globally not per profile) Commented Sep 8, 2015 at 13:33

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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 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).
If you forgot to map the property that you now renamed, it'll still compile. :)
@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.
thanks @ken2k for your suggestion. i decided to continue with my approach .:)
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.