1
votes

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 {}
   }
 }
1
Please be more specific. Why do you need to use AutoMapper and how do you expect these two object types to map to each other? Would you like to flatten them and consider them equal if all their properties are the same except for the SubMessages property? What are you hoping to accomplish? - nicholas
This object is returned from a web service call to a javascript routine. The integer index in the dictionary is causing a serialization problem because its being returned to the javascript routine, so I have to find a way to map the data in the object to another object that has a string index defined for the dictionary. I can't touch the back end code that is returning the object with the integer index so I must receive the object from the database, convert it to use a string index by creating a similar class with the different property definition and then return that object. - James Wallace
The only thing I need done different to the data in the object that I returning is to deal with fact that the dictionary has an integer index. What I did was create an identical class structure with the only difference being the index. I now want to map the object returned from the database to the object created by the new class definition. Please let me know if AutoMapper can be used for this??? Thanks for your help! - James Wallace
How is an entry in the string-indexed dictionary generated from the int-indexed dictionary? You receive the serialized web object with the int-indexed dictionary and you need to send it back as a string-indexed dictionary? What type of serialization is the web service using? AutoMapper probably can do what you want, but I am not sure it is necessary. - nicholas
I am using SOAP serialization to send to a js webservice call. I receive an error message when using the integer based dictionary, but an older version of the DLL that processed the webservice call used a string based dictionary, which worked fine. As I understand it, javascript wants a string based index to be passed and this is why the serialization fails. Any help with Automapper would be truly appreciated. - James Wallace

1 Answers

1
votes

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>();