/// <summary> /// Initializes a new instance of the <see cref="ValidateWithDataErrorInfo"/> class. /// </summary> /// <param name="appliesWhen">The rule applies when.</param> /// <exception cref="ArgumentOutOfRangeException">appliesWhen;appliesWhen may only be a combination of Insert or Update</exception> public ValidateWithDataErrorInfo(OperationTypes appliesWhen) : base(appliesWhen) { if (appliesWhen.HasFlag(OperationTypes.Select) || appliesWhen.HasFlag(OperationTypes.Delete)) { throw new ArgumentOutOfRangeException("appliesWhen", appliesWhen, "appliesWhen may only be a combination of Insert or Update"); } }
/// <summary> /// Initializes a new instance of the <see cref="ValueRule"/> class. /// </summary> /// <param name="columnName">Name of the column.</param> /// <param name="valueFactory">The value factory.</param> /// <param name="appliesWhen">The applies when.</param> /// <exception cref="ArgumentOutOfRangeException">appliesWhen;appliesWhen may only be a combination of Insert, Update, or Delete</exception> public ValueRule(string columnName, ColumnValueGenerator valueFactory, OperationTypes appliesWhen) : base(columnName, appliesWhen) { if (appliesWhen.HasFlag(OperationTypes.Select)) throw new ArgumentOutOfRangeException("appliesWhen", appliesWhen, "appliesWhen may only be a combination of Insert, Update, or Delete"); ValueFactory = valueFactory; }
/// <summary> /// Initializes a new instance of the <see cref="UserDataRule"/> class. /// </summary> /// <param name="columnName">Name of the column.</param> /// <param name="propertyName">Name of the property.</param> /// <param name="appliesWhen">The rule can be applied to insert, update, and/or soft delete operations.</param> /// <exception cref="ArgumentOutOfRangeException">appliesWhen;appliesWhen may only be a combination of Insert, Update, or Delete</exception> /// <remarks>This will have no effect on hard deletes.</remarks> public UserDataRule(string columnName, string propertyName, OperationTypes appliesWhen) : base(columnName, appliesWhen) { if (appliesWhen.HasFlag(OperationTypes.Select)) throw new ArgumentOutOfRangeException("appliesWhen", appliesWhen, "appliesWhen may only be a combination of Insert, Update, or Delete"); PropertyName = propertyName; }
/// <summary> /// Initializes a new instance of the <see cref="UserDataRule" /> class. /// </summary> /// <param name="columnName">Name of the column.</param> /// <param name="appliesWhen">The rule can be applied to insert, update, and/or soft delete operations.</param> /// <exception cref="ArgumentOutOfRangeException">appliesWhen;appliesWhen may only be a combination of Insert, Update, or Delete</exception> /// <remarks> /// This will have no effect on hard deletes. /// </remarks> public DateTimeOffsetRule(string columnName, OperationTypes appliesWhen) : base(columnName, appliesWhen) { if (appliesWhen.HasFlag(OperationTypes.Select)) { throw new ArgumentOutOfRangeException("appliesWhen", appliesWhen, "appliesWhen may only be a combination of Insert, Update, or Delete"); } }
/// <summary> /// Initializes a new instance of the <see cref="UserDataRule" /> class. /// </summary> /// <param name="columnName">Name of the column.</param> /// <param name="kind">The kind.</param> /// <param name="appliesWhen">The rule can be applied to insert, update, and/or soft delete operations.</param> /// <exception cref="ArgumentOutOfRangeException">appliesWhen;appliesWhen may only be a combination of Insert, Update, or Delete</exception> /// <remarks> /// This will have no effect on hard deletes. /// </remarks> public DateTimeRule(string columnName, DateTimeKind kind, OperationTypes appliesWhen) : base(columnName, appliesWhen) { if (appliesWhen.HasFlag(OperationTypes.Select)) throw new ArgumentOutOfRangeException("appliesWhen", appliesWhen, "appliesWhen may only be a combination of Insert, Update, or Delete"); if (kind == DateTimeKind.Unspecified) throw new ArgumentOutOfRangeException("kind"); Kind = kind; }
/// <summary> /// Initializes a new instance of the <see cref="ValueRule"/> class. /// </summary> /// <param name="columnName">Name of the column.</param> /// <param name="valueFactory">The value factory.</param> /// <param name="appliesWhen">The applies when.</param> /// <exception cref="ArgumentOutOfRangeException">appliesWhen;appliesWhen may only be a combination of Insert, Update, or Delete</exception> public ValueRule(string columnName, ColumnValueGenerator valueFactory, OperationTypes appliesWhen) : base(columnName, appliesWhen) { if (appliesWhen.HasFlag(OperationTypes.Select)) { throw new ArgumentOutOfRangeException(nameof(appliesWhen), appliesWhen, "appliesWhen may only be a combination of Insert, Update, or Delete"); } ValueFactory = valueFactory; }
/// <summary> /// Initializes a new instance of the <see cref="UserDataRule"/> class. /// </summary> /// <param name="columnName">Name of the column.</param> /// <param name="propertyName">Name of the property.</param> /// <param name="appliesWhen">The rule can be applied to insert, update, and/or soft delete operations.</param> /// <exception cref="ArgumentOutOfRangeException">appliesWhen;appliesWhen may only be a combination of Insert, Update, or Delete</exception> /// <remarks>This will have no effect on hard deletes.</remarks> public UserDataRule(string columnName, string propertyName, OperationTypes appliesWhen) : base(columnName, appliesWhen) { if (appliesWhen.HasFlag(OperationTypes.Select)) { throw new ArgumentOutOfRangeException(nameof(appliesWhen), appliesWhen, "appliesWhen may only be a combination of Insert, Update, or Delete"); } PropertyName = propertyName; }
/// <summary> /// Initializes a new instance of the <see cref="UserDataRule" /> class. /// </summary> /// <param name="columnName">Name of the column.</param> /// <param name="value">The value to apply.</param> /// <param name="appliesWhen">The rule can be applied to insert, update, and/or soft delete operations.</param> /// <exception cref="ArgumentOutOfRangeException">appliesWhen;appliesWhen may only be a combination of Insert, Update, or Delete</exception> /// <remarks> /// This will have no effect on hard deletes. /// </remarks> public ValueRule(string columnName, object value, OperationTypes appliesWhen) : base(columnName, appliesWhen) { if (appliesWhen.HasFlag(OperationTypes.Select)) { throw new ArgumentOutOfRangeException("appliesWhen", appliesWhen, "appliesWhen may only be a combination of Insert, Update, or Delete"); } ValueFactory = (av, uv, ov) => value; }
/// <summary> /// Initializes a new instance of the <see cref="UserDataRule" /> class. /// </summary> /// <param name="columnName">Name of the column.</param> /// <param name="kind">The kind.</param> /// <param name="appliesWhen">The rule can be applied to insert, update, and/or soft delete operations.</param> /// <exception cref="ArgumentOutOfRangeException">appliesWhen;appliesWhen may only be a combination of Insert, Update, or Delete</exception> /// <remarks> /// This will have no effect on hard deletes. /// </remarks> public DateTimeRule(string columnName, DateTimeKind kind, OperationTypes appliesWhen) : base(columnName, appliesWhen) { if (appliesWhen.HasFlag(OperationTypes.Select)) { throw new ArgumentOutOfRangeException(nameof(appliesWhen), appliesWhen, "appliesWhen may only be a combination of Insert, Update, or Delete"); } if (kind == DateTimeKind.Unspecified) { throw new ArgumentOutOfRangeException(nameof(kind)); } Kind = kind; }
/// <summary> /// Sets the source property value from <see cref="DatabaseRecord"/>. /// </summary> /// <param name="value">The source value.</param> /// <param name="dr">The <see cref="DatabaseRecord"/>.</param> /// <param name="operationType">The single <see cref="Mapper.OperationTypes"/> being performed to enable selection.</param> public void SetSrceValue(TSrce value, DatabaseRecord dr, OperationTypes operationType) { if (value == null) { throw new ArgumentNullException(nameof(value)); } if (dr == null) { throw new ArgumentNullException(nameof(dr)); } if (!OperationTypes.HasFlag(operationType)) { return; } if (_mapFromDbOverride != null) { typeof(TSrce).GetProperty(SrcePropertyName).SetValue(value, _mapFromDbOverride(dr, value, operationType)); return; } TSrceProperty val = default(TSrceProperty); if (Mapper != null) { var em = (IDatabaseMapper)Mapper; val = (TSrceProperty)em.MapFromDb(dr, operationType, this); } else { int index = dr.GetOrdinal(DestPropertyName); if (!dr.IsDBNull(index)) { if (Converter != null) { val = (TSrceProperty)Converter.ConvertToSrce(Convert.ChangeType(dr.DataRecord.GetValue(index), Converter.DestUnderlyingType)); } else { val = dr.GetValue <TSrceProperty>(index); } } } SetSrceValue(value, val, operationType); }
/// <summary> /// Sets the destination <see cref="DatabaseParameters"/> from the source property value. /// </summary> /// <param name="value">The source value.</param> /// <param name="parameters">The <see cref="DatabaseParameters"/>.</param> /// <param name="operationType">The single <see cref="Mapper.OperationTypes"/> being performed to enable selection.</param> public void SetDestValue(TSrce value, DatabaseParameters parameters, OperationTypes operationType) { if (parameters == null) { throw new ArgumentNullException(nameof(parameters)); } if (!OperationTypes.HasFlag(operationType)) { return; } if (parameters.Contains(DestParameterName)) { return; } if (_mapToDbOverride != null) { _mapToDbOverride(value, parameters, operationType); return; } var val = GetSrceValue(value, operationType); if (Mapper != null) { var em = (IDatabaseMapper)Mapper; em.MapToDb(val, parameters, operationType, this); } else { if (DestDbType.HasValue) { parameters.AddParameter(DestParameterName, Converter == null ? val : Converter.ConvertToDest(val), dbType: DestDbType.Value); } else { parameters.AddParameter(DestParameterName, Converter == null ? val : Converter.ConvertToDest(val)); } } }
/// <summary> /// Sets the source property value from <see cref="DatabaseRecord"/>. /// </summary> /// <param name="value">The source value.</param> /// <param name="dr">The <see cref="DatabaseRecord"/>.</param> /// <param name="operationType">The single <see cref="Mapper.OperationTypes"/> being performed to enable selection.</param> public void SetSrceValue(TSrce value, DatabaseRecord dr, OperationTypes operationType) { if (value == null) { throw new ArgumentNullException(nameof(value)); } if (dr == null) { throw new ArgumentNullException(nameof(dr)); } if (!OperationTypes.HasFlag(operationType)) { return; } if (_mapFromDbOverride != null) { typeof(TSrce).GetProperty(SrcePropertyName).SetValue(value, _mapFromDbOverride(dr, value, operationType)); return; } TSrceProperty val = default !;
/// <summary> /// Conditionally map this path with the specified the <see cref="Beef.Mapper.OperationTypes"/> against the <see cref="IMappingOperationOptions.Items"/> <see cref="OperationTypesName"/> value, evaluated before accessing the source value. /// </summary> /// <typeparam name="TSource">The source entity <see cref="Type"/>.</typeparam> /// <typeparam name="TDestination">The destination entity <see cref="Type"/>.</typeparam> /// <typeparam name="TMember">The member <see cref="Type"/>.</typeparam> /// <param name="pce">The <see cref="IPathConfigurationExpression{TSource, TDestination, TMember}"/>.</param> /// <param name="operationTypes">The <see cref="Beef.Mapper.OperationTypes"/>.</param> /// <remarks>Uses the <see cref="IPathConfigurationExpression{TSource, TDestination, TMember}.Condition(Func{ConditionParameters{TSource, TDestination, TMember}, bool})"/>.</remarks> public static IPathConfigurationExpression <TSource, TDestination, TMember> OperationTypes <TSource, TDestination, TMember>(this IPathConfigurationExpression <TSource, TDestination, TMember> pce, OperationTypes operationTypes) { if (pce == null) { throw new ArgumentNullException(nameof(pce)); } pce.Condition(cp => !cp.Context.Options.Items.TryGetValue(OperationTypesName, out var ot) || operationTypes.HasFlag((OperationTypes)ot)); return(pce); }
/// <summary> /// Conditionally map this member with the specified the <see cref="Beef.Mapper.OperationTypes"/> against the <see cref="IMappingOperationOptions.Items"/> <see cref="OperationTypesName"/> value, evaluated before accessing the source value. /// </summary> /// <typeparam name="TSource">The source entity <see cref="Type"/>.</typeparam> /// <typeparam name="TDestination">The destination entity <see cref="Type"/>.</typeparam> /// <typeparam name="TSourceMember">The source entity member <see cref="Type"/>.</typeparam> /// <param name="mce">The <see cref="IMemberConfigurationExpression{TSource, TDestination, TMember}"/>.</param> /// <param name="operationTypes">The <see cref="Beef.Mapper.OperationTypes"/>.</param> /// <remarks>Uses the <see cref="IMemberConfigurationExpression{TSource, TDestination, TMember}.PreCondition(Func{ResolutionContext, bool})"/>.</remarks> public static IMemberConfigurationExpression <TSource, TDestination, TSourceMember> OperationTypes <TSource, TDestination, TSourceMember>(this IMemberConfigurationExpression <TSource, TDestination, TSourceMember> mce, OperationTypes operationTypes) { if (mce == null) { throw new ArgumentNullException(nameof(mce)); } mce.PreCondition((ResolutionContext rc) => !rc.Options.Items.TryGetValue(OperationTypesName, out var ot) || operationTypes.HasFlag((OperationTypes)ot)); return(mce); }
/// <summary> /// Initializes a new instance of the <see cref="ValidateWithNotifyDataErrorInfo"/> class. /// </summary> /// <param name="appliesWhen">The rule applies when.</param> /// <exception cref="ArgumentOutOfRangeException">appliesWhen;appliesWhen may only be a combination of Insert or Update</exception> public ValidateWithNotifyDataErrorInfo(OperationTypes appliesWhen) : base(appliesWhen) { if (appliesWhen.HasFlag(OperationTypes.Select) || appliesWhen.HasFlag(OperationTypes.Delete)) throw new ArgumentOutOfRangeException("appliesWhen", appliesWhen, "appliesWhen may only be a combination of Insert or Update"); }