Data constraints in a multi-tier application

Wednesday, 19 April 2006

I’m wondering about what are the best practises, particularly in a .NET world for this kind of scenario, which must be pretty common. My goals are to a) have decent performance and b) have as few instances of separate (but identical) constant values as possible. Here’s the scenario:

  1. You have a database with some tables, and say a table has a varchar column with a maximum length of 255 characters. All good so far! (Constant count = 1)
  2. You then create a .NET 2.0 DataSet with TableAdapters for that table, so now the DataSet also knows that the column has a MaxLength of 255. (Constant count = 2)
  3. You now create a business object which has a property which also maps to the same column (and would be persisted by passing it back through the DataSet/TableAdapter from step 2). You add checks to this property to ensure it also never gets larger than the maximum length allowed. (Constant count = 3)
  4. You create a web form (or a Windows Forms for that matter) that has a text field for the user to edit the same column. You set the maxlength attribute of that text field so the browser restricts the user to the size limit for that field. You also add a server-side validator to validate the text field and enforce that size limit (just in case their web browser ignores the maxlength attribute). (Constant count = 4)

We’ve managed to embed the value ‘255’ at least 4 times in our application! While that may not bother some people, it it does annoy me. The concept of having a single constant that represents a value and allows you to easily change the value in one place should you need to is something I’ve adopted for a long time.

So how can we reduce this down?

Assuming that we’re going to stick with a normal relational database and keep the limit for the column there, then that’s one instance we can’t remove.

It is true that we could use database queries to retrieve that value and use it all over our app, but I’m pretty sure the performance for that would not be ideal, and it does mean that some potential compile-time checks might be missed.

If you assume that your UI tier only talks to the business layer, and the business layer is the only thing that talks to the data layer, what is the best way to percolate through these kind of values with as few separate constants as possible? If I change the length of the column in the database, I want to change as few constants in my code as possible, so that there’s less chance I miss one and cause a problem with the application.

I’d be interested in other people’s thoughts, or if anyone has links to content that covers this topic.

"Hey, that's me!"

Friday, 14 April 2006

I just noticed there’s a link to my blog on the Australian SQL Server User Group home page - I’m not sure how long it’s been there, but then I don’t always look up in the top-right corner :-)

Which reminds me, when I have some spare time I need to see if I can fix the layout problem with their Forums area in Firefox. I also put my hand up to add some RSS feeds to the site which I haven’t done anything about yet.

Returning to a positive note however, I’m pleased to see that the Adelaide SQL group is doing really well at the moment. Maybe it’s our jet-setting leader, or the great speakers, but we are getting regular crowds of 30+ and I’m always learning something new each time I go.

The 'Atlas' Control Toolkit

Thursday, 13 April 2006

Brad Adams mentions the release of the”The ‘Atlas’ Control Toolkit” - I think we will use this.

How to subscribe to Tfs alerts for new workitems only

Thursday, 13 April 2006

Something to remember - BisSubscribe.exe is the command-line tool for maintaining alerts for Team Foundation Server

This tools is installed on the server, and by default ends up in the following path:

C:\Program Files\Microsoft Visual Studio 2005 Team Foundation Server\TF Setup

Supposedly, you can list all the events that can have alerts attached to them by using the web service on the TFS box - eg. http://localhost:8080/Services/v1.0/registration.asmx/GetRegistrationEntries but this fails with an exception for me.

"Atlas" April CTP Release

Wednesday, 12 April 2006

We’ve taken the decision to run Visual Studio 2005 Professional for now (until we figure out the costs of going to Team Edition)

I thought this also might be the ideal time to do a complete reload of my workstation. A chance to re-install everything from scratch and lose all the accumulated rubbish that ends up on your PC after a year or two of use.

This isn’t something I do often, as it take about a day and a half to reinstall the OS + all the various applications that I regularly use (approximately 50 on my list)

But one day on, and my list is already out of date! Time to uninstall the March CTP of Atlas and try out the April one. Roger’s already making use of Atlas in our new Online Assessment Tool that we’re working on, so this is relevant to us.