Hi everybody. It's been a while. A lot has been going on lately in CslaGenerator Land and I wanted to post about what's coming. For background on what's been going on you can read
this.
Let's start with something that has been needing some work for a while, rules:
I've done a lot of work here, adding a lot of flexibility. Here's the list of new properties for the rules:
- ArgumentType: You can choose between RuleArgs and DecoratedRuleArgs. (Csla 3.0 and beyond).
- DecoratorArgs: This is a collection where you can define the decorator arguments. It has 3 properties: Name, Value and ValueType.
- DescriptionType: String/Code. The difference here is that if you choose code, it doesn't output quotes for the description, allowing you to get the string from a function, resource or whatever.
- Mode: Generated/Custom/CustomGeneric. Generated is the standard way and it's what we've always had so far. Custom only does the AddRule() call without generating an actual rule method so that you can code it by hand or call an external validation routine. CustomeGeneric is the same as custom, the difference is that Custom uses "object" as the target type and custom generic uses the business object's type as the target type.
Other improvements to rules:
Description can now use a format string, where {0} will be passed the friendly name of the property.
Assert expression now also uses format strings, so you can use {0} for the field.
Also, for both assert expression and description, if you have decorator args, you'll get them as {1}...{n}.
Example for a string max length rule.
Here's the decorator args (only one argument here):
The result looks like this (reformatted a bit so that it's a bit more readable):
Public Shared Function CustNameMaxLenth(ByVal target As Customer, _
ByVal e As Csla.Validation.DecoratedRuleArgs) As Boolean
Dim result As Boolean = _
target.m_customerName.Length < DirectCast(e("MaxLength"), Int32)
If result = False Then
e.Description = String.Format( _
"{0} must be less than {1} characters.", _
"Customer Name", e("MaxLength").ToString())
e.Severity = Csla.Validation.RuleSeverity.Error
End If
Return result
End Function
Protected Overrides Sub AddBusinessRules()
'Rules for "CustomerName"
Dim StrMaxLenthArgs As New DecoratedRuleArgs("CustomerName")
StrMaxLenthArgs("MaxLength") = 10
ValidationRules.AddRule(Of Customer, DecoratedRuleArgs)( _
AddressOf CustomerRules.StrMaxLenth, StrMaxLenthArgs, 0)
End Sub
Now, while this introduces a lot of flexibility and power to rule definitions in CslaGenerator, I bet the question will arise:
"Does it create some default rules automatically now?"
The short answer is NO. Rules are really very tied to your business, locale and other variables, and while the app could create string max length rules for example, you may not like the default message to name one thing.
Instead, there will be other ways to define rules automatically that will be entirely under your control. I'll explain this in another post, since it doesn't apply to rules exclusively. I'll give you one hint: PLUGINS.
Andrés