/// <summary> /// Initializes a new instance of the <see cref="DependsOnClause{TEntity, TProperty}"/> class. /// </summary> /// <param name="dependsOnExpression">The <see cref="Expression"/> to reference the compare to entity property.</param> public DependsOnClause(Expression <Func <TEntity, TProperty> > dependsOnExpression) { // Validate the expression. Beef.Check.NotNull(dependsOnExpression, nameof(dependsOnExpression)); _dependsOn = PropertyExpression.Create(dependsOnExpression, true); }
/// <summary> /// Initializes a new instance of the <see cref="PropertyRule{TEntity, TProperty}"/> class. /// </summary> /// <param name="propertyExpression">The <see cref="LambdaExpression"/> to reference the entity property.</param> public PropertyRule(Expression <Func <TEntity, TProperty> > propertyExpression) { _property = PropertyExpression.Create(propertyExpression, true); Name = _property.Name; JsonName = _property.JsonName; Text = _property.Text; }
/// <summary> /// Adds (or gets) a <see cref="DatabasePropertyMapper{TSrce, TSrceProperty}"/>. /// </summary> /// <typeparam name="TSrceProperty">The source property <see cref="Type"/>.</typeparam> /// <param name="srcePropertyExpression">The <see cref="Expression"/> to reference the source entity property.</param> /// <param name="columnName">The database column name.</param> /// <param name="operationTypes">The <see cref="Mapper.OperationTypes"/> selection to enable inclusion or exclusion of property (default to <see cref="OperationTypes.Any"/>).</param> /// <param name="property">An <see cref="Action"/> enabling access to the created <see cref="DatabasePropertyMapper{TSrce, TSrceProperty}"/>.</param> /// <returns>The <see cref="DatabaseMapper{TEntity}"/>.</returns> public DatabaseMapper <TSrce> HasProperty <TSrceProperty>(Expression <Func <TSrce, TSrceProperty> > srcePropertyExpression, string columnName = null, OperationTypes operationTypes = OperationTypes.Any, Action <DatabasePropertyMapper <TSrce, TSrceProperty> > property = null) { if (srcePropertyExpression == null) { throw new ArgumentNullException(nameof(srcePropertyExpression)); } var spe = PropertyExpression <TSrce, TSrceProperty> .Create(srcePropertyExpression); var px = GetBySrcePropertyName(spe.Name); if (px != null && px.DestPropertyName != columnName) { throw new ArgumentException($"Source property '{srcePropertyExpression.Name}' mapping already exists with a different destination column name"); } DatabasePropertyMapper <TSrce, TSrceProperty> p = null; if (px == null) { p = Property(srcePropertyExpression, columnName, operationTypes); } else { p = (DatabasePropertyMapper <TSrce, TSrceProperty>)px; } property?.Invoke(p); return(this); }
/// <summary> /// Adds (or gets) a source <see cref="PropertySrceMapper{TSrce, TSrceProperty, TDest}"/>. /// </summary> /// <typeparam name="TSrceProperty">The source property <see cref="Type"/>.</typeparam> /// <param name="srcePropertyExpression">The <see cref="Expression"/> to reference the source entity property.</param> /// <param name="property">An <see cref="Action"/> enabling access to the created <see cref="PropertyMapper{TSrce, TSrceProperty, TDest, TDestProperty}"/>.</param> /// <returns>The <see cref="EntityMapper{TSrce, TDest}"/>.</returns> public virtual EntityMapper <TSrce, TDest> HasSrceProperty <TSrceProperty>(Expression <Func <TSrce, TSrceProperty> > srcePropertyExpression, Action <PropertySrceMapper <TSrce, TSrceProperty, TDest> > property = null) where TSrceProperty : class { if (srcePropertyExpression == null) { throw new ArgumentNullException(nameof(srcePropertyExpression)); } var spe = PropertyExpression <TSrce, TSrceProperty> .Create(srcePropertyExpression); var px = GetBySrcePropertyName(spe.Name); if (px != null && (px.DestPropertyName != null)) { throw new ArgumentException($"Source property '{srcePropertyExpression.Name}' mapping already exists with a different destination property name"); } PropertySrceMapper <TSrce, TSrceProperty, TDest> p = null; if (px == null) { p = new PropertySrceMapper <TSrce, TSrceProperty, TDest>(spe); AddPropertyMapper(p); } else { p = (PropertySrceMapper <TSrce, TSrceProperty, TDest>)px; } property?.Invoke(p); return(this); }
/// <summary> /// Initializes a new instance of the <see cref="DependsOnClause{TEntity, TProperty}"/> class. /// </summary> /// <param name="dependsOnExpression">The <see cref="Expression"/> to reference the compare to entity property.</param> public DependsOnClause(Expression <Func <TEntity, TProperty> > dependsOnExpression) { // Validate the expression. if (dependsOnExpression == null) { throw new ArgumentNullException("dependsOnExpression"); } _dependsOn = PropertyExpression <TEntity, TProperty> .Create(dependsOnExpression, true); }
/// <summary> /// Initializes a new instance of the <see cref="CompareValueRule{TEntity, TProperty}"/> class specifying the compare to property. /// </summary> /// <param name="compareOperator">The <see cref="CompareOperator"/>.</param> /// <param name="compareToPropertyExpression">The <see cref="Expression"/> to reference the compare to entity property.</param> /// <param name="compareToText">The compare to text <see cref="LText"/> to be passed for the error message (default is to derive the text from the property itself).</param> public ComparePropertyRule(CompareOperator compareOperator, Expression <Func <TEntity, TCompareProperty> > compareToPropertyExpression, LText compareToText = null) : base(compareOperator) { if (compareToPropertyExpression == null) { throw new ArgumentNullException("compareToPropertyExpression"); } _compareTo = PropertyExpression <TEntity, TCompareProperty> .Create(compareToPropertyExpression, true); _compareToText = compareToText; }
/// <summary> /// Specifies that the collection is to be checked for duplicates using the specified item property. /// </summary> /// <typeparam name="TItemProperty">The item property <see cref="Type"/>.</typeparam> /// <param name="propertyExpression">The <see cref="Expression"/> to reference the item property that is being duplicate checked.</param> /// <param name="duplicateText">The duplicate text <see cref="LText"/> to be passed for the error message (default is to derive the text from the property itself where possible).</param> /// <returns>The <see cref="CollectionRuleItem{TItemEntity}"/> instance to support chaining/fluent.</returns> public CollectionRuleItem <TItemEntity> DuplicateCheck <TItemProperty>(Expression <Func <TItemEntity, TItemProperty> > propertyExpression, LText?duplicateText = null) { if (_duplicateCheck) { throw new InvalidOperationException("A DuplicateCheck or UniqueKeyDuplicateCheck can only be specified once."); } _propertyExpression = PropertyExpression.Create(Check.NotNull(propertyExpression, nameof(propertyExpression)), true); _duplicateText = duplicateText ?? _propertyExpression.Text; _duplicateCheck = true; return(this); }
/// <summary> /// Initializes a new instance of the <see cref="CompareValueRule{TEntity, TProperty}"/> class specifying the compare to property. /// </summary> /// <param name="compareOperator">The <see cref="CompareOperator"/>.</param> /// <param name="compareToPropertyExpression">The <see cref="Expression"/> to reference the compare to entity property.</param> /// <param name="compareToText">The compare to text <see cref="LText"/> to be passed for the error message (default is to derive the text from the property itself).</param> public ComparePropertyRule(CompareOperator compareOperator, Expression <Func <TEntity, TCompareProperty> > compareToPropertyExpression, LText?compareToText = null) : base(compareOperator) { Beef.Check.NotNull(compareToPropertyExpression, nameof(compareToPropertyExpression)); _compareTo = PropertyExpression.Create(compareToPropertyExpression, true); _compareToText = compareToText; }
/// <summary> /// Creates the property expression from the expression. /// </summary> private PropertyExpression <TEntity, TProperty> CreatePropertyExpression <TProperty>(Expression <Func <TEntity, TProperty> > propertyExpression) { return(PropertyExpression <TEntity, TProperty> .Create(propertyExpression, true)); }
/// <summary> /// Initializes a new instance of the <see cref="PropertyRule{TEntity, TProperty}"/> class. /// </summary> /// <param name="propertyExpression">The <see cref="LambdaExpression"/> to reference the entity property.</param> public PropertyRule(Expression <Func <TEntity, TProperty> > propertyExpression) : this(PropertyExpression.Create(propertyExpression, true)) { }
/// <summary> /// Throws a <see cref="ValidationException"/> where the <see cref="MessageItem"/> <see cref="MessageItem.Property"/> is set based on the <paramref name="propertyExpression"/>. The property /// friendly text and <paramref name="propertyValue"/> are automatically passed as the first two arguments to the string formatter. /// </summary> /// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam> /// <param name="propertyExpression">The <see cref="Expression"/> to reference the entity property.</param> /// <param name="format">The composite format string.</param> /// <param name="propertyValue">The property values (to be used as part of the format).</param> /// <param name="values"></param> public void ThrowValidationException <TProperty>(Expression <Func <TEntity, TProperty> > propertyExpression, LText format, TProperty propertyValue, params object[] values) { var p = PropertyExpression.Create(propertyExpression, true); throw new ValidationException(MessageItem.CreateErrorMessage(ValidationArgs.DefaultUseJsonNames ? p.JsonName : p.Name, string.Format(System.Globalization.CultureInfo.CurrentCulture, format, new object[] { p.Text, propertyValue ! }.Concat(values).ToArray())));
/// <summary> /// Throws a <see cref="ValidationException"/> where the <see cref="MessageItem"/> <see cref="MessageItem.Property"/> is set based on the <paramref name="propertyExpression"/>. /// </summary> /// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam> /// <param name="propertyExpression">The <see cref="Expression"/> to reference the entity property.</param> /// <param name="text">The message text.</param> public void ThrowValidationException <TProperty>(Expression <Func <TEntity, TProperty> > propertyExpression, LText text) { var p = PropertyExpression.Create(propertyExpression, true); throw new ValidationException(MessageItem.CreateErrorMessage(ValidationArgs.DefaultUseJsonNames ? p.JsonName : p.Name, text)); }