CSLA .NET

Vibrant discussion about CSLA .NET and using the framework to build great business applications.

Simplifying the process of adding common rules

rated by 0 users
This post has 5 Replies | 2 Followers

Top 200 Contributor
Posts 44
David Posted: Thu, Sep 7 2006 3:14 AM

I have a lot of business rules to add for time and date validation and I am trying to write some code that will simplify the addition of these rules. I am trying to use the new generic common rules from 2.1 which help, but I have the following issue:

I am adding a rule to test that a time value does not exceed 6pm as follows. I have split this up into several lines to make it more readable:

 Dim MyRuleArg As New MaxValueRuleArgs(Of DateTime)("FinishTime", TimeSerial(18, 0, 0))
 ValidationRules.AddRule(AddressOf MaxValue(Of DateTime), MyRuleArg)

This works, but the message is not so good: "FinishTime can not exceed 1/01/0001 6:00:00PM"

Specifically I need to do something about the way it is formatting my maximum time value. Is there an easy way to do this?

 

Top 100 Contributor
Posts 86

A solution would be to create your own MaxValue rule. What would allow you to personalize the message.
Top 10 Contributor
Posts 525

Another option, if you are willing to alter the default CSLA objects, is to provide an overloaded constructor to the MaxValueRuleArgs class that accepts a format string.  Then you can simply add such a string to the code you've included and accomplish your goal.  (You'll want to change the ToString method to use the format string, too!)

Top 100 Contributor
Posts 86
Yes  it's a better solution
Top 10 Contributor
Posts 9,270
Yeah, that's a very good idea - I'll add it to my list of "things to do in a future release"

Rocky

Top 200 Contributor
Posts 44
David replied on Thu, Sep 7 2006 9:20 PM

I looked into this suggestion (overloaded constructor to the MaxValueRuleArgs class) but there is one problem. When the code in the MaxValue function formats the message, it declares a variable of type T, assigns the max value to this variable and then calls the ToString method. So I have no way of intercepting the formatting process.

So what I did was to create my own MaxValue function as well as my own MaxValueRuleArgs(OF T) class. I then added a read only property FormattedMaxValue to MaxValueRuleArgs that inspected the current type and value and did the type specific formatting I required. I then altered MaxValue to call this property instead of the current method (using ToString).

This works nicely for me, and rather than changing CSLA I have just renamed these and included the code in my project.

 

Page 1 of 1 (6 items) | RSS

Copyright (c) 2006-2010 Marimer LLC. All rights reserved.
Email admin@lhotka.net for support.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems