Our system architects have recently become enamored with the concept of Enumerated Types as outlined in Effective
Java. I like the idea but we differ regarding how to use them. First, let me say that I am a developer working in our system data access layer. My issue is who should be responsible for converting the Integers and Strings retrieved from the database or, turning it around, converting the Strings retrieved from a request object into the corresponding enumerated type.
I hope you will review the following code and offer any comments. Thanks.
A system architect coded the following method on an Enumerated Type class named SourceSystem:
where idToType is a HashMap containing all the SourceSystem enumerated types. Additionally, the system architect coded the following setter method on the corresponding data transfer object:
I have several problems with this approach:
1. My data access object now has to "know" how to instantiate the enumerated type and invoke the appropriate method to perform the conversion. I think it should be the data transfer object's responsibility to "know" how perform any conversions required. Additionally, if defined more generically, the same setter method can be used to perform not only the conversion of an Integer value from a database table but also the conversion for a
String parameter from the request object on the front-end. The amount of coding required by developers is minimized.
2. By not having to know about the enumerated type SourceSystem, that is one less import statement and one less dependency for my data access object. I fail to see the need to introduce a dependency where one is not required.
3. Because the system architect decided to handle invalid values by throwing an IllegalArgumentException rather than, say, returning a default value, I will have to code defensively (as will the developer of the corresponding
JSP page) in order to handle a potential run-time exception. Again, I think it is the data transfer object's responsibility to decide how it wants to handle that possibility.
I proposed the following implementation for the setter method:
along with this corresponding new getter method on the enumerated type:
These two methods would allow me code the following line in the data access object to retrieve the source system from the database table:
instead of:
Personally, I think my solution is cleaner, is more flexible, reduces dependencies, isolates potential run-time exceptions, minimizes the code developers have to write, and results in more readable code. However, the system architect is not buying it.
Does my solution for using this enumerated type not seem preferable ?