Beispiel #1
0
        /// <summary>
        /// Creates and caches a function that is euqivalent to the expression <paramref name="expressionToGetTheFunctionFor"/>.
        ///   The expression is modified to return NULL instead of throwing ReferenceNullExceptions if a part of the expression is null.
        /// </summary>
        /// <param name="expressionToGetTheFunctionFor"> The expression that should be translated into a compiled function  </param>
        /// <typeparam name="TObject"> The type of the object that is the parameter for the function  </typeparam>
        /// <typeparam name="TResult1"> The type of the result of the function  </typeparam>
        /// <returns> The compiled function that is equivalent to the expression  </returns>
        private static Func <TObject, TResult1> GetMethod <TObject, TResult1>(
            Expression <Func <TObject, TResult1> > expressionToGetTheFunctionFor)
        {
            var key = expressionToGetTheFunctionFor + typeof(TObject).FullName;

            if (!Expressions.ContainsKey(key))
            {
                var x = (Expression <Func <TObject, TResult1> >)Modifier.Modify(expressionToGetTheFunctionFor);
                Expressions.Add(key, x.Compile());
            }

            return((Func <TObject, TResult1>)Expressions[key]);
        }
Beispiel #2
0
        /// <summary>
        /// Register a mapping in the private instance.
        /// </summary>
        /// <param name="getEntity"> The get expression. </param>
        /// <param name="setEntity"> The set method. </param>
        /// <typeparam name="TSource">The source object type</typeparam>
        /// <typeparam name="TTarget">The target object type</typeparam>
        public void Register <TSource, TTarget>(
            Expression <Func <TSource, object> > getEntity,
            Action <TTarget, object> setEntity)
        {
            var typeTupel = new Tuple <Type, Type>(typeof(TSource), typeof(TTarget));

            if (!this.mappings.ContainsKey(typeTupel))
            {
                this.mappings.Add(typeTupel, new List <Tuple <object, object> >());
            }

            var mapping           = this.mappings[typeTupel];
            var modifier          = new NullLiftModifier();
            var getEntityFunction = ((Expression <Func <TSource, object> >)modifier.Modify(getEntity)).Compile();

            mapping.Add(new Tuple <object, object>(getEntityFunction, setEntity));
        }
Beispiel #3
0
        /// <summary>
        /// compares old to new and sets the destination if both are different.
        /// </summary>
        /// <param name="dirty"> The dirty-flag (set to true if a modification in the destination object has been done).  </param>
        /// <param name="newSource"> The new std element.  </param>
        /// <param name="oldSource"> The old std element.  </param>
        /// <param name="valueExtractionExpression"> The expression to extract the value from the source type.  </param>
        /// <param name="setter"> The setter method for the destination object.  </param>
        /// <typeparam name="TDestination"> The type of the destination property.  </typeparam>
        /// <typeparam name="TSource"> The type of the source object.  </typeparam>
        public static void MapIfDiffers <TDestination, TSource>(
            ref bool dirty,
            TSource newSource,
            TSource oldSource,
            Expression <Func <TSource, TDestination> > valueExtractionExpression,
            Action <TDestination> setter)
        {
            var modifier = new NullLiftModifier();
            var function =
                ((Expression <Func <TSource, TDestination> >)modifier.Modify(valueExtractionExpression)).Compile();
            var newValue = function(newSource);

            if (Equals(function(oldSource), newValue))
            {
                return;
            }

            setter(newValue);
            dirty = true;
        }