Skip to main content
.NET 6.0+

RuleCriteriaAttribute Class

Defines a validation rule that demands an object of the target type satisfy a specified criterion.

Namespace: DevExpress.Persistent.Validation

Assembly: DevExpress.Persistent.Base.v24.1.dll

Declaration

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Interface, AllowMultiple = true)]
public class RuleCriteriaAttribute :
    RuleBaseAttribute,
    IRuleCriteriaProperties,
    IRuleCollectionPropertyProperties,
    IRuleBaseProperties

Remarks

Apply this attribute to a business class to define a criterion that must be satisfied by this class’ objects. Use the criteria parameter to specify the required criterion. To learn how to write criteria correctly, refer to the Ways to Build Criteria topic. In criteria, you can use Function Criteria Operators. For instance, the “Today = LocalDateTimeToday()” criterion means that the Today property must be set to the current date.

When applying this attribute, use common parameters that are inherited from the RuleBaseAttribute class, in addition to the criteria parameter.

Note that when you need to check whether a property is specified, use the RuleRequiredFieldAttribute, rather than the RuleCriteriaAttribute. However, there are scenarios when you need to use the RuleCriteriaAttribute for this purpose. The following code demonstrates one of them. The Address objects are related to the User and Client objects by the One-To-One relationship. But, the Address object must not have the House property set to 0 when it is assigned to the Client objects.

[DefaultClassOptions]
public class Address : BaseObject {
    public virtual string Street { get; set; }
    public virtual int House { get; set; }
}

[DefaultClassOptions]
[RuleCriteria("", DefaultContexts.Save, "ClientAddress.House != 0",
   "House should not be 0", SkipNullOrEmptyValues = false)]
public class Client : BaseObject {
    public virtual string ClientName { get; set; }
    public virtual Address ClientAddress { get; set; }
}

[DefaultClassOptions]
public class User : BaseObject {
    public virtual string UserName { get; set; }
    public virtual Address UserAddress { get; set; }
}

// Make sure that you use options.UseChangeTrackingProxies() in your DbContext settings.

In the code above, the RuleCriteriaAttribute has the RuleBaseAttribute.SkipNullOrEmptyValues parameter set to false. This is required, so that the rule defined by this attribute is checked when the target property is not specified.

The rule generated by the RuleCriteriaAttribute will be loaded to the Application Model‘s IModelValidationRules node. So, you can customize this rule via the Model Editor. In addition, you can create new rules in the Model Editor as well. For details, refer to the Implement Property Value Validation topic.

You can see examples of using the RuleCriteria and other validation attributes in the Validation section of the FeatureCenter demo. This demo is located in the %PUBLIC%\Documents\DevExpress Demos 24.1\Components\XAF\FeatureCenter.NETFramework.XPO folder, by default.

Inheritance

See Also