Csla.Validation - or how to upgrade from 3.5.x and newer to Csla 4.5

Csla.Validation - or how to upgrade from 3.5.x and newer to Csla 4.5

Old forum URL: forums.lhotka.net/forums/t/12034.aspx


JonnyBee posted on Sunday, June 23, 2013

Hi all, 

In the next beta there will be new assemblies for Csla.Validation in all platforms.

Using these assemblies, existing code that uses CSLA 3.5.x or never will be able to compile and run with a minimum off changes (typically breaking changes) and the old style rule engine signature is supported for synchronous validation rules and authorization rules.

For async validation/business rules the recommended solution is to rewrite these to new style rule classes.

From my own experience thus is the basic changes you must update in your code:

1.  AddAuthorizationRules method no longer exists. Move this code to AddBusinessRules method.  
 
2.  Change code to override CanReadProperty(IPropertyInfo)/CanWriteProperty(IPropertyInfo)/
      CanExecuteMethod(IMethodInfo) in your code (as it is these that gets called by SetProperty/
      GetProperty and the rule engine).
 
3.  Recommended: Create intermediate base classes (if you do not have them already).
 
4.  CommandObjects now make a copy (serialize) before calling Execute (ie: all fields must be serializable).
 
5.  CriteriaBase is generic base class in CSLA 4.5 and must have generic constraint.
 
6.  Recommended: All BO/CommandObjects/CriteriaBase objects should have managed properties whenever possible.
 
7.  ObjectFactory – CommandObjects will call the “Execute” method - not "Update".

8. The "old" style 3.x rule engine supported both Allow/Deny authorization rules (both active).
The Csla 4.x rule engine only supports one rule (so etiher IsInRole or IsNotInRole rule).

9. In AddObjectAuthorizationRules method you must update code to call static methods on the class
Csla.Security.AuthorizationRules
Using the new Csla.Validation assemblies this code from ProjectTracker (in Csla 3.6 Samples) compiles 
and runs fine with CSLA 4.5.vNext:

    #region  Validation Rules     protected override void AddBusinessRules()     {       AuthorizationRules.AllowWrite(NameProperty, "ProjectManager");       AuthorizationRules.AllowWrite(StartedProperty, "ProjectManager");       AuthorizationRules.AllowWrite(EndedProperty, "ProjectManager");       AuthorizationRules.AllowWrite(DescriptionProperty, "Administrator");       ValidationRules.AddRule(Csla.Validation.CommonRules.StringRequired,                               new Csla.Validation.RuleArgs(NameProperty));       ValidationRules.AddRule(         Csla.Validation.CommonRules.StringMaxLength,         new Csla.Validation.CommonRules.MaxLengthRuleArgs(NameProperty, 50));       var args = new Csla.Validation.DecoratedRuleArgs(NameProperty);       args["MaxLength"] = 50;       ValidationRules.AddRule(         Csla.Validation.CommonRules.StringMaxLength,         args);       ValidationRules.AddRule<Project>(StartDateGTEndDate<Project>, StartedProperty);       ValidationRules.AddRule<Project>(StartDateGTEndDate<Project>, EndedProperty);       ValidationRules.AddDependentProperty(StartedProperty, EndedProperty, true);     }     private static bool StartDateGTEndDate<T>(T target, Csla.Validation.RuleArgs e) where T : Project     {       if (target.ReadProperty(StartedProperty) > target.ReadProperty(EndedProperty))       {         e.Description = "Start date can't be after end date";         return false;       }       else       {         return true;       }     }     #endregion     #region  Authorization Rules     protected static void AddObjectAuthorizationRules()     {       // add object-level authorization rules here       Csla.Security.AuthorizationRules.AllowCreate(typeof (Project), "ProjectManager");       Csla.Security.AuthorizationRules.AllowGet(typeof(Project), "Administrator");       Csla.Security.AuthorizationRules.AllowEdit(typeof (Project), "ProjectManager");       Csla.Security.AuthorizationRules.AllowDelete(typeof(Project), "ProjectManager""Administrator");     }     #endregion
Hope this helps you upgrade to the latest CSLA version!!

Copyright (c) Marimer LLC