Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For validation that is based on business rules, I usually expose a Validation Delegate that my ViewModel can set.</p> <p>For example, the ViewModel for the Recipe might contain code that looks like this:</p> <pre><code>public GetRecipe(id) { CurrentRecipe = DAL.GetRecipe(id); CurrentRecipe.AddValidationErrorDelegate(ValidateRecipe); } private string ValidateRecipe(string propertyName) { if (propertyName == "TemperatureSetpoint") { var maxTemp = Configuration.MaxTemperatureSetpoint; if (CurrentRecipe.TemperatureSetpoint &gt;= maxTemp ) { return string.Format("Temperature cannot be greater than {0}", maxTemp); } } return null; } </code></pre> <p>The idea is that your <code>Model</code> should only contain raw data, therefore it should only validate raw data. This can include validating things like maximum lengths, required fields, and allowed characters. Business Logic, which includes business rules, should be validated in the <code>ViewModel</code>, and this allows that to happen.</p> <p>The actual implementation of my <code>IDataErrorInfo</code> on the <code>Recipe</code> class would look like this:</p> <pre><code>#region IDataErrorInfo &amp; Validation Members /// &lt;summary&gt; /// List of Property Names that should be validated /// &lt;/summary&gt; protected List&lt;string&gt; ValidatedProperties = new List&lt;string&gt;(); #region Validation Delegate public delegate string ValidationErrorDelegate(string propertyName); private List&lt;ValidationErrorDelegate&gt; _validationDelegates = new List&lt;ValidationErrorDelegate&gt;(); public void AddValidationErrorDelegate(ValidationErrorDelegate func) { _validationDelegates.Add(func); } #endregion // Validation Delegate #region IDataErrorInfo for binding errors string IDataErrorInfo.Error { get { return null; } } string IDataErrorInfo.this[string propertyName] { get { return this.GetValidationError(propertyName); } } public string GetValidationError(string propertyName) { // If user specified properties to validate, check to see if this one exists in the list if (ValidatedProperties.IndexOf(propertyName) &lt; 0) { //Debug.Fail("Unexpected property being validated on " + this.GetType().ToString() + ": " + propertyName); return null; } string s = null; // If user specified a Validation method to use, Validate property if (_validationDelegates.Count &gt; 0) { foreach (ValidationErrorDelegate func in _validationDelegates) { s = func(propertyName); if (s != null) { return s; } } } return s; } #endregion // IDataErrorInfo for binding errors #region IsValid Property public bool IsValid { get { return (GetValidationError() == null); } } public string GetValidationError() { string error = null; if (ValidatedProperties != null) { foreach (string s in ValidatedProperties) { error = GetValidationError(s); if (error != null) { return error; } } } return error; } #endregion // IsValid Property #endregion // IDataErrorInfo &amp; Validation Members </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload