Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting rid of hard coded values when dealing with lookup tables and related business logic

Example case:

We're building a renting service, using SQL Server. Information about items that can be rented is stored in a table. Each item has a state that can be either "Available", "Rented" or "Broken". The different states reside in a lookup table.

ItemState table:

id name
1 'Available'
2 'Rented'
3 'Broken'

Adding to this we have a business rule which states that whenever an item is returned, it's state is changed from "Rented" to "Available".
This could be done with a an update statement like "update Items set state=1 where id=@itemid". In application code we might have an enum that maps to the ItemState id:s. However, these contain hard coded values that could lead to maintenance issues later on. Say if a developer were to change the set of states but forgot to fix the related business logic layer...

What good methods or alternate designs are there for dealing with this type of design issues?
Links to related articles are also appreciated in addition to direct answers.

like image 831
Mika Avatar asked Oct 30 '09 14:10

Mika


1 Answers

In my experience this is a case where you actually have to hardcode, preferably by using an Enum which integer values match the id's of your lookup tables. I can't see nothing wrong with saying that "1" is always "Available" and so forth.

like image 100
Otávio Décio Avatar answered Sep 23 '22 20:09

Otávio Décio