Enumerations within most normal languages are annoying to maintain and build logic off of. In most cases I have seen String variables or manually mapped integers being used. My experiences within the system analysis, database and business intelligence combination always led me to using database table entries with business logic attributes, although convincing the developers of their benefit was not always an easy battle. But, more on that transition in another post. So in comes Mendix with a more formal, easier-to-combine into business enumeration. But as we all use them on a daily basis (or at least should be) I won’t go into diatribes about their benefits, proper usages and so on. Instead this article will revolve around a Java function entity combination we use to get around the two issues we have had with enumerations.
The first common issue we faced was associating attributes with an enumeration. Creating an entity with the enumeration as a unique attribute solves the issue. However, the problem comes in maintaining this entity when creating new enumeration values particularly across multiple environments.
The second issue we are commonly faced with is mapping web service string results into enumerations (A nightmare for anyone who has ever tried). Creating a microflow to map these manually is a terrible solution but unfortunately the one we tend towards. Not only does it create clunky technical microflows but it also creates maintenance issues.
So what do we do make our lives simpler? Well fortunately the team from Mendix have provided the reflection module a perfect spot to get inspiration from. So using this starting point we construct a Java function to be called during the start-up microflows which using code inspired liberally from the reflection module runs through an enumeration set and populates an entity and voila we have a self-building entity to run our business logic and web service mappings off of.
Now words are great but let’s not recreate work for everyone and get to an example. So here we have a module (I will work on adding it to the store soon) EnumEntityBuilder with a simple entity FruitEnumEntity add it to a play project and link the FruitEnumEntity-Overview form from the navigation. I have provided 3 buttons to play with “Build”, “Clear” and “Find a fruit”. Build runs the Java function which populates the enumeration entity, the grid on the page will provide them. The second function clears the entities so knock yourself out adding new and removing new fruits. “Find a fruit” pops up a text search dialog to emulate a web service mapping to find that elusive enumeration.
Well I hope this can help you in future projects to save time and build more robust logic. And once more here is that example module: EnumEntityBuilder.mpk.