CommonRules useless messages

CommonRules useless messages

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


chrisb posted on Monday, February 26, 2007

Surely you all have come across this:  I have a property called LastName this is required.  I do this:

ValidationRules.AddRule(CommonRules.StringRequired, "LastName");

The problem is user.BrokenRulesCollection[0].Description returns "LastName required".  I can't display that to the user.  I want to show "Last Name is required."

So, is CommonRules.StringRequired useless to me?  Do I have to write a whole new custom validator just to display my custom message?

I can't believe anybody would really want to display "LastName required" to a user.

Chris

 

ajj3085 replied on Monday, February 26, 2007

You'll have to write your own rule handler that returns the description you want.

JoeFallon1 replied on Monday, February 26, 2007

This topic has been discussed before. Do a search. Rocky may modify CSLA in the future to accomodate this scenario.

In the meantime I wrote code like this in my BO layer:

Public Class MyRuleArgs
 
Inherits RuleArgs

  Private mPropertyDescr As String

  Public ReadOnly Property PropertyDescr() As String
   
Get
     
Return mPropertyDescr
   
End Get
 
End Property

  Public Sub New(ByVal propertyName As String, Optional ByVal propertyDescr As String = "")
   
MyBase.New(propertyName)
    mPropertyDescr = propertyDescr
 
End Sub

  Public Sub New(ByVal propertyName As String, ByVal severity As RuleSeverity, Optional ByVal propertyDescr As String = "")
   
MyBase.New(propertyName)
    mPropertyDescr = propertyDescr
   
Me.Severity = severity
 
End Sub

  Public Sub New(ByVal propertyName As String, ByVal severity As RuleSeverity, ByVal stopProcessing As Boolean, Optional ByVal propertyDescr As String = "")
   
MyBase.New(propertyName)
    mPropertyDescr = propertyDescr
   
Me.Severity = severity
   
Me.StopProcessing = stopProcessing
 
End Sub

End Class

This allows me to use more constructors and to also pass in a value like "First Name" as the property description.

I actually have more code where I load a list of all field names and pull the corresponding description from the list so I do not have to code "First Name" at all in my BOs. But if I wanted to use a special value then whatever I pass in takes precedence over my list. e.g. "My Special First Name"

Joe

RockfordLhotka replied on Monday, February 26, 2007

I'll almost certainly add a friendly name property to RuleArgs in the next version of the framework, yes. And that'll impact CommonRules of course, but also will make it easier to standardize on the same approach for custom rules.

I'm also strongly considering creating a DecoratedRuleArgs that uses the decorator pattern to minimize the need to create custom RuleArgs subclasses. Rather than having to create a custom object for each rule method, you'd be able to use DecoratedRuleArgs and just pass arbitrary name/value pairs into the rule.

The downside, of course, is loss of compile-time checking and type safety. But the upside is a much simpler and more standardized way of passing in arguments to rule methods.

In particular, this technique would simplify code generation rather a lot. Right now a would-be code generator has to somehow determine the type of args object required, and the properties that need to be set on that object. Quite challenging really, especially if you are creating a designer or anything for it. That's made simpler if arbitrary name/value pairs can be passed into the rule.

Jimbo replied on Saturday, March 03, 2007

Another aspect that deters the use of CommonRules is that it does not appear to provide for specifying the use of  a Resource message string.  It seems to use hard coded literal message strings.  fxCop wont pick this oversiight up.

RockfordLhotka replied on Saturday, March 03, 2007

Which rule? They should all be using resource strings.

Rocky

-----Original Message-----
From: "Jimbo"
To: "rocky@lhotka.net"
Sent: 3/3/2007 6:38 PM
Subject: Re: [CSLA .NET] CommonRules useless messages

Another aspect that deters the use of CommonRules is that it does not appear to provide for specifying the use of a Resource message string. It seems to use hard coded literal message strings. fxCop wont pick this oversiight up.


Copyright (c) Marimer LLC