示例#1
0
        /// <summary>
        /// Resolves type conversion between map item source and destination type.
        /// </summary>
        /// <param name="item">The item<see cref="MappedItem"/></param>
        /// <param name="sourceValue">The sourceValue<see cref="object"/></param>
        /// <returns>A possible conversion to try from source to destination type.</returns>
        public static TypeConversion Resolve(MappedItem item, object sourceValue)
        {
            Type sourceType      = item.Source.Type;
            Type destinationType = item.Destination.Type;

            return(Resolve(sourceValue, sourceType, destinationType));
        }
        /// <summary>
        /// Removes mapping by the provided source or destination member name.
        /// </summary>
        /// <param name="memberName">A name of the source or destination member to remove.</param>
        /// <returns>A changed <see cref="Map"/> instance.</returns>
        public ISimpleMapper Remove(string memberName)
        {
            if (string.IsNullOrWhiteSpace(memberName))
            {
                throw new ArgumentNullException(nameof(memberName));
            }

            for (int i = items.Count - 1; i >= 0; i--)
            {
                MappedItem m = items[i];

                if (m.Source.Name == memberName ||
                    m.Destination.Name == memberName)
                {
                    //// if item is complex, remove the sub map
                    if (m.IsComplex)
                    {
                        subMaps.Remove(m.Key);
                    }

                    items.RemoveAt(i);
                    m.Remove();
                }
            }

            return(this);
        }
        /// <summary>
        /// Add simple or complex item that maps source member to destination member.
        /// </summary>
        /// <typeparam name="TSourceMember"></typeparam>
        /// <typeparam name="TDestinationMember"></typeparam>
        /// <param name="source">The source<see cref="TSourceMember"/></param>
        /// <param name="destination">The destination<see cref="TDestinationMember"/></param>
        /// <param name="complex">The complex<see cref="bool"/></param>
        /// <returns>The <see cref="SimpleMapper"/></returns>
        private SimpleMapper AddItem <TSourceMember, TDestinationMember>(TSourceMember source, TDestinationMember destination, bool complex)
            where TSourceMember : MemberInfo
            where TDestinationMember : MemberInfo
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            var sourceItem      = new MappedItem.Item(source, true);
            var destinationItem = new MappedItem.Item(destination, false);

            MappedItem mapping = new MappedItem(sourceItem, destinationItem, this);

            this.items.Add(mapping);

            if (complex)
            {
                SimpleMapper subMap = MapResolver.Resolve(sourceItem.Type, destinationItem.Type);

                mapping.IsComplex = true;

                subMaps.Add(mapping.Key, subMap);
            }

            return(this);
        }
示例#4
0
        /// <summary>
        /// Convert provided source object to destination object based
        /// of an provided <see cref="MappedItem"/>.
        /// </summary>
        /// <param name="mapItem">A <see cref="MappedItem"/> that defines the mapping.</param>
        /// <param name="source">A source member value.</param>
        /// <returns>A destination member value or <c>null</c>.</returns>
        public static object Convert(MappedItem mapItem, object source)
        {
            if (mapItem == null)
            {
                throw new ArgumentNullException(nameof(mapItem));
            }

            Type sourceType      = mapItem.Source.Type;
            Type destinationType = mapItem.Destination.Type;

            return(Convert(source, sourceType, destinationType));
        }
        /// <summary>
        /// Add simple or complex item that maps source member to destination member.
        /// </summary>
        /// <param name="sourceMemberName">The sourceMemberName<see cref="string"/></param>
        /// <param name="sourceType">The sourceType<see cref="Type"/></param>
        /// <param name="destinationMemberName">The destinationMemberName<see cref="string"/></param>
        /// <param name="destinationType">The destinationType<see cref="Type"/></param>
        /// <param name="complex">The complex<see cref="bool"/></param>
        /// <returns>The <see cref="SimpleMapper"/></returns>
        private SimpleMapper AddItem(string sourceMemberName, Type sourceType, string destinationMemberName, Type destinationType, bool complex)
        {
            if (string.IsNullOrWhiteSpace(sourceMemberName))
            {
                throw new ArgumentNullException(nameof(sourceMemberName));
            }

            if (string.IsNullOrWhiteSpace(destinationMemberName))
            {
                throw new ArgumentNullException(nameof(destinationMemberName));
            }

            if (sourceType == null)
            {
                throw new ArgumentNullException(nameof(sourceType));
            }

            if (destinationType == null)
            {
                throw new ArgumentNullException(nameof(destinationType));
            }

            MemberInfo sm = sourceType.GetMember(sourceMemberName).FirstOrDefault();
            MemberInfo dm = destinationType.GetMember(destinationMemberName).FirstOrDefault();

            MappedItem.Item sourceItem      = null;
            MappedItem.Item destinationItem = null;
            MappedItem      mapping         = null;

            if (sm != null && dm != null)
            {
                sourceItem      = new MappedItem.Item(sm, true);
                destinationItem = new MappedItem.Item(dm, false);
                mapping         = new MappedItem(sourceItem, destinationItem, this);

                this.items.Add(mapping);
            }

            if (mapping != null)
            {
                if (complex)
                {
                    SimpleMapper subMap = MapResolver.Resolve(sourceItem.Type, destinationItem.Type);

                    mapping.IsComplex = true;

                    subMaps.Add(mapping.Key, subMap);
                }
            }

            return(this);
        }
        /// <summary>
        /// Gets sub map for the provided complex map item.
        /// </summary>
        /// <param name="item">The complex map item.</param>
        /// <returns>A sub map between complex types.</returns>
        internal SimpleMapper GetSubMap(MappedItem item)
        {
            Debug.Assert(item != null);
            Debug.Assert(item.Map == this);
            Debug.Assert(item.IsComplex);

            if (subMaps.TryGetValue(item.Key, out SimpleMapper subMap))
            {
                return(subMap);
            }

            return(null);
        }
        /// <summary>
        /// Restores previously ignored member.
        /// </summary>
        /// <param name="mapping">The <see cref="MappedItem"/> to unignore.</param>
        /// <returns>A changed <see cref="Map"/> instance.</returns>
        public ISimpleMapper Unignore(MappedItem mapping)
        {
            if (mapping == null)
            {
                throw new ArgumentNullException(nameof(mapping));
            }

            if (mapping.Map != this)
            {
                throw new ArgumentException("mapping is not from this setup.", "mapping");
            }

            mapping.IsIgnored = false;

            return(this);
        }
        /// <summary>
        /// Marks the mapping as ignored. If you want to remove mapping completely,
        /// use <see cref="Remove"/> method.
        /// </summary>
        /// <param name="item">A mapping to mark ignored.</param>
        /// <returns>A changed <see cref="Map"/> instance.</returns>
        public ISimpleMapper Ignore(MappedItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            if (item.Map != this)
            {
                throw new ArgumentException("mapping is not from this setup.", "mapping");
            }

            item.IsIgnored = true;

            return(this);
        }
        /// <summary>
        /// Removes provided item from the map.
        /// </summary>
        /// <param name="item">A mapping to remove.</param>
        /// <returns>A changed <see cref="Map"/> instance.</returns>
        public ISimpleMapper Remove(MappedItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            if (item.Map != this)
            {
                throw new ArgumentException("MappedItem is not from this setup.", "item");
            }

            int index = this.items.IndexOf(item);

            if (index >= 0)
            {
                this.items.RemoveAt(index);
                item.Remove();
            }

            return(this);
        }