James Wallace
James Wallace

Reputation: 23

AutoMapper: Mapping with Dictionaries containing different index types

I have a need to use AutoMapper to map an object's data that is pulled from the applications database to another object, whose class is just slightly different. The difference being the namespace and a property named SubMessages as seen immediately below:

   public Dictionary<int, QcMessageBody> SubMessages
   {
     get { return mSubMessages; }
     set { mSubMessages = value;}
   }

where the dictionary key is an integer in the object coming from the database (source), and the destination object has the same property with a dictionary key of type string.

The class definitions are exactly the same, minus the differences that I have already covered.

Would anyone please help me to determine how I can set up AutoMapper to map the one object to the other. I am brand new to AutoMapper and I am afraid that I just don't understand AutoMapper well enough to set this up.

Thanks for your help.

[Serializable()]
public class MessageProperties : QCProperties, ISerializable
{
   public MessageProperties() {}
   public Dictionary<string, QcMessageBody> SubMessages
   {
     get {}
     set {}
   }

   public QcMessageBody CurrentSubMessage
   {
     get
     {}
   }

   public Boolean IsVarMsg
   {
     get {}
     set {}
   }

   public Guid SenderId
   {
     get {}
     set {}
   }

   public String SenderName
   {
     get {}
     set {}
   }

   public MessageSchedule Schedule
   {
     get {}
     set {}
   }

   public DateTime TimeSent
   {
     get {}
     set {}
   }

   public Guid RowQueryId
   {
     get {}
     set {}
   }

   public Boolean Solo
   {
     get {}
     set {}
   }

   public List<DestinationTag> Destinations
   {
     get {}
     set {}
   }

   public MessageType MsgType
   {
     get {}
     set {}
   }
 }

Upvotes: 1

Views: 1361

Answers (1)

James Wallace
James Wallace

Reputation: 23

I found a solution to my problem and I wanted to report that to hopefully help others as they work with AutoMapper. First off, I was trying to map the wrong object. After a better inspection of the code I determined that I needed to map a class that had the following multilevel inheritance configuration.

public class MessageInsert : ISerializable, ICloneable {}
public class DateTimeInsert : MessageInsert {}
public class TimeStampInsert : DateTimeInsert, ISerializable {}
public class DateStampInsert : DateTimeInsert, ISerializable {}
public class ClockInsert : DateTimeInsert, ISerializable {}
public class CalendarInsert : DateTimeInsert, ISerializable {}
public class DataInsert : MessageInsert, ISerializable {} 
public class TokenInsert : DataInsert, ISerializable {}
public class VariableInsert : DataInsert, ISerializable {}

I needed to map a MessageInsert object and have all of the properties from the child levels map over to the new object. AutoMapper handles inheritance mapping but it must be set up in a specific manner with the use of the CreateMap statements. The CreateMap statements that I used below are how I set up the mapping and they seemed to work. If someone sees a better way of doing these and wants to chime in the by all means.

The first block of statements were used to handle the namespace mapping of the different classses, enumerations, structures, etc that had a namespace associated with it.

AutoMapper.Mapper.CreateMap<ABC.QC.Properties.MessageSchedule, DBWebAgent.Properties.MessageSchedule>();
AutoMapper.Mapper.CreateMap<ABC.QC.Properties.DashPropType, DBWebAgent.Properties.DashPropType>();
AutoMapper.Mapper.CreateMap<ABC.QC.Properties.MessageType, DBWebAgent.Properties.MessageType>();
AutoMapper.Mapper.CreateMap<ABC.QC.Properties.DestinationTag, DBWebAgent.Properties.DestinationTag>();
AutoMapper.Mapper.CreateMap<ABC.QC.Properties.QcMessageBody, DBWebAgent.Properties.QcMessageBody>();
AutoMapper.Mapper.CreateMap<ABC.QC.Properties.DataInsertThreshold, DBWebAgent.Properties.DataInsertThreshold>();
AutoMapper.Mapper.CreateMap<ABC.QC.Properties.QuickCOMAlarm, DBWebAgent.Properties.QuickCOMAlarm>();
AutoMapper.Mapper.CreateMap<ABC.QC.Properties.ScrollEffectType, DBWebAgent.Properties.ScrollEffectType>();
AutoMapper.Mapper.CreateMap<ABC.QC.Properties.MessagePriorityType, DBWebAgent.Properties.MessagePriorityType>();
AutoMapper.Mapper.CreateMap<ABC.QC.Properties.QcMessageSegment, DBWebAgent.Properties.QcMessageSegment>();          
AutoMapper.Mapper.CreateMap<ABC.QC.Properties.DateTimeInsert, DBWebAgent.Properties.DateTimeInsert>();
AutoMapper.Mapper.CreateMap<ABC.QC.Properties.DateStampInsert, DBWebAgent.Properties.DateStampInsert>();
AutoMapper.Mapper.CreateMap<ABC.QC.Properties.TimeStampInsert, DBWebAgent.Properties.TimeStampInsert>();
AutoMapper.Mapper.CreateMap<ABC.QC.Properties.ClockInsert, DBWebAgent.Properties.ClockInsert>();
AutoMapper.Mapper.CreateMap<ABC.QC.Properties.CalendarInsert, DBWebAgent.Properties.CalendarInsert>();
AutoMapper.Mapper.CreateMap<ABC.QC.Properties.DataInsert, DBWebAgent.Properties.DataInsert>();
AutoMapper.Mapper.CreateMap<ABC.QC.Properties.VariableInsert, DBWebAgent.Properties.VariableInsert>();
AutoMapper.Mapper.CreateMap<ABC.QC.Properties.TokenInsert, DBWebAgent.Properties.TokenInsert>();

The second set of CreateMap statements was defined to handle all of the levels of inheritance in the class.

AutoMapper.Mapper.CreateMap<ABC.QC.Properties.MessageInsert, DBWebAgent.Properties.MessageInsert>()
     .Include<ABC.QC.Properties.DateTimeInsert, DBWebAgent.Properties.DateTimeInsert>()
     .Include<ABC.QC.Properties.DateStampInsert, DBWebAgent.Properties.DateStampInsert>()
     .Include<ABC.QC.Properties.TimeStampInsert, DBWebAgent.Properties.TimeStampInsert>()
     .Include<ABC.QC.Properties.ClockInsert, DBWebAgent.Properties.ClockInsert>()
     .Include<ABC.QC.Properties.CalendarInsert, DBWebAgent.Properties.CalendarInsert>()
     .Include<ABC.QC.Properties.DataInsert, DBWebAgent.Properties.DataInsert>()
     .Include<ABC.QC.Properties.VariableInsert, DBWebAgent.Properties.VariableInsert>()
     .Include<ABC.QC.Properties.TokenInsert, DBWebAgent.Properties.TokenInsert>();

Upvotes: 1

Related Questions