Archive for January, 2005

Business Entity Metadata With Attributes

Today I created a little validation library that utilizes Attributes to specify simple business rules on an entity. It didn’t take that long to code, and seems to work pretty well:

...
[RequiredValidator( "First name is required." )]
[RangeValidator( 0, 50,
   "First name cannot be longer than 50 characters."  )]
public string FirstName
{
	get{ return _firstName; }
	set{ _firstName = value; }
}

[RequiredValidator( "Birth date is required" )]
[RangeValidator( "1900/1/1", "2010/1/1",
   "Birth date must be after 1900/1/1" )]
public string BirthDate
{
	get{ return _birthDate; }
	set{ _birthDate = value; }
}
...

The attribute based approach seems like it would work quite well for specifying simple business rules. The main advantages as I see them are:

  • Simple, easy to read declarative approach. It’s a lot easier to see at a glance what the rules are for each property.
  • It should be possible to use these rules to generate UI validation controls.
  • Keeps business rules where they belong; with the entity they pertain to.

I still haven’t given much thought to the disadvantages of this approach, however there are at least a couple:

  • Attributes somewhat limit what you can accomplish. For example, when validating a birth date, you’d ideally set sane lower bound (1900/1/1 as I do in the above example). However, since you must specify constant values in an attribute, you can’t set the upper bound to DateTime.Now, which would be ideal. Instead, you have to hard-code a specific value.
  • They clutter up your business classes. With one or two attributes on each property, this isn’t a big problem. But what happens when you start using attributes to map to UI components, or database tables? You could get something ugly like this:
[DatabaseColumn( "FirstName" )]
[UserInterface( typeof(TextBox), "txtFirstName" )]
[RequiredValidator( "First name is required." )]
[RangeValidator( 0, 50,
   "First name cannot be longer than 50 characters."  )]
public string FirstName
{
	get{ return _firstName; }
	set{ _firstName = value; }
}

The other issue to wrestle with is whether to assign these attributes to properties, as I do above, or to the underlying fields which they are hiding. Ideally your underlying fields will be marked private, so if you do map to fields you could run into issues with Trust, depending on the environment to which you are deploying. Either way, it’s definitely something to think about.

I’ll post the code for the validator library as soon as I find the time to fire up my laptop.

Advertisements

JeffPerrin.com Version 69

Now with 30% more foliage!

Seriously though, I think I’m finally getting to the point where I can keep my hands off the design for a while. I’m almost satisfied. I swear it.

The crappy thing about designing my own website, is that I have all these cool ideas in my head, but I have absolutely no idea how to implement them because I can’t use Photoshop-like tools. I see sweet designs like at HicksDesign and I think… Man, it’d be cool if I could just whip up a little bullet graphic. But I can’t, so I end up using a stupid little Webding box instead. Oh well.

My RSS Feed URL Has Changed

Just in case there’s anyone out there who cares, when I last updated my site I created a new RSS feed that combines the feeds from my personal and work blogs. It can be accessed at:

http://www.jeffperrin.com/Feed.aspx

More Thoughts on Desktop Search

I just read yet another article on desktop search by the inimitable Dare Obasanjo, which helped to further crystalize my thoughts on why I don’t use any of the current offerings.

The main problem is that the entire idea is only half finished. Sure, the search engine side of things has been mastered by everyone and their dog, but there’s another half of the equation that hasn’t even been touched yet:

How do we actually add metadata to our files?

In some areas, the addition of metadata is pretty good. For example, when I rip a cd into mp3s with iTunes all the relevant song information is automatically downloaded from the internet and inserted without my having to do a thing. But what about images? Files received via e-mail? There are probably lots of other examples of files that could contain lots of additional relevant information, but don’t.

The other issue lies in hiding the whole idea of searching behind a UI that users may actually use. I don’t want to have to open an application window to access my commonly used searches. I’d like to have automatic folders, similar to the way the Recent Files folder/menu item works. It would be cool if they could be automatically created for me as well. I definitely don’t have all the answers, but I’m pretty sure the entire experience could be a lot better. Who knows? Maybe this is where Microsoft is headed with WinFS.