Round trip preservation of unmapped properties?

Skip to first unread message


Jun 26, 2020, 10:15:47 AM6/26/20
to AutoMapper-users

I have a DTO that is thinner than the Domain object it maps to, but the DTO has a Dictionary<string, object> property on it intended to preserve properties it doesn't know about for round trip purposes. Similar in concept to WCF's ExtensionDataObject

Is there a way to create a map so that any unmapped properties get inserted into the Dictionary property?

public class MyClass
    public string Prop1 {get;set;}
    public int Prop2 {get;set;}

public class MyDTO
    public string Prop1 {get;set;}
    public IDictionary<string,object> ExtendedProperties {get;set;}

I'm looking for an outcome like this:

var a = new MyClass() {Prop1 = "Abc", Prop2 = 12345};
var b = mapper.Map<MyDto>(a);

Assert.AreEqual(a.Prop1, b.Prop1);
Assert.AreEqual(a.Prop2, b.ExtendedProperties["Prop2"]);

and also the round trip:

var c = mapper.Map<MyClass>(b);
Assert.AreEqual(a.Prop1, c.Prop1);
Assert.AreEqual(a.Prop2, c.Prop2);

While there's probably a path here if MyDto inherited from System.Dynamic.DynamicObject, that's not an option at this point in time. I'm looking for a way to follow normal convention mapping for the properties that do exist, and direct all unmapped properties in to and out of the ExtendedProperties dictionary without having to manually specify explicit mappings for each property that I know beforehand doesn't exist in the DTO.


Reply all
Reply to author
0 new messages