4

I have a very large class (500+ properties and nested complex objects) and we are mapping to another class with the same properties i.e. it is a one-to-one mapping.

Please no comments about why we are doing this (a long story - but this is a legacy system that is in the process of being re-architected and this is a stepping stone to the next stage of refactoring out services) - and why not automapper etc. Data mapping is hand coded in C#.

I could create a test object, map and compare the mapped object, however there are SO many properties to populate, this in itself is a major task which we hope to avoid.

Any thoughts on whether I could use reflection or serialize/deserialize or some test libraries or maybe use automapper in some way to fill object, map and compare?

We need to ensure a) all properties are mapped and b) each property is mapped to the correct property (properties on each object is named the same)

I suspect a manual code review is probably the only feasible solution but I'm reaching out...

UPDATE OK not sure why people have down-voted this. It is a valid question with some potentially complex technical solutions. Thanks for you guys that have responded with useful suggestions!

Mark Chidlow
  • 1,432
  • 2
  • 24
  • 43
  • 2
    http://docs.automapper.org/en/latest/Configuration-validation.html – Lucian Bargaoanu Sep 11 '18 at 11:27
  • 1
    How about? `foreach (var prop in typeof(FIRSTCLASS).GetProperties()) { typeof(SECONDCLASS).GetProperty(prop.Name).SetValue(INSTANCEOFSECONDCLASS, prop.GetValue(INSTANCEOFFIRSTCLASS)); }` This is not a full blown solution, but if you want to do this dirty, this is a good way to get started. – Mixxiphoid Sep 11 '18 at 11:35
  • Some options [here](https://stackoverflow.com/questions/318210/compare-equality-between-two-objects-in-nunit) – stuartd Sep 11 '18 at 11:38
  • Thanks @Mixxiphoid will have to consider this. – Mark Chidlow Sep 11 '18 at 11:43
  • Thanks @LucianBargaoanu interesting that would certainly confirm both objects are the same – Mark Chidlow Sep 11 '18 at 11:43
  • Thanks @stuartd I would still have to populate all fields with unique data (I think) which I'd like to avoid :-) – Mark Chidlow Sep 11 '18 at 11:45
  • 1
    You might be able to write a bit of reflection code to assign random values depending on the underlying property type - you'd have to drill into the objects until you find a type you can handle, and if they don't have default constructors that would be a problem. – stuartd Sep 11 '18 at 11:52
  • Thanks @stuartd, I think this is similar to what Mixxiphoid above suggests. – Mark Chidlow Sep 11 '18 at 12:19

1 Answers1

1

Any thoughts on whether I could use reflection or serialize/deserialize or some test libraries or maybe use automapper in some way to fill object, map and compare?

You could just use a serializer and serialize one object and deserialize the other. Could be a three-to-five-liner if your objects are plain data classes that don't do exotic stuff.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • Thanks - this was my first thought but i still need to populate the entire object with unique data, to serialize. Something I want to avoid. – Mark Chidlow Sep 11 '18 at 11:46
  • I don't really get the problem. Surely you will have a way to fill the original object? – nvoigt Sep 11 '18 at 11:47
  • I won't bore you with the details, but the way the source object is populated is not predictable and not in my control - and only a small (effectively random) subset of properties will have values! So if I were to write a unit test for this, I would have to manually populate all properties with known/unique values. – Mark Chidlow Sep 11 '18 at 12:16