B4J Tutorial [ClassFromLayout] Why provide a Map interface for data in ClassFromLayout?

Relates to Project: ClassFromLayout

1751709999614.png

It may seem a little over the top to provide a map interface to set and get data from a class using a map, but there are several good reasons to consider it.

1 Map Vs Type list.
  • Data Storage and Compatibility: Storing data in Types allows for simple saving using RandomAccessFiles B4xObject. The same applies to Maps. However, problems can occur when you change the data fields in the Type during development or updates, requiring extra code in load methods to initialize new values, otherwise they will be null. Maps simplify this process. With the use of GetDefault when you access the Map to get the value and assign a reasonable default to the new field / variable.
  • Type Changes: Changing the type of a Type variable is more difficult as it is read directly from the file into the Type. Changing a variables type with a Map you can use GetDefault and cast / process it before assigning to the field. While B4x automatically casts compatible types, Maps offer flexibility for complex changes, such as converting single values to lists.
2 Check data changes.
  • Ease of Comparison: Comparing Maps is more straightforward than comparing Types, this is especially useful when checking if data has changed. If the Map contains only primitive values or Maps/Lists of primitive values, a single method can compare Maps regardless of the fields they contain. Types require a specific comparator for each Type.
3 Global Access.
  • Accessibility: Types have an advantage here, but defining constants for Map access in a separate Code Module ensures field names are accessible globally.
4 Other considerations
  • Integration with Nitrite NoSQL Database: I am quite partial to the Nitrite NoSQL Database, creating and updating records for that database can be done directly from a Map. In addition it is straightforward to save to and load data JSON objects into Maps.
  • Sorting Data: I often write Map To Type and Type to Map methods especially if the data needs to be sorted as List.SortType makes generalised sorting brilliantly simple. This process is used in the ClassFromLayout code.
  • Data Stability: For data that is unlikely to change, I will often use a Type to store it. This can be applied at a class level, so some classes data are stored in Maps, and some in Types.
I am not advocating that Maps are inherently better than Types or that you should always use Maps instead of Types. I just wanted to explain my reasoning for including the Map interface in ClassFromLayout, in case you were wondering, or are looking for options.

You may have a completely different process, or thoughts on the the above, let me know.
 
Last edited:
Top