Wednesday, March 01, 2006
« SPOIL - interesting ideas | Main | Expert VB/C# 2005 Business Objects book ... »

I was recently asked whether I thought it was a good idea to avoid using the Validation events provided by Microsoft (in the UI), in favor of putting the validation logic into a set of objects. I think the answer to the question (to some degree) depends on whether you are doing Windows or Web development.

 

With Windows development, Windows Forms provides all the plumbing you need to put all your validation in the business objects and still have a very rich, expressive UI - with virtually no code in the UI at all. This is particularly true in .NET 2.0, but is quite workable in .NET 1.1 as well.

 

With Web development life isn't as nice. Of course there's no way to run real code in the browser, so you are stuck with JavaScript. While the real authority for any and all business logic must reside on the web server (because browsers are easily compromised), many web applications duplicate validation into the browser to give the user a better experience. While this is expensive and unfortunate, it is life in the world of the Web.

 

(Of course what really happens with a lot of Web apps is that the validation is only put into the browser - which is horrible, because it is too easy to bypass. That is simply a flawed approach to development...)

 

At least with ASP.NET there are the validation controls, which simplify the process of creating and maintaining the duplicate validation logic in the browser. You are still left to manually keep the logic in sync, but at least it doesn't require hand-coding JavaScript in most cases.

 

 

Obviously Windows Forms is an older and more mature technology (or at least flows from an older family of technologies), so it is no surprise that it allows you to do more things with less effort. But in most cases the effort to create Web Forms interfaces isn't bad either.

 

In any case, I do focus greatly on keeping code out of the UI. There's nothing more expensive than a line of code in the UI - because you _know_ it has a half-life of about 1-2 years. Everyone is rewriting their ASP.NET 1.0 UI code to ASP.NET 2.0. Everyone is tweaking their Windows Forms 1.0 code for 2.0. And all of it is junk when WinFX comes out, since WPF is intended to replace both Windows and Web UI development in most cases. Thus code in the UI is expensive, because you'll need to rewrite it in less than 2 years in most cases.

 

Code in a business object, on the other hand, is far less expensive because most business processes don't change nearly as fast as the UI technologies provided by our vendors... As long as your business objects conform to the basic platform interfaces for data binding, they tend to flow forward from one UI technology to the next. For instance, WPF uses the same interfaces as Windows Forms, so reusing the same objects from Windows Forms behind WPF turns out to be pretty painless. You just redesign the UI and away you go.

 

A co-worker at Magenic has already taken my ProjectTracker20 sample app and created a WPF interface for it – based on the same set of CSLA .NET 2.0 business objects as the Windows Forms and Web Forms interfaces. Very cool!

 

So ultimately, I strongly believe that validation (and all other business logic) should be done in a set of business objects. That’s much of the focus in my upcoming Expert VB 2005 and C# 2005 Business Objects books. While you might opt to duplicate some of the validation in the UI for a rich user experience, that’s merely an unfortunate side-effect of the immature (and stagnant) state of the HTML world.


Wednesday, March 01, 2006 4:24:03 PM (Central Standard Time, UTC-06:00)
Rocky,

I just placed my VB 2005 book order with Amazon... got those puppies shipped yet? How about $75 for a direct mail? :)

Agree... always validate in the BO, duplicate in web UI if you must (sometimes you just gotta do what you gotta do in the land of web... particularly hosted web).
Mike
Thursday, March 02, 2006 9:19:15 AM (Central Standard Time, UTC-06:00)
Hi Rocky,

You wouldn't have some screenshots handy of that WPF Project Tracker port, would you?

WPF goodness. :-)
Jeremy Wiebe
Thursday, March 02, 2006 9:31:33 AM (Central Standard Time, UTC-06:00)
If you want to do server side validation for web forms, consider doing it using AJAX. Of all the things you can do in AJAX, validation of client side data is one of the few things that actually make a good deal of sense.

It is not really that complicated. Write a javascript "validationCheck(params);" that takes in all the stuff from the form, write a simple validation aspx that returns a simple response stream that contains a return code that the callback js functioncan understand, and write a callback js function that can either submit the form on an OK return code, or show the appropriate validation message if not OK.

Of course, it isn't OO - and won't be until JS gets reinvented as a true OO language, but it will work, and at least has the feature of avoiding having to duplicate the validation code in the browser (as in, the validator aspx that does the actual validation calls the same objects as you would call when you do the server side validation).
Thursday, March 02, 2006 1:53:03 PM (Central Standard Time, UTC-06:00)
Jeremy,

I am that co-worker handling that WPF application. Right now it is just a proof and works well with CSLA .NET 2.0, but it is just to darn ugly to release screen shots. When I get another chunk of time I will get this cleaned up and released (Still dont expect much WPF goodness. I aint no Graphic Designer.)
Friday, March 03, 2006 1:37:57 PM (Central Standard Time, UTC-06:00)
Be careful with validation logic in general. Does everthing have to be perfect to allow the user to save the data? My users may be working on a quote for a client over several days. Business rules state that certain fields must not be null for the quote to be valid. Do I allow the user to save the quote if the fields are not filled? Of course I do. It just isn't saved as a "completed" quote. I allow quotes to be saved with broken rules. It just can't be marked as complete until all their are no broken rules. So regardless of where the validation takes place, keep this scenario in mind.
Matt Janofsky
Friday, March 03, 2006 7:19:05 PM (Central Standard Time, UTC-06:00)
Dont you think with using AJAX you can put ALL of it in your business objects and get the same exact rich experience you have with WinForms for validation? I do not see why not, and by using Anthem (AJAX Support for .NET) controls you can have rich controls that can get/set data from the server without refreshing your page. Some call this Web 2.0 but I just think its using current technology thats available with some cleverness.

Saturday, March 04, 2006 9:58:55 AM (Central Standard Time, UTC-06:00)
I find the AJAX thing to be somewhat amusing. Back in 1995 when Microsoft came out with MTS, a lot of people tried putting their business logic in MTS on the server, and having the client call the server every time a user left a textbox - to get Windows-style interaction with centralized validation logic on the server.

Of course it was an abysmal failure. That model doesn't scale and tends to perform terribly. And yet this is _exactly_ what many people want to do with AJAX. So I'm left wondering what's different today as compared to back then?

Certainly the speed of light hasn't changed, so bits can round-trip from client to server to client faster.

But server CPUs are faster, and servers have lots more memory. So perhaps that's enough to make the difference. Maybe hardware has sped up enough over the past decade, such that such an inherently inefficient and expensive architecture can be made to actually work in a practical manner.

Years ago people whined and complained about software bloat. How the quality of software decreased as the hardware increased. Methinks they were right :)
Sunday, March 05, 2006 10:19:20 AM (Central Standard Time, UTC-06:00)
Rocky writes...

"Back in 1995 when Microsoft came out with MTS, a lot of people tried putting their business logic in MTS on the server, and having the client call the server every time a user left a textbox - to get Windows-style interaction with centralized validation logic on the server."

I remember those heady days. If you were doing Microsoft platform development of any consequence in the mid 90s, you were on at least one such project.

The problem is that a purist approach that does not allow for common sense (e.g. thou shalt not have business logic - defined as any kind of validation by some - on the client) - the result is usually distorted (as in worse) performance, such as what you observed.

Of course, if you put a simple client side validation suite on the client (e.g. data format checker, "is it a number", "is the email address valid", etc.), you can save your AJAX round trips for things that really matter (non-trivial business logic that has a capacity to change more than once in a blue moon).
Monday, March 06, 2006 9:12:13 AM (Central Standard Time, UTC-06:00)
Yes, I agree. I'm not saying AJAX is bad - look at some of what Google has done for proof that it can be quite good - but I hear quite a lot of people proposing to use AJAX for things that have historically not worked. Things that have been tried, and that failed.

I think it is a common failing in our industry that we don't learn from the past. Moreso than in many other industries I think. Certainly it is a common _human_ failing, but you don't see too many cases where carpenters throw out the lessons of history, or accountants and certainly not lawyers. Heck, even sales people examine the past to decide how to proceed into the future. :)
Tuesday, March 07, 2006 7:03:14 AM (Central Standard Time, UTC-06:00)
How about putting logic in the database? I work in a bank now * and all IT people thinks that is the better idea...
Tuesday, March 07, 2006 7:18:52 AM (Central Standard Time, UTC-06:00)
Problem with putting logic in the database is you ultimatly duplicate the logic or UI responsiveness is very poor. If you wait until the database logic is executed your wasting a great deal of user time and processor cycles to tell them a field is required, and also take up database cpu cycles doing validation logic. So what do people start doing? They end up putting logic in the UI and end up maintaining multiple copies of the same logic, usually in different languages. Thus the reason for CSLA Mobile Objects. Have the objects that contain the business rules move around and stay close to where the processing is currently occurring.
Monday, March 20, 2006 4:30:49 PM (Central Standard Time, UTC-06:00)
"Many people choose the browser for wide reach, but still really want to provide a Windows-like experience for their users," he said. "Atlas is a good step toward achieving that goal. Obviously, the browser is still a long way from a rich client, but using Atlas we've been able to provide field-by-field validation of data as the user tabs off each field, which is a big issue for user productivity.

"Also, we've been able to radically reduce the number of page refreshes, which makes Web pages feel much more like a Windows application, leading to happier users and more productivity."

So - this field by field stuff you are talking about in this article:

http://www.eweek.com/article2/0,1895,1939971,00.asp

... how does it differ from what I described above (using AJAX to allow for server side non-trivial business rule validation). You are talking about using Atlas, which is MS-ified AJAX, to do field level validation as the user tabs.

Are you proposing somehow integrating CLSA 2.0 with Atlas somehow?

Questions, questions, questions...
Comments are closed.