Friday, May 21, 2010

Generating for multiple layers

You often need the same structures in different places. You have a data-layer where you need data classes to hold the data, you need a network layer for transportation and you also might have a GUI to display your data. Depending on what you do, you may need to decorate the data with additional information or change its representation. One problem with this is that a new feature must be implemented on all layers to fully support it. With actifsource you can make sure that a feature is handled on all layers and no task gets forgotten. Simply create templates for each layer. Today I want to give you a small example how actifsource can help.

Lets look at the data layer. I generate really simple data classes, they have a getter, setter and field for the value/reference. For this example I don't care about change notification or null checking:



As an example I defined a data class Person in actifsource:



The template that generates the java class using some helper functions "toJavaType" to translate the actifsource types to java:



For the second layer I generate a simple viewer for each data structure. There is no need to define additional information in the model, I just use the same data. For simplicity I don't care about the type of the properties and create a text field for each one. In a real world example you might create components instead of frames and create specific controls for each attribute/relation type.

The following two screenshots show the viewer template and the output for person:





In the viewer template I also added a main-method and the createExampleValue-method with a protected region to create an example value to display. The running example looks like this:



Now I have two layers that will updated every time when I change the model. It is ensured that there is always both a text field in the view and a data field in the data class. Think about a third layer: a network layer with the serialization code is stored or different output format implementations. You even might have implementations or clients/servers in different programming languages (c++, c#, java). In all cases actifsource takes care about consistency, no more trouble due to missing fields or different order in the serialization/deserialization code between the different implementations.

No comments:

Post a Comment