Class FailureMessageStrategyAttribute
Decorates a failure message strategy class to indicate where that strategy class may be used.
Inheritance
Implements
Inherited Members
Namespace: CSF.Validation.Messages
Assembly: CSF.Validation.Abstractions.dll
Syntax
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class FailureMessageStrategyAttribute : Attribute, IGetsMessageProviderTypeMatchingInfoForRule
Remarks
Using [FailureMessageStrategy]
upon a class that provides validation failure messages is a
high-performance way to indicate to the framework when that class should be used to provide the failure
message for any given validation result.
A failure message class is one which implements one of the following interfaces:
When used, every one of the predicate values which is set to a non-null value must be satisfied by the failure (or error) result - a logical AND of all of the values for the result to match. This attribute may be used more then once however, and each separate usage of the attribute upon the same class represents an alternative scenario - a logical OR between each of the times that this attribute is used upon the same class.
Every one of the properties of this attribute represents a predicate value.
You are encouraged to read more about how validation message providers are used and selected at the documentation relating to message providers.
Properties
| Improve this Doc View SourceMemberName
Gets or sets a string which the MemberName of the rule identifier at Identifier must match, if this property is non-null.
Declaration
public string MemberName { get; set; }
Property Value
Type | Description |
---|---|
System.String |
Outcome
Gets or sets a RuleOutcome which the outcome of the current rule must match, if this property is non-null.
Declaration
public RuleOutcome? Outcome { get; set; }
Property Value
Type | Description |
---|---|
System.Nullable<RuleOutcome> |
Remarks
Whilst validation messages are only generated by failure results by default, it is possible to use this property to cause validation messages to be generated other outcomes such as Errored or DependencyFailed.
If this property is set to either null or Failed then the decorated class will be used to generate messages only for Failed outcomes; this is the default behaviour. If this property is set to any value other than failed then the class may be used to generate messages for that other outcome.
ParentValidatedType
Gets or sets a System.Type which the validated type of the parent validated object must match, if this property is non-null.
Declaration
public Type ParentValidatedType { get; set; }
Property Value
Type | Description |
---|---|
System.Type |
Remarks
The parent validated object is typically a model object from which the current validated value was accessed/derived. Most commonly this is a property access, so the parent validated object will be the object with the property from which the currently-validated value is accessed.
More technically, the parent validated object type is found from the ValidationRuleResult by accessing the RuleContext, traversing to the first item within AncestorContexts (which must exist), to ValueInfo and then reading ValidatedType. Developers need not do this themselves though, as the matching process handles this automatically.
RuleInterface
Gets or sets a System.Type which the interface used by the current rule must match, if this property is non-null.
Declaration
public Type RuleInterface { get; set; }
Property Value
Type | Description |
---|---|
System.Type |
RuleName
Gets or sets a string which the RuleName of the rule identifier at Identifier must match, if this property is non-null.
Declaration
public string RuleName { get; set; }
Property Value
Type | Description |
---|---|
System.String |
RuleType
Gets or sets a System.Type which the RuleType of the rule identifier at Identifier must match, if this property is non-null.
Declaration
public Type RuleType { get; set; }
Property Value
Type | Description |
---|---|
System.Type |
Remarks
If this is a non-generic type or a closed generic type then the rule type must match this type precisely. If this is an open generic type then the rule type must be a closed version of that same open generic type, although the precise generic type parameters of that closed version are irrelevant.
ValidatedType
Gets or sets a System.Type which the ValidatedType of the rule identifier at Identifier must match, if this property is non-null.
Declaration
public Type ValidatedType { get; set; }
Property Value
Type | Description |
---|---|
System.Type |
Methods
| Improve this Doc View SourceGetPriority()
Gets a numeric priority indicating the preference that this attribute will take over other attributes, should multiple attributes match.
Declaration
public int GetPriority()
Returns
Type | Description |
---|---|
System.Int32 | A numeric priority value. |
Remarks
If more than one instance of FailureMessageStrategyAttribute matches a ValidationRuleResult, for example the attributes across a number of message-providing classes match, then this priority will be used to tie-break and decide which class to actually use.
The rules for calculating priority are simple. Each of the properties upon the attribute that is set to a non-null value increases the priority by one. The attribute with the highest priority 'wins the tie' if there are multiple matches. So, higher priority will be awarded to attributes which have more specific matching criteria (more predicate values present).
IsMatch(ValidationRuleResult)
Gets a value that indicates whether this attribute is a match for the specified validation rule result.
Declaration
public bool IsMatch(ValidationRuleResult result)
Parameters
Type | Name | Description |
---|---|---|
ValidationRuleResult | result | A validation rule result. |
Returns
Type | Description |
---|---|
System.Boolean | true if this attribute matches the specified result; false otherwise. |
Remarks
This method is used to determine whether a class decorated with this attribute is eligible to be used to provide a message for the specified result.
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | If the |