I have the following classes.
public class FieldValue
{
public string Field { get; set; }
public object Value { get; set; }
}
public class FieldValues
{
public FieldValue[] Values { get; set; }
}
public static class FieldValuesExtensions
{
public static IDictionary<string, object> ToDictionary(this FieldValues self)
{
return self.Values.ToDictionary(v => v.Field, v => v.Value);
}
}
public class SourceClass : FieldValues
{
public int Id { get; set; }
public string Name { get; set; }
}
public class DestinationClassA
{
public int Id { get; set; }
public string Field1 { get; set; }
public string Field2 { get; set; }
}
public class DestinationClassB
{
public string Name { get; set; }
public string Field2 { get; set; }
public string Field3 { get; set; }
}
SourceClass
derives from FieldValues
and has its own fields. DestinationClassX
types are composed of fields only.
I need to map from SourceClass
to DestinationClassX
as follows:
public class TestClass
{
public static void Test(IMapper mapper)
{
// create a SourceClass object
SourceClass source = new() {
Id = 123,
Name = "Foo",
Values = new FieldValue[] {
new() { Field = "Field1", Value = "Value1" },
new() { Field = "Field2", Value = "Value2" },
new() { Field = "Field3", Value = "Value3" },
new() { Field = "Field4", Value = "Value4" }
}
};
// how do we setup mapping configuration to achieve the following?
// map to DestinationClassA
DestinationClassA destinationA = mapper.Map<SourceClass, DestinationAClass>(source);
// destinationA.Id == 123;
// destinationA.Field1 == "Value1";
// destinationA.Field2 == "Value2";
// map to DestinationClassB
DestinationClassB destinationB = mapper.Map<SourceClass, DestinationBClass>(source);
// destinationB.Name == "Foo";
// destinationB.Field2 == "Value2";
// destinationB.Field3 == "Value3";
}
}
I need to set up Automapper mapping to map from SourceClass
to DestinationClassX
so that any matching destination fields are mapped from:
-
any matching Field/Value from
SourceClass
‘s baseFieldValues
-
as well as any matching members from
SourceClass
itself as normal.
I understand that Automapper can automatically map from dynamic objects as well as IDictionary<string, object>. If I have, for example a class with a DestinationClassX
member:
public class DestinationClassWithA
{
public DestinationClassA A { get; set; }
}
with the following mapping:
CreateMap<SourceClass, DestinationClassA>(); // map 1
CreateMap<SourceClass, DestinationClassWithA>()
.ForMember(dst => dst.A, opt => opt.MapFrom(src => src.ToDictionary())); // map 2
Destination.A
Field1
and Field2
are populated as per map 2, but Id
from map 1 is set to null
. If I comment out map 2, map 1 works but of course I don’t get the dictionary entries.
I’m not sure how to incorporate the dictionary map (map 2) into map 1.
CreateMap<SourceClass, DestinationClassA>()
// what goes here?
CreateMap<SourceClass, DestinationClassB>()
// what goes here?
Petrock is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.