Exposing Vaidation rules that could benefit the UI?

Exposing Vaidation rules that could benefit the UI?

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


Helen W posted on Thursday, May 11, 2006

First, a huge thank you for an awesome business object framework. I've learned so much just by stepping through your code.

I would like to suggest that some validation rules for properties, e.g. the maximum length rule, could be very useful to the UI developer. Perhaps an attribute for validation rules that affect values easily set for UI controls could allow some of these validation rules to be used by the UI devleoper?

I like my UI to be offer immediate feedback whenever possible, and setting the maximum length that can be entered into a textbox (for example) or a regex that specifies the format for input allows me to give that feedback.

Just a thought.

Helen

Tamir replied on Thursday, May 11, 2006

Not long ago, I did something like that.

Airosafe ICE™  (my company main product, not literally mine…) UI uses heavily this concept. I'll explain:

First, let me mention that the UI is a web application. I wanted to just "Bind" property to textbox and so obtain the following:

 

Let's look at class 'Device' (may represent: PC, Router, Switch etc), it has a 'MacAddress' property. I assigned 'MacAddressRuleAttribute' Attribute to this property.

 

[MacAddressRule()]

public string MacAddress

{

      get...

      set...

}

 

Then in the when I want to bind this property:

 

Binder.Bind(txtMacAddress,macAddressObject,"MacAddress")

 

It's not exactly the way I implemented but you get the idea…

 

You are invited to look at the end result. You may download and install it, or you can just look at a flash demonstration .Notice the red frame when the text box has invalid data, also notice that the Save button is disabled until all controls on the page is valid. Looks familiar..?

I'm on the process of combining my work with csla.net, when I'm done I'll write an article.

 

P.s

In the flash demonstration there a delay with the red frame and the enable state of the save button that caused by the program used to create it. Plus there a nice tooltip that says why the textbox is not valid that is not shown also on the flash demonstration.

 

Tamir

 

BARRY4679 replied on Thursday, May 11, 2006

Hi Helen,

one of the aims is to move business rules, such as validation, out of the UI, and into the BO's. This way the validation rules can be consistently applied by any other UI's, and be available back at the server (if there is one) as a belt-and-braces check just before the db is updated.

Also don't forget that the BO's are present inside the UI, so in that sense the rules are available for immediate feedback without any server roundtrip.

Vaxman2 replied on Thursday, May 11, 2006

I think it would be nice to stop someone from typing a 60 character note into a 10 char field and then being told they can only enter 10 chars max, by setting the textbox maxlength to 10.. It would be the UI developer's choice to implement, and the business object would still do it's own rule checking.

I think someone mentioned making information like this available with attributes on the old forum..

hurcane replied on Friday, May 12, 2006

I've mentioned this several times before on other threads. What is working for us is to define an interface for UI validation. The interface contains the properties that UI controls can use to make the user experience more immediate. Here's an example of how I might state some interfaces:

Interface ITextboxStringInput
ReadOnly Property IsRequired As Boolean
ReadOnly Property IsReadOnly As Boolean
ReadOnly Property MaxLength As Integer
Function TryFormat(ByRef Text as String) As Boolean
End Interface

Interface ITextboxDoubleInput
Readonly Property IsRequired As Boolean
ReadOnly Property IsReadOnly As Boolean
ReadOnly Property Precision As Integer
Function TryFormat(ByRef Text As String) As Boolean
End Interface

The business object needs a dictionary (keyed by property name) to hold objects that support these interfaces. They can be exposed directly as a dictionary or they can be exposed through functions. (We use a function that takes the property name as a parameter.)

For Windows Forms projects, a custom textbox control can get at these properties through the databindings. The datasource of the Text property's binding would be the business object. The binding also exposes the property name on the business object. These properties can be used to retrieve the interface from the data source.

The IsReadOnly and IsRequired properties can be used to change the visual appearance of the control. The TryFormat is used during the KeyPress event to ensure that the format is correct. For the double, for example, a RegEx pattern is used to validate the Text parameter. The parameter is ByRef for strings. The string may need to be converted to uppercase for input, and TryFormat can handle that. You could have the interface simply expose the RegEx pattern. We didn't do that because we couldn't come up with a single regular expression to handle all the acceptable date formats. We allow shortcuts (e.g. 2W for 2 weeks from today), and that is acceptable during TryFormat.

To do this, you need "smart" data objects to implement the interface(s). Ours were fairly easy to develop. For the MaxLength on the string, the smart data object figures it out automatically from the parameters passed during construction, which are a datareader and a column name. The smart data object can access the schema data from the data reader to get the length/size of the column. The Doubles are also able to get their precision from the schema data available through the data reader.

This works great for our Windows Forms projects. I don't know if this is feasible for ASP.NET projects; I've never worked on one.

Copyright (c) Marimer LLC