Business rules are not just those rules that violate business constraints; they are also the rules that violate common sense that the business may not think about. For instance, you may not allow any employees to be younger than 18. But what someone will often forget is that you cannot have a person with a birth date later in time than the current date. Things like this often get overlooked in applications. It is always best to get the business rules from the business community and then examine them yourself. So let's examine the rules for the Employee object.
Note | This might seem like a business rule that is so obvious that it could not possibly be missed. But if you read the sidebar "The Importance of Business Rules" you will see that there were some fairly common sense business rules that seem to have gotten completely misplaced. If these types of rules are so common sense, then they should never be missed. |
As mentioned earlier, database constraints must also be considered rules or else an object can never be persisted if it violates those rules. Generally, you can break database column constraints (excluding referential integrity) into three categories: maximum length of a field, null fields, and allowable field values. Table 9-1 takes each piece of data and breaks out the rules that you must apply to that piece of data.
Column | Maximum Length | Nullable | Allowable Values |
---|---|---|---|
EmployeeID | No | ||
LastName | 20 | No | |
FirstName | 10 | No | |
Title | 30 | No [*] | |
TitleOfCourtesy | 25 | Yes | (Mr., Mrs., Ms., Dr.) |
BirthDate | No[*] | ||
HireDate | No[*] | ||
Address | 60 | No[*] |
|
City | 15 | No[*] | |
Region | 15 | Yes | |
PostalCode | 10 | No[*] | |
Country | 15 | No[*] | |
HomePhone | 24 | No[*] | |
Extension | 4 | Yes | |
Photo | Yes | ||
Notes | Yes | ||
ReportsTo | Yes | ||
PhotoPath | 255 | Yes | |
[*]The database as set up allows nulls in these columns, but you will disallow them in your business rules. |
There are a couple of things to note about Table 9-1 as you look through it. The first is that there are no check constraints on the table that ensure values fall into a certain range. I have also added some allowable values for the TitleOfCourtesy field to demonstrate that type of business rule. So now that you have the database constraints figured out, let's move on to some real rules.
For simplicity, this section lists the business rules. This is probably not as comprehensive a list as you might find in a real application, but it will serve your purposes for this chapter:
BirthDate
Cannot be older than 60 years old
Cannot be younger than 18 years old
Cannot be a date in the future
HireDate
Cannot be more than two weeks in the future
Cannot be before 1976 (because that is when your company started)
PhotoPath
Check for file
For your purposes, there are no additional business rules dealing with this object. Another rule, which you might be able to apply here (but because of the complexity you will not), is the phone number format based on the employee's region. If the employee is in Europe, the length and format would be different than if the employee resides in the United States or Canada. Rules like these are often overlooked, and for expediency, you will overlook them here as well.