예제 #1
0
        public static List <MappingMember> GetMemberBinding(Type source, Type target)
        {
            TypePair typePair = TypePair.Create(source, target);
            var      result   = targetMapperBuilder.MemberBuilder.Build(typePair);

            return(result);
        }
예제 #2
0
        /// <summary>
        /// Maps the source to Target type.
        /// The method can be called in parallel to Map methods, but cannot be called in parallel to Bind method.
        /// </summary>
        /// <param name="sourceType">Source type.</param>
        /// <param name="targetType">Target type.</param>
        /// <param name="source">Source object.</param>
        /// <param name="target">Target object.</param>
        /// <returns>Mapped object.</returns>
        public static object Map(Type sourceType, Type targetType, object source, object target = null)
        {
            var typePair = TypePair.Create(sourceType, targetType);

            var mapper = GetMapper(typePair);
            var result = mapper.Map(source, target);

            return(result);
        }
예제 #3
0
        /// <summary>
        /// Find out if a binding exists from Source to Target.
        /// </summary>
        /// <param name="sourceType">Source type.</param>
        /// <param name="targetType">Target type.</param>
        /// <returns>True if exists, otherwise - False.</returns>
        /// <remarks>The method is thread safe.</remarks>
        public static bool BindingExists(Type sourceType, Type targetType)
        {
            var typePair = TypePair.Create(sourceType, targetType);

            lock (_mappersLock)
            {
                return(_mappers.ContainsKey(typePair));
            }
        }
예제 #4
0
        /// <summary>
        /// Find out if a binding exists from Source to Target.
        /// </summary>
        /// <typeparam name="TSource">Source type.</typeparam>
        /// <typeparam name="TTarget">Target type.</typeparam>
        /// <returns>True if exists, otherwise - False.</returns>
        /// <remarks>The method is thread safe.</remarks>
        public static bool BindingExists <TSource, TTarget>()
        {
            var typePair = TypePair.Create <TSource, TTarget>();

            lock (_mappersLock)
            {
                return(_mappers.ContainsKey(typePair));
            }
        }
예제 #5
0
        public static TTarget Map <TSource, TTarget>(TSource source, TTarget target = default(TTarget))
        {
            TypePair typePair = TypePair.Create <TSource, TTarget>();

            Mapper mapper = GetMapper(typePair);
            var    result = (TTarget)mapper.Map(source, target);

            return(result);
        }
예제 #6
0
        /// <summary>
        /// Create a one-way mapping between Source and Target types.
        /// </summary>
        /// <typeparam name="TSource">Source type.</typeparam>
        /// <typeparam name="TTarget">Target type.</typeparam>
        /// <remarks>The method is thread safe.</remarks>
        public static void Bind <TSource, TTarget>()
        {
            var typePair = TypePair.Create <TSource, TTarget>();

            lock (_mappersLock)
            {
                _mappers[typePair] = _targetMapperBuilder.Build(typePair);
            }
        }
예제 #7
0
        public static void Bind <TSource, TTarget>(Action <IBindingConfig <TSource, TTarget> > config)
        {
            TypePair typePair = TypePair.Create <TSource, TTarget>();

            var bindingConfig = new BindingConfigOf <TSource, TTarget>();

            config(bindingConfig);

            _mappers[typePair] = _targetMapperBuilder.Build(typePair, bindingConfig);
        }
예제 #8
0
        internal static TTarget MapCore <TSource, TTarget>(TSource source, TTarget target, int depth)
        {
            if (source == null || depth > mapperConfig.MaxDepth)
            {
                return(target);
            }
            TypePair pair   = TypePair.Create <TSource, TTarget>();
            IMapper  mapper = MapperUtil.GetMapper(pair);

            return((TTarget)mapper.Map(source, target, mapperConfig.ReferencePropertyHandle, depth + 1));
        }
예제 #9
0
        public static void BindCustom <TSource, TTarget>(Func <TSource, TTarget> mapper)
        {
            TypePair typePair = TypePair.Create <TSource, TTarget>();

            if (mapper == null)
            {
                throw new ArgumentNullException(nameof(mapper));
            }
            Func <object, object> innerMapper = x => mapper((TSource)x);

            CustomMapper.Add(typePair, innerMapper);
        }
예제 #10
0
        /// <summary>
        /// Create a one-way mapping between Source and Target types.
        /// </summary>
        /// <typeparam name="TSource">Source type.</typeparam>
        /// <typeparam name="TTarget">Target type.</typeparam>
        /// <param name="config">BindingConfig for custom binding.</param>
        /// <remarks>The method is thread safe.</remarks>
        public IObjectMapper Bind <TSource, TTarget>(Action <IBindingConfig <TSource, TTarget> > config)
        {
            TypePair typePair = TypePair.Create <TSource, TTarget>();

            var bindingConfig = new BindingConfigOf <TSource, TTarget>();

            config(bindingConfig);
            var mapper = _targetMapperBuilder.Build(typePair, bindingConfig);

            Bind(typePair, mapper);
            return(this);
        }
예제 #11
0
        /// <summary>
        ///     Find out if a binding exists for Source to Target
        /// </summary>
        /// <typeparam name="TSource">Source type</typeparam>
        /// <typeparam name="TTarget">Target type</typeparam>
        /// <returns></returns>
        public static bool BindingExists <TSource, TTarget>()
        {
            TypePair typePair = TypePair.Create <TSource, TTarget>();
            Mapper   mapper;

            _mappersLock.EnterReadLock();
            var result = _mappers.TryGetValue(typePair, out mapper);

            _mappersLock.ExitReadLock();

            return(result);
        }
예제 #12
0
        internal static IEnumerable <TTarget> MapCores <TSource, TTarget>(IEnumerable <TSource> source, int depth)
        {
            if (source == null || depth > mapperConfig.MaxDepth)
            {
                return(null);
            }
            TypePair pair   = TypePair.Create <TSource, TTarget>();
            IMapper  mapper = MapperUtil.GetMapper(pair);
            var      list   = new List <TTarget>();

            foreach (TSource item in source)
            {
                TTarget rst = (TTarget)mapper.Map(item, default, mapperConfig.ReferencePropertyHandle, depth + 1);
예제 #13
0
        /// <summary>
        /// Create a one-way mapping between Source and Target types.
        /// </summary>
        /// <typeparam name="TSource">Source type.</typeparam>
        /// <typeparam name="TTarget">Target type.</typeparam>
        /// <param name="config">BindingConfig for custom binding.</param>
        /// <remarks>The method is thread safe.</remarks>
        public static void Bind <TSource, TTarget>(Action <IDefaultBindingConfig <TSource, TTarget> > config)
        {
            var typePair = TypePair.Create <TSource, TTarget>();

            var bindingConfig = new DefaultBindingConfigOf <TSource, TTarget>();

            config(bindingConfig);

            lock (_mappersLock)
            {
                _mappers[typePair] = _targetMapperBuilder.Build(typePair, bindingConfig);
            }
        }
예제 #14
0
        /// <summary>
        /// Maps the source to Target type.
        /// The method can be called in parallel to Map methods, but cannot be called in parallel to Bind method.
        /// </summary>
        /// <typeparam name="TTarget">Target type.</typeparam>
        /// <param name="source">Source object [Not null].</param>
        /// <returns>Mapped object. The method can be called in parallel to Map methods, but cannot be called in parallel to Bind method.</returns>
        public static TTarget Map <TTarget>(object source)
        {
            if (source.IsNull())
            {
                throw Error.ArgumentNull("Source cannot be null. Use TinyMapper.Map<TSource, TTarget> method instead.");
            }

            var typePair = TypePair.Create(source.GetType(), typeof(TTarget));

            var mapper = GetMapper(typePair);
            var result = (TTarget)mapper.Map(source);

            return(result);
        }
예제 #15
0
        protected override void BuildCore(TypePair typePair, BindingConfig bindingConfig)
        {
            var members           = MemberBuilder.Build(typePair).Where(x => !x.Ignored).ToList();
            var equalMembers      = new List <MappingMember>();
            var mappingMembers    = new List <MappingMember>();
            var refMembers        = new List <MappingMember>();
            var collectionMembers = new List <MappingMember>();
            var expressionMembers = new List <MappingMember>();

            foreach (var item in members)
            {
                if (item.IsExpressionMapping)
                {
                    expressionMembers.Add(item);
                }
                else
                {
                    var memberTypePair = TypePair.Create(item.Source.GetMemberType(), item.Target.GetMemberType());
                    if (memberTypePair.IsEqualTypes)
                    {
                        equalMembers.Add(item);
                    }
                    else if (memberTypePair.IsBaseTypes)
                    {
                        mappingMembers.Add(item);
                    }
                    else if (config.ReferencePropertyHandle != ReferencePropertyHandle.Ignore)
                    {
                        if (memberTypePair.IsEnumerableTypes)
                        {
                            collectionMembers.Add(item);
                        }
                        else
                        {
                            refMembers.Add(item);
                        }
                    }
                }
            }

            var func = CreateMapper(typePair, equalMembers, mappingMembers);

            MapperCache.Add(typePair, func);
            var action = CreateMapperRef(typePair, refMembers, collectionMembers, expressionMembers);

            if (action != null)
            {
                MapperRefCache.Add(typePair, action);
            }
        }
예제 #16
0
        /// <summary>
        ///     Create a one-way mapping between one type and another
        /// </summary>
        /// <typeparam name="TSource">Source type</typeparam>
        /// <typeparam name="TTarget">Target type</typeparam>
        public static void Bind <TSource, TTarget>()
        {
            TypePair typePair = TypePair.Create <TSource, TTarget>();

            _mappersLock.EnterWriteLock();
            try
            {
                _mappers[typePair] = _targetMapperBuilder.Build(typePair);
            }
            finally
            {
                _mappersLock.ExitWriteLock();
            }
        }
예제 #17
0
        /// <summary>
        ///     Maps the specified source to <see cref="TTarget" /> type.
        /// </summary>
        /// <typeparam name="TTarget">The type of the target.</typeparam>
        /// <param name="source">The source value.</param>
        /// <returns>Value</returns>
        /// <remarks>For mapping nullable type use <see cref="Map{TTarget}" />method.</remarks>
        public static TTarget Map <TTarget>(object source)
        {
            if (source.IsNull())
            {
                throw Error.ArgumentNull("source, for mapping nullable type use Map<TSource, TTarget> method");
            }

            TypePair typePair = TypePair.Create(source.GetType(), typeof(TTarget));

            Mapper mapper = GetMapper(typePair);
            var    result = (TTarget)mapper.Map(source);

            return(result);
        }
예제 #18
0
파일: TinyMapper.cs 프로젝트: ewin66/SAE
        /// <summary>
        /// Create a one-way mapping between Source and Target types.
        /// </summary>
        /// <typeparam name="TSource">Source type.</typeparam>
        /// <typeparam name="TTarget">Target type.</typeparam>
        /// <param name="config">BindingConfig for custom binding.</param>
        /// <remarks>The method is thread safe.</remarks>
        public static IReverseBinding <TTarget, TSource> Bind <TSource, TTarget>(Action <IBindingConfig <TSource, TTarget> > config)
        {
            TypePair typePair = TypePair.Create <TSource, TTarget>();

            var bindingConfig = new BindingConfigOf <TSource, TTarget>();

            config(bindingConfig);

            lock (_mappersLock)
            {
                _mappers[typePair] = _targetMapperBuilder.Build(typePair, bindingConfig);
            }

            return(new ReverseBinding <TTarget, TSource>(bindingConfig.BindExpression));
        }
예제 #19
0
        /// <summary>
        /// Create a one-way mapping between Source and Target types.
        /// </summary>
        /// <param name="sourceType">Source type.</param>
        /// <param name="targetType">Target type.</param>
        /// <remarks>The method is thread safe.</remarks>
        public IObjectMapper Bind(Type sourceType, Type targetType)
        {
            if (sourceType == null)
            {
                throw new ArgumentNullException(nameof(sourceType));
            }
            if (targetType == null)
            {
                throw new ArgumentNullException(nameof(targetType));
            }
            TypePair typePair = TypePair.Create(sourceType, targetType);
            var      mapper   = _targetMapperBuilder.Build(typePair);

            Bind(typePair, mapper);
            return(this);
        }
예제 #20
0
        public static void Bind <TSource, TTarget>(Action <IBindingConfig <TSource, TTarget> > config = null)
        {
            TypePair typePair = TypePair.Create <TSource, TTarget>();

            if (config == null)
            {
                Bind(typePair);
            }
            else
            {
                var bindingConfig = new BindingConfig <TSource, TTarget>();
                config(bindingConfig);
                BindingConfig.Add(typePair, bindingConfig);
                CustomMapper.Remove(typePair);
                targetMapperBuilder.Build(typePair);
            }
        }
예제 #21
0
        public PropertyMap Build()
        {
            if (_receiverProperty == null)
            {
                throw new InvalidOperationException("You must configure receiver member before build");
            }

            if (_mappingActions == null)
            {
                throw new InvalidOperationException("You must configure mapping actions before build");
            }

            return(new PropertyMap(
                       TypePair.Create <TSource, TReceiver>(),
                       _receiverProperty,
                       _mappingActions));
        }
예제 #22
0
        /// <summary>
        /// Create a one-way mapping between Source and Target types.
        /// </summary>
        /// <param name="sourceType">Source type.</param>
        /// <param name="targetType">Target type.</param>
        /// <remarks>The method is thread safe.</remarks>
        public static void Bind(Type sourceType, Type targetType)
        {
            if (sourceType is null)
            {
                throw new ArgumentNullException(nameof(sourceType));
            }
            if (targetType is null)
            {
                throw new ArgumentNullException(nameof(targetType));
            }
            var typePair = TypePair.Create(sourceType, targetType);

            lock (_mappersLock)
            {
                _mappers[typePair] = _targetMapperBuilder.Build(typePair);
            }
        }
예제 #23
0
파일: TinyMapper.cs 프로젝트: ewin66/SAE
        /// <summary>
        /// Find out if a binding exists from Source to Target.
        /// </summary>
        /// <param name="sourceType">Source type.</param>
        /// <param name="targetType">Target type.</param>
        /// <returns>True if exists, otherwise - False.</returns>
        /// <remarks>The method is thread safe.</remarks>
        public static bool BindingExists(Type sourceType, Type targetType)
        {
            if (sourceType == null)
            {
                throw new ArgumentNullException(nameof(sourceType));
            }
            if (targetType == null)
            {
                throw new ArgumentNullException(nameof(targetType));
            }

            TypePair typePair = TypePair.Create(sourceType, targetType);

            lock (_mappersLock)
            {
                return(_mappers.ContainsKey(typePair));
            }
        }
예제 #24
0
        /// <summary>
        ///     Create a one-way mapping between one type and another
        /// </summary>
        /// <typeparam name="TSource">Source type</typeparam>
        /// <typeparam name="TTarget">Target type</typeparam>
        /// <param name="config">BindingConfig for Custom Binding</param>
        public static void Bind <TSource, TTarget>(Action <IBindingConfig <TSource, TTarget> > config)
        {
            TypePair typePair = TypePair.Create <TSource, TTarget>();

            var bindingConfig = new BindingConfigOf <TSource, TTarget>();

            config(bindingConfig);

            _mappersLock.EnterWriteLock();
            try
            {
                _mappers[typePair] = _targetMapperBuilder.Build(typePair, bindingConfig);
            }
            finally
            {
                _mappersLock.ExitWriteLock();
            }
        }
예제 #25
0
        public ProfileMap Build()
        {
            if (_executionClause == null)
            {
                throw new InvalidOperationException("Execution clause must be set for non default profile");
            }

            if (!_propertyMaps.Any())
            {
                throw new InvalidOperationException("No property maps configured");
            }

            var typePair = TypePair.Create <TSource, TReceiver>();

            return(new ProfileMap(
                       typePair,
                       GetReceiverConstructorInfo(typePair.ReceiverType),
                       _executionClause,
                       _isDefault,
                       _propertyMaps));
        }
예제 #26
0
        public IProfileMapBuilder <TSource, TReceiver> UsePropertyMaps(IEnumerable <PropertyMap> propertyMaps)
        {
            if (propertyMaps == null)
            {
                throw new ArgumentNullException(nameof(propertyMaps));
            }

            var typePair         = TypePair.Create <TSource, TReceiver>();
            var propertyMapsList = propertyMaps.ToList();

            if (!propertyMapsList.Any(c => c.TypePair.Equals(typePair)))
            {
                throw new ArgumentException("Property map for different type pair received");
            }

            foreach (var propertyMap in propertyMapsList)
            {
                AddOrReplacePropertyMap(propertyMap);
            }

            return(this);
        }
예제 #27
0
        //Note: Lock should already be acquired for the mapper
        private static Mapper GetPolymorphicMapping(TypePair types)
        {
            // Walk the polymorphic heirarchy until we find a mapping match
            Type source = types.Source;

            do
            {
                Mapper result;
                foreach (var iface in source.GetInterfaces())
                {
                    if (_mappers.TryGetValue(TypePair.Create(iface, types.Target), out result))
                    {
                        return(result);
                    }
                }

                if (_mappers.TryGetValue(TypePair.Create(source, types.Target), out result))
                {
                    return(result);
                }
            }while ((source = source.BaseType) != null);

            return(null);
        }
예제 #28
0
        public static void Bind <TSource, TTarget>()
        {
            TypePair typePair = TypePair.Create <TSource, TTarget>();

            _mappers[typePair] = _targetMapperBuilder.Build(typePair);
        }
예제 #29
0
        public static void Bind(Type source, Type target)
        {
            TypePair typePair = TypePair.Create(source, target);

            Bind(typePair);
        }
예제 #30
0
        public static bool BindingExists(Type source, Type target)
        {
            TypePair typePair = TypePair.Create(source, target);

            return(BindingConfig.Has(typePair));
        }