Example #1
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));
        }
Example #2
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;
        }