Example #1
0
        /// <summary>
        /// Maps the source object to the target object. Contains the main logic of mapping.
        /// </summary>
        /// <param name="from">The source object.</param>
        /// <param name="to">The target object.</param>
        /// <param name="mappingData">The mapping data.</param>
        public override void DoMapping(object from, object to, MappingData mappingData)
        {
            if (from == null || to == null)
                throw new ArgumentNullException();

            mappingData = mappingData ?? new MappingData();

            //get target properties to copy data into them
            Dictionary<string,PropertyInfo> targetProperties = this.GetWritableProperties(to.GetType()).ToDictionary(p=>p.Name.ToLower(),p=>p);

            List<string> excludes = (mappingData.ExcludedProperties.Count == 0) ? null : mappingData.ExcludedProperties.Where(x => !string.IsNullOrWhiteSpace(x)).ToList();

            foreach (PropertyInfo sourceProperty in from.GetType().GetProperties().Where(x => x.CanRead))
            {
                if (excludes != null && excludes.Count > 0)
                {
                    if (excludes.Where(x => string.Equals(x, sourceProperty.Name, mappingData.IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal)).SingleOrDefault() != null)
                        continue;
                }

                // Find a set of properties for current.
                List<MappingDataItem> mappingDataItems = mappingData.PropertiesMapping.Where(x => string.Equals(x.PropertyFrom, sourceProperty.Name, mappingData.IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) ).ToList();

                if (targetProperties.ContainsKey(sourceProperty.Name.ToLower()))
                {
                    bool bExists = mappingDataItems.Where(x => string.Equals(x.PropertyFrom, sourceProperty.Name, mappingData.IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal)  && string.Equals(x.PropertyTo, sourceProperty.Name, mappingData.IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal)).FirstOrDefault() != null;

                    if(!bExists)
                        mappingDataItems.Add(new MappingDataItem(sourceProperty.Name, sourceProperty.Name, null, mappingData.IgnoreCase));
                }

                foreach (MappingDataItem mdi in mappingDataItems)
                {
                    string mappingDataPropertyTo = mdi.PropertyTo.ToLower();

                    if (targetProperties.ContainsKey(mappingDataPropertyTo))
                    {
                        this.CopyData(from, to, sourceProperty, targetProperties[mappingDataPropertyTo], mdi.Converter);
                    }
                }
            }
        }
 protected GalleryItemModel Convert(CuratedGallery item)
 {
     MappingData md = new MappingData(new string[]
         {
             Utils.GetPropertyName<CuratedGallery, GalleryPublicationPeriod>(x => x.PublicationPeriod),
             Utils.GetPropertyName<CuratedGallery, DateTime>(x => x.DateCreated),
             Utils.GetPropertyName<CuratedGallery, DateTime?>(x => x.DateModified)
         });
     var output = this.ObjectMapper.DoMapping<GalleryItemModel>(item, md);
     output.DateCreated = item.DateCreated.ToString(DateTimeFormat);
     output.DateModified = item.DateModified.HasValue ? item.DateModified.Value.ToString(DateTimeFormat) : null;
     output.PublicationPeriod = item.PublicationPeriod == null ? null : new Range<string>() { From = item.PublicationPeriod.Start.ToString(), To = item.PublicationPeriod.End.HasValue ? item.PublicationPeriod.End.Value.ToString() : null };
     return output;
 }
 protected CuratedGallery Convert(CuratedGalleryRecord record)
 {
     MappingData md = new MappingData(new string[] { Utils.GetPropertyName<CuratedGallery, AdminUserInfo>(x => x.Editor) });
     md.PropertiesMapping.Add(new MappingDataItem(Utils.GetPropertyName<CuratedGalleryRecord, short>(x => x.StatusID), Utils.GetPropertyName<CuratedGallery, CuratedGalleryStatuses>(x => x.Status), x => x.ToEnum<CuratedGalleryStatuses>()));
     md.PropertiesMapping.Add(new MappingDataItem(Utils.GetPropertyName<CuratedGalleryRecord, DateTime>(x => x.DateCreated), null, x => ((DateTime)x).ToLocalTime()));
     md.PropertiesMapping.Add(new MappingDataItem(Utils.GetPropertyName<CuratedGalleryRecord, DateTime?>(x => x.DateModified), null, x => (x == null ? (DateTime?)null : ((DateTime)x).ToLocalTime())));
     return this.ObjectMapper.DoMapping<CuratedGallery>(record, md);
 }