Esempio n. 1
0
        /// <summary>
        /// Gets the mapper.
        /// </summary>
        /// <param name="typeOfSource">The t source.</param>
        /// <param name="typeOfTarget">The t dest.</param>
        /// <param name="throwExceptionOnNoFound">if set to <c>true</c> [throw exception on no found].</param>
        /// <param name="name">The name of mapper.</param>
        /// <returns></returns>
        /// <exception cref="NoFoundMapperException"></exception>
        protected static MapperConfigurationBase GetMapper(Type typeOfSource, Type typeOfTarget, bool throwExceptionOnNoFound, string name = null)
        {
            MapperConfigurationBase mapperExterne = null;

            mapperExterne = MapperConfigurationCollectionContainer.Instance.Find(typeOfSource, typeOfTarget, name);
            // we raise an exception if there is nothing and configured
            if (mapperExterne == null && throwExceptionOnNoFound)
            {
                throw new NoFoundMapperException(typeOfSource, typeOfTarget);
            }

            return(mapperExterne);
        }
        /// <summary>
        /// Reverses the map.
        /// </summary>
        /// <param name="name">The name of the mapper.</param>
        /// <returns>
        /// the new Mapper
        /// </returns>
        /// <exception cref="MapperExistException"></exception>
        public MapperConfiguration <TDest, TSource> ReverseMap(string name = null)
        {
            MapperConfigurationBase map = GetMapper(typeof(TDest), typeof(TSource), false, name);

            if (map != null)
            {
                throw new MapperExistException(typeof(TDest), typeof(TSource));
            }
            string finalName = string.IsNullOrEmpty(name) ? "s" + (MapperConfigurationCollectionContainer.Instance.Count).ToString() : name;

            map = new MapperConfiguration <TDest, TSource>(finalName);
            MapperConfigurationCollectionContainer.Instance.Add(map);
            CreateCommonMember();

            // Path is the mapping of existing properties and inverse relationships are created
            for (int i = 0; i < PropertiesMapping.Count; i++)
            {
                Tuple <Expression, Expression, bool, string> item = PropertiesMapping[i];
                PropertyInfo propertyDest = GetPropertyInfo(item.Item1);
                if (propertyDest.CanWrite)
                {
                    if (!string.IsNullOrEmpty(item.Item4))
                    {
                        //Find the reverse mapper
                        var reverseMapper = GetMapper(item.Item2.Type, item.Item1.Type, false);
                        if (reverseMapper != null)
                        {
                            map.ForMemberBase(item.Item2, item.Item1, item.Item3, reverseMapper.Name);
                        }
                    }
                    else
                    {
                        if (item.Item1.NodeType == ExpressionType.MemberAccess)
                        {
                            map.ForMemberBase(item.Item2, item.Item1, item.Item3, item.Item4);
                        }
                    }
                }
            }


            return(map as MapperConfiguration <TDest, TSource>);
        }
Esempio n. 3
0
 private void CreatBindingFromSimple(ref Tuple <Expression, Expression, bool, string> configExpression, Type typeSource, Type typeTarget, PropertyInfo propTarget)
 {
     // no specific action.
     if (typeSource == typeTarget)
     {
         // We create the binding.
         CreateMemberBinding(configExpression.Item1, propTarget, configExpression.Item3);
     }
     else
     {
         // Try to find a mapper.
         MapperConfigurationBase externalMapper = GetAndCheckMapper(typeSource, typeTarget, configExpression.Item4);
         // If the mapper is not initialized at this moment
         externalMapper.CreateMappingExpression(constructorFunc);
         // By default, check the nullity of the object.
         Expression mapExpression     = externalMapper.GetMemberInitExpression();
         Expression defaultExpression = Expression.Constant(MapperHelper.GetDefaultValue(configExpression.Item1.Type), configExpression.Item1.Type);
         // To change the parameter.
         Expression expSource = visitorMapper.Visit(configExpression.Item1, false);
         ChangParameterExpressionVisitor changeParamaterVisitor = new ChangParameterExpressionVisitor(expSource);
         mapExpression = changeParamaterVisitor.Visit(mapExpression);
         // Now we can create with the good parameters.
         Expression checkIfNull = Expression.NotEqual(expSource, defaultExpression);
         // Create condition.
         var checkExpression = Expression.Condition(checkIfNull, mapExpression,
                                                    Expression.Constant(MapperHelper.GetDefaultValue(mapExpression.Type), mapExpression.Type),
                                                    mapExpression.Type);
         MemberAssignment bindExpression = Expression.Bind(propTarget, checkExpression);
         // We find the mapper and not configured.
         if (string.IsNullOrEmpty(configExpression.Item4))
         {
             configExpression = Tuple.Create(configExpression.Item1, configExpression.Item2, configExpression.Item3, externalMapper.Name);
         }
         memberForNew.Add(bindExpression);
     }
 }