Note that there are some explanatory texts on larger screens.

plurals
  1. POSuggestions for Entity/Business object validation where the validation is dependent on other Entity/Service
    primarykey
    data
    text
    <p><strong>Context</strong></p> <p>For a WPF application using the MVVM pattern I validate my entity(/business object) using the IDataErrorInfo interface on the entity so that validation rules in my entity are automatically called by WPF and the validationerrors automatically appear in the View. (inspired by Josh Smith in this article: <a href="http://joshsmithonwpf.wordpress.com/2008/11/14/using-a-viewmodel-to-provide-meaningful-validation-error-messages/" rel="nofollow">http://joshsmithonwpf.wordpress.com/2008/11/14/using-a-viewmodel-to-provide-meaningful-validation-error-messages/</a> </p> <p>This works OK for simple validation rules like (name > 10 characters, value must be > 0)</p> <p>But what to do when the validation rule in the model is more complex (like name must be unique / max value of the property is defined in another entity). I first thought of solving this by let the entity have a reference to a repository, but this doesn't feel good because I think there should only be references from the repository to the entity and not the other way (creating a cyclic reference)</p> <p><strong>Is it 'legal' to have a reference from the Recipe entity to the ConfigurationRepository. Or do you have a better suggestion?</strong> <strong>Do you have suggestions how to implement Entity/Business object validation where the validation is dependent on other Entity/Service, like in the example below.</strong></p> <p>Below the simplified code of my real world problem. In the Recipe entity I want to validate that the maximum temperature is less than the value stored in Configuration.MaximumTemperature. <strong>How would you solve this?</strong></p> <p><strong>The Configuration entity</strong> (Stores the maximal allowed temperature for a recipe)</p> <pre><code>public class Configuration: INotifyPropertyChanged, IDataErrorInfo { private int _MaxTemperatureSetpoint; public int MaxTemperatureSetpoint { get { return _MaxTemperatureSetpoint; } set { if (value != _MaxTemperatureSetpoint) { _Setpoint = value; RaisePropertyChanged("MaxTemperatureSetpoint"); } } } </code></pre> <p><strong>The Simplified Recipe</strong> (Class where the user configures a recipe with a desired temperature (TemperatureSetpoint) and a desired Time (TimeMilliSeconds). The TemperatureSetpoint must be &lt; Configuration.MaxTemperature)</p> <pre><code>public class Recipe: INotifyPropertyChanged, IDataErrorInfo { private int _TemperatureSetpoint; public int TemperatureSetpoint { get { return _TemperatureSetpoint; } set { if (value != _TemperatureSetpoint) { _Setpoint = value; RaisePropertyChanged("Setpoint"); } } } private int _TimeMilliSeconds; public int TimeMilliSeconds { get { return _TimeMilliSeconds; } set { if (value != _TimeMilliSeconds) { _TimeMilliSeconds= value; RaisePropertyChanged("TimeMilliSeconds"); } } } #region IDataErrorInfo Members public string Error { get { throw new NotImplementedException(); } } public string this[string propertyName] { get { switch(propertyName) { case "TimeMilliSeconds": //TimeMilliSeconds must be &lt; 30 seconds if (TimeMilliSeconds &lt; 30000) { return "TimeMilliSeconds must be &gt; 0 milliseconds";} case "TemperatureSetpoint": //MaxTemperatureSetpoint &lt; maxTemperature stored in the ConfigurationRepository int maxTemperatureSetpoint = ConfigurationRepository.GetConfiguration().MaxTemperatureSetpoint; if (TemperatureSetpoint&gt; maxTemperatureSetpoint ) { return "TemperatureSetpoint must be &lt; " + maxTemperatureSetpoint.ToString();} } } #endregion } </code></pre> <p><strong>Recipe Repository</strong></p> <pre><code>public interface IRecipeRepository { /// &lt;summary&gt; /// Returns the Recipe with the specified key(s) or &lt;code&gt;null&lt;/code&gt; when not found /// &lt;/summary&gt; /// &lt;param name="recipeId"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; TemperatureRecipe Get(int recipeId); .. Create + Update + Delete methods } </code></pre> <p><strong>Configuration Repository</strong></p> <pre><code>public interface IConfigurationRepository { void Configuration GetConfiguration(); } </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. 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