locked
Entity property validation RRS feed

  • Question

  • All examples that I have seen of entity property validation so far use hard coded values in the _Validate method.
    I would like to use a second table that would contain the values used to validate against this would allow greater flexability as a user could set these values and if they needed to change in the future I wouldn't have to make a coding change.
    I have created a table/entity called IndividualDefaults then in the _Validate method of each of the main main table/entity properties  I do something like the code below.
    The problem is I'm calling the defaults table for each property and its a large table/entity. I thought that if I put the LINQ in the constructor of the main entity I would only call it once but the LS entity's dosn't have a constructor.  There is a  _All_PreprocessQuery but that's in a different file and declaring individualDefault there wouldn't work because its not a silverlight library.
     
            partial void LifeUnits_Validate(EntityValidationResultsBuilder results)
            {
    
                try
                {
                    individualDefault = (from t in this.DataWorkspace.CAIPWIndividuals.IndividualDefaults
                                         where t.Id == 1
                                         select t).FirstOrDefault();
    
                    if (LifeUnits < individualDefault.MinLifeUnits)
                        results.AddPropertyError("Life Units less than minimum Life units allowed");
    
                    if (LifeUnits > individualDefault.MaxLifeUnits)
                        results.AddPropertyError("Life Units greater than maximum Life units allowed");
    
                }
                catch
                {
                    results.AddPropertyError("A FATAL ERROR HAS OCCURRED, INDIVIDUALS.DEFAULTS NOT FOUND");
                }
            }
    
    

    Sunday, November 13, 2011 9:13 PM

Answers

  • What about retrieving them in something like Application_Initialize, & caching them in a global variable?

    Yann - LightSwitch Central

    (plus ça change, plus c'est la même chose!)

    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer".

    This will help people find the answers that they're looking for more quickly.

    • Marked as answer by TerryHam Monday, November 14, 2011 2:10 PM
    Monday, November 14, 2011 8:26 AM
    Moderator

All replies

  • What about retrieving them in something like Application_Initialize, & caching them in a global variable?

    Yann - LightSwitch Central

    (plus ça change, plus c'est la même chose!)

    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer".

    This will help people find the answers that they're looking for more quickly.

    • Marked as answer by TerryHam Monday, November 14, 2011 2:10 PM
    Monday, November 14, 2011 8:26 AM
    Moderator
  • You could do something this to hold a static singleton of the values you want. Then you keep context close. Update as needed to fill your validation values as needed.

     

        public partial class Invoice
        {
            private static List<string> _validValues;
            private static List<string> ValidValues
            {
                get
                {
                    if (_validValues == null)
                        _validValues = new List<string>() {"1","2","3","4","5" };
                    return _validValues;
                }
            }
    
            partial void InvoiceTotal_Compute(ref decimal result)
            {
                result = this.InvoiceLineItems.Sum(li => li.ItemTotal);
            }
    
            partial void InvoiceNumber_Validate(EntityValidationResultsBuilder results)
            {
                if (!ValidValues.Contains(this.InvoiceNumber))
                    results.AddPropertyError("Invalid invoice number");
            }
        }

     




    Monday, November 14, 2011 8:50 AM