示例#1
0
        /// <summary>
        /// Cleans a <see cref="DateTime"/> value.
        /// </summary>
        /// <param name="value">The value to clean.</param>
        /// <param name="transform">The <see cref="DateTimeTransform"/> to be applied (defaults to <see cref="DefaultDateTimeTransform"/>).</param>
        /// <returns>The cleaned value.</returns>
        public static DateTime Clean(DateTime value, DateTimeTransform transform = DefaultDateTimeTransform)
        {
            switch (transform)
            {
            case DateTimeTransform.DateOnly:
                return(DateTime.SpecifyKind(value.Date, DateTimeKind.Unspecified));

            case DateTimeTransform.DateTimeLocal:
                if (value == DateTime.MinValue || value == DateTime.MaxValue)
                {
                    return(DateTime.SpecifyKind(value, DateTimeKind.Local));
                }
                else
                {
                    return((value.Kind == DateTimeKind.Local) ? value : TimeZoneInfo.ConvertTime(value, TimeZoneInfo.Local));
                }

            case DateTimeTransform.DateTimeUtc:
                if (value == DateTime.MinValue || value == DateTime.MaxValue)
                {
                    return(DateTime.SpecifyKind(value, DateTimeKind.Utc));
                }
                else
                {
                    return((value.Kind == DateTimeKind.Utc) ? value : TimeZoneInfo.ConvertTime(value, TimeZoneInfo.Utc));
                }

            case DateTimeTransform.DateTimeUnspecified:
                return(DateTime.SpecifyKind(value, DateTimeKind.Unspecified));
            }

            return(value);
        }
示例#2
0
        /// <summary>
        /// Cleans a <see cref="Nullable{DateTime}"/> value.
        /// </summary>
        /// <param name="value">The value to clean.</param>
        /// <param name="transform">The <see cref="DateTimeTransform"/> to be applied (defaults to <see cref="DefaultDateTimeTransform"/>).</param>
        /// <returns>The cleaned value.</returns>
        public static DateTime?Clean(DateTime?value, DateTimeTransform transform = DefaultDateTimeTransform)
        {
            if (value == null || !value.HasValue)
            {
                return(value);
            }

            return(Clean(value.Value, transform));
        }
示例#3
0
        /// <summary>
        /// Sets a <see cref="Nullable{DateTime}"/> property value and raises the <see cref="PropertyChanged"/> event where applicable.
        /// </summary>
        /// <param name="propertyValue">The property value to set.</param>
        /// <param name="setValue">The value to set.</param>
        /// <param name="immutable">Indicates whether the value is immutable; can not be changed once set.</param>
        /// <param name="transform">The <see cref="DateTimeTransform"/> to be applied (defaults to <see cref="DateTimeTransform.DateTimeLocal"/>).</param>
        /// <param name="beforeChange">Function to invoke before changing the value; a result of <c>true</c> indicates that the property change is to be cancelled; otherwise, <c>false</c>.</param>
        /// <param name="propertyNames">The names of the properties that changed.</param>
        /// <returns><c>true</c> indicates that the property value changed; otherwise, <c>false</c>.</returns>
        /// <remarks>The first property name specified (see <paramref name="propertyNames"/>) is the primary property; therefore, the only property
        /// where the <see cref="BeforePropertyChanged"/> event is raised. The additional property names allow for the <see cref="PropertyChanged"/>
        /// event to be raised for other properties where related versus having to raise seperately.</remarks>
        protected bool SetValue(ref DateTime?propertyValue, DateTime?setValue, bool immutable = false, DateTimeTransform transform = DateTimeTransform.DateTimeLocal, Func <DateTime?, bool> beforeChange = null, params string[] propertyNames)
        {
            ValidateSetValuePropertyNames(propertyNames);

            lock (_lock)
            {
                DateTime?val       = Cleaner.Clean(setValue, transform);
                var      isChanged = val != propertyValue;
                if (!RaisePropertyChangedWhenSame && !isChanged)
                {
                    return(false);
                }

                if (IsReadOnly && isChanged)
                {
                    throw new InvalidOperationException(EntityIsReadOnlyMessage);
                }

                if (immutable && isChanged && propertyValue != null)
                {
                    throw new InvalidOperationException(ValueIsImmutableMessage);
                }

                if (beforeChange != null)
                {
                    if (beforeChange.Invoke(setValue))
                    {
                        return(false);
                    }
                }

                if (OnBeforePropertyChanged(propertyNames[0], setValue))
                {
                    return(false);
                }

                propertyValue = val;
                TriggerPropertyChanged(propertyNames);
                return(true);
            }
        }
示例#4
0
 /// <summary>
 /// Sets a <see cref="Nullable{DateTime}"/> property value and raises the <see cref="PropertyChanged"/> event where applicable.
 /// </summary>
 /// <param name="propertyValue">The property value to set.</param>
 /// <param name="setValue">The value to set.</param>
 /// <param name="immutable">Indicates whether the value is immutable; can not be changed once set.</param>
 /// <param name="transform">The <see cref="DateTimeTransform"/> to be applied (defaults to <see cref="DateTimeTransform.DateTimeLocal"/>).</param>
 /// <param name="propertyNames">The names of the properties that changed.</param>
 /// <returns><c>true</c> indicates that the property value changed; otherwise, <c>false</c>.</returns>
 /// <remarks>The first property name specified (see <paramref name="propertyNames"/>) is the primary property; therefore, the only property
 /// where the <see cref="BeforePropertyChanged"/> event is raised. The additional property names allow for the <see cref="PropertyChanged"/>
 /// event to be raised for other properties where related versus having to raise seperately.</remarks>
 protected bool SetValue(ref DateTime?propertyValue, DateTime?setValue, bool immutable = false, DateTimeTransform transform = DateTimeTransform.DateTimeLocal, params string[] propertyNames)
 {
     return(SetValue(ref propertyValue, setValue, immutable, transform, null, propertyNames));
 }