Back to Devexpress

RuleCombinationOfPropertiesIsUniqueAttribute Class

expressappframework-devexpress-dot-persistent-dot-validation-b3387ee4.md

latest6.1 KB
Original Source

RuleCombinationOfPropertiesIsUniqueAttribute Class

Defines a validation rule that demands the target properties’ values combination be unique.

Namespace : DevExpress.Persistent.Validation

Assembly : DevExpress.Persistent.Base.v25.2.dll

NuGet Package : DevExpress.Persistent.Base

Declaration

csharp
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Interface, AllowMultiple = true)]
public class RuleCombinationOfPropertiesIsUniqueAttribute :
    RuleBaseAttribute,
    IRuleCombinationOfPropertiesIsUniqueProperties,
    IRuleSearchObjectProperties,
    IRuleCollectionPropertyProperties,
    IRuleBaseProperties
vb
<AttributeUsage(AttributeTargets.Class Or AttributeTargets.Property Or AttributeTargets.Interface, AllowMultiple:=True)>
Public Class RuleCombinationOfPropertiesIsUniqueAttribute
    Inherits RuleBaseAttribute
    Implements IRuleCombinationOfPropertiesIsUniqueProperties,
               IRuleSearchObjectProperties,
               IRuleCollectionPropertyProperties,
               IRuleBaseProperties

Remarks

Apply this attribute to a business class to define a validation rule that will check whether a set of the class’ properties values is unique. Use the CriteriaEvaluationBehavior parameter, to specify whether to look for objects that are currently retrieved from the database, in addition to objects in the database itself. In addition, use the common parameters that are inherited from the RuleBaseAttribute class. The properties whose combination must be unique are specified by the attribute constructors’ targetProperties parameter. This string parameter holds a list of property names separated by the comma, semicolon or a whitespace characters.

csharp
using DevExpress.Persistent.BaseImpl.EF;
using DevExpress.Persistent.Validation;
[RuleCombinationOfPropertiesIsUnique("SampleRule", DefaultContexts.Save, "Description, Company")]
public class SampleClass : BaseObject {
    //...
    public virtual string Company { get; set; }
    public virtual string Description { get; set; }
    //...
}

// Make sure that you use options.UseChangeTrackingProxies() in your DbContext settings.
csharp
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;
using DevExpress.Xpo;
[RuleCombinationOfPropertiesIsUnique("SampleRule", DefaultContexts.Save, "Description, Company")]
public class SampleClass: BaseObject {
  public SampleClass(Session session) : base(session) { }
    string company;
    string description;
    [Size(SizeAttribute.DefaultStringMappingFieldSize)]
    public string Description {
        get {
            return description;
        }
        set {
            SetPropertyValue(nameof(Description), ref description, value);
        }
    }

    [Size(SizeAttribute.DefaultStringMappingFieldSize)]
    public string Company {
        get {
            return company;
        }
        set {
            SetPropertyValue(nameof(Company), ref company, value);
        }
    }
}
//...

The rule generated by the RuleCombinationOfPropertiesIsUniqueAttribute 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. This is helpful when you need to add a rule to a class provided by a third party library. For details, refer to the Implement Property Value Validation topic.

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

Note

You can apply RuleUniqueValue, RuleObjectExists, RuleIsReferenced, and RuleCombinationOfPropertiesIsUnique rules to persistent business objects only because they search for objects within the application database. If the rule is applied to a base class that has descendants, the condition is tested against objects of all derived types.

Implements

IRuleCollectionPropertyProperties

IRuleBaseProperties

Inheritance

Object Attribute RuleBaseAttribute RuleCombinationOfPropertiesIsUniqueAttribute

See Also

RuleCombinationOfPropertiesIsUniqueAttribute Members

Validation Rules

DevExpress.Persistent.Validation Namespace