0

I am writing a simple mapper class to clone and copy properties between different objects of different/same types.

Mapping is performed via reflection and works just fine for value types, lists and reference types.

Everything works for types like this one:

class SimpleValueTypes 
{
    public string Name { get; set; }
    public int Number { get; set; }
    public long LongNumber { get; set; }
    public float FloatNumber { get; set; }
    public bool BooleanValue { get; set; }
    public AnotherType AnotherProperty{ get; set; }

}

where AnotherType is a class type. Mapping of reference types is performed by inspecting the properties recursively until all the properties are mapped to the destination pretty much this way:

object value = mapFrom.GetValue(input, null);                
mapTo.SetValue(output, value, null);

where mapForm and mapTo are PropertyInfo objects.

Problems began when a new type with a "Bitmap" property arrived and I realised a whole class of objects cannot be treated the same way.

Class NewType
{
    public Bitmap Bitmap{get;set;}
    public string Name{get;set;}
}

What would you reccomend to do for cases like these? Obviously copying the properties would not lead to a new copy of the original Bitmap object.

PS

I can't use automapper/emit mapper or any other external packages.

mhttk
  • 1,688
  • 1
  • 16
  • 29

2 Answers2

1

If you are implementing a mapper and not use some existing ones :

I think that this always worked on your side, it's only a case of your project (which is a good news). The thing is that, by me, you arrived on point where you need to implement custom mapper. So define some base class like BaseMapper with mapping basic functionality, and derive from it BitmapMapper:BaseMapper class which takes care of mapping of Bitmap type objects.

After a while could be you will get some other type, you will create MyTypeMapper : BaseMapper with concrete implementation of mapping.

This just a basic idea, you should think of this to find out the most suitable way of concrete implementation of it for your project.

If using your own mapper is not mandatory for you, you can use some already existing solutions available on market, like for example Automapper

Regards.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • This is what I was leaning towards too. Can't use external libraries on this one. – mhttk Sep 09 '11 at 14:04
  • You can use unique abstract SetMap property that runs an overriden concrete mapper implementation. – Tigran Sep 09 '11 at 14:57
  • Yes, I think I am goig for this. Now implementing a mapping mechanism that follows the strategy pattern. This way I would look in the strategy repository for any mapping. Also I can extend it quite easily. – mhttk Sep 09 '11 at 15:07
1

If the object is ICLoneable you can leverage that, you can easily check if it is and rely on the implementation. For object that are not ICloneable this thread might help you along.

Gert-Jan

Community
  • 1
  • 1
gjvdkamp
  • 9,929
  • 3
  • 38
  • 46
  • I like the ICloneable approach (Image implements it and would definitely work) and definitely the thread you mentioned is of great help. I am not to keen on relying on ICloneable for the problems that that may hide (cloning/deepcloning etc). +1 anyway – mhttk Sep 09 '11 at 14:00