예제 #1
0
        public static void IgnoreSourceWhenDefault <TSource, TDestination>(this IMemberConfigurationExpression <TSource, TDestination, object> opt)
        {
            var    destinationType = opt.DestinationMember.GetMemberType();
            object defaultValue    = destinationType.GetTypeInfo().IsValueType ? Activator.CreateInstance(destinationType) : null;

            opt.Condition((src, dest, srcValue) => !Equals(srcValue, defaultValue));
        }
 public static void IfSafeAgainst <T, TException>(this IMemberConfigurationExpression <T> options, Func <T, object> get)
     where TException : Exception
 {
     return(options.Condition(source => {
         try { var value = get(source); return true; }
         catch (TException) { return false; }
     }));
 }
 public static void IfSafeAgainst <T, TException>(this IMemberConfigurationExpression <T> options, Func <T, object> get)
     where TException : Exception
 {
     return(options.Condition(source => {
         try { string value = source.PositionFolder; return true; }
         catch (StrongTypingException) { return false; }
     }));
 }
        /// <summary>
        /// Strings the condition.
        /// </summary>
        /// <param name="map">The map.</param>
        /// <param name="expression">The expression.</param>
        public static void StringCondition(PropertyMap map, IMemberConfigurationExpression expression)
        => expression.Condition(
            (source, destination, sourceValue, sourceDestination, context) =>
        {
            if (!string.IsNullOrWhiteSpace((string)sourceValue))
            {
                return(true);
            }

            return(false);
        }
            );
        /// <summary>
        /// Nullables the value type condition.
        /// </summary>
        /// <param name="map">The map.</param>
        /// <param name="expression">The expression.</param>
        public static void NullableValueTypeCondition(PropertyMap map, IMemberConfigurationExpression expression)
        => expression.Condition(
            (source, destination, sourceValue, sourceDestination, context) =>
        {
            if (sourceValue != null)
            {
                return(true);
            }

            return(false);
        }
            );
        public static void IgnoreSourceWhenDefault <TSource, TDestination>(this IMemberConfigurationExpression <TSource, TDestination, object> opt)
        {
            opt.Condition((src, dest, srcValue) =>
            {
                var propertyInfo = src.GetType().GetProperty(opt.DestinationMember.Name);
                var srcPropValue = propertyInfo.GetValue(src, null);
                bool isNull      = srcPropValue == null;

                bool useSource = !isNull;
                return(useSource);
            });
        }
    /// <summary>
    ///     Strings the condition.
    /// </summary>
    /// <param name="map">The map.</param>
    /// <param name="expression">The expression.</param>
    public static void StringCondition(PropertyMap map, IMemberConfigurationExpression expression)
    {
        expression.Condition(
            (_, _, sourceValue, _, _) =>
        {
            if (!string.IsNullOrWhiteSpace((string)sourceValue))
            {
                return(true);
            }

            return(false);
        }
            );
    }
    /// <summary>
    ///     Nullables the value type condition.
    /// </summary>
    /// <param name="map">The map.</param>
    /// <param name="expression">The expression.</param>
    public static void NullableValueTypeCondition(PropertyMap map, IMemberConfigurationExpression expression)
    {
        expression.Condition(
            (_, _, sourceValue, _, _) =>
        {
            if (sourceValue != null)
            {
                return(true);
            }

            return(false);
        }
            );
    }
        /// <summary>
        /// Values the type condition.
        /// </summary>
        /// <param name="map">The map.</param>
        /// <param name="expression">The expression.</param>
        public static void ValueTypeCondition(PropertyMap map, IMemberConfigurationExpression expression)
        {
            var defaultValue = Activator.CreateInstance(map.SourceType);

            expression.Condition(
                (source, destination, sourceValue, sourceDestination, context) =>
            {
                if (!Equals(defaultValue, sourceValue))
                {
                    return(true);
                }

                return(false);
            }
                );
        }
 /// <summary>
 /// Skips mapping an expression if source value is null, zero or DateTime.MinValue.
 /// </summary>
 /// <param name="expression">Auto mapper configuration expression</param>
 public static void IgnoreIfSourceIsNullOrZero <TSource, TDestination, TMember>(this IMemberConfigurationExpression <TSource, TDestination, TMember> expression)
 {
     expression.Condition((source, destination, sourceMember) => IgnoreIfSourceIsNullOrZero(source, destination, sourceMember));
 }
 private static void ApplyIgnoreNullOrEmptyConfiguration <TSrc, TDest>(IMemberConfigurationExpression <TSrc, TDest, object> opts)
 {
     opts.Condition((_, __, srcMember) => !CheckIfNullOrEmpty(srcMember));
 }
 public static void IgnoreIfSourceIsNull <T>(this IMemberConfigurationExpression <T> expression)
 {
     expression.Condition(IgnoreIfSourceIsNull);
 }
예제 #13
0
 /// <summary>
 /// 加载实体对象。
 /// <remarks></remarks>
 /// </summary>
 public void LoadEntity <TSource, TMember, TKey>(IMemberConfigurationExpression <TSource> opt,
                                                 Func <TSource, TKey> getId, Func <TKey, TMember> doLoad) where TMember : class
 {
     opt.Condition(src => (getId(src) != null));
     opt.MapFrom(src => CheckIdIsValid <TKey>(getId(src)) ? null : doLoad(getId(src)));
 }