Tuesday, July 21, 2015

Pragmatic MDD: Hibernate injection

Ok, now we have a domain model - perhaps not the most exciting one, but it has everything we usually need: inheritance, custom data types, an enum.

So let's continue and go a little bit deeper: How about adding JPA hibernate annotations to our classes? Of course, we don't have to use hibernate, but it really doesn't matter, which ORM we take, the principle is always the same.

How can we add these annotations? The same way we have customized our setup before: By declaring injection points in the primary transformations, overriding these points with our custom logic in secondary transformation and then load this secondary transformation on workflow execution.

Enough theory, how does the code really looks like?

1) The normal POJO m2t transformation - here we added some empty methods, like preClassDeclaration and preAssociationFieldDeclaration

2) Override of these empty methods in hibernate injector - here we add our annotations

3) Loading in the runner happens exactly the same way as before - we don't need to change anything

This solution is ok, but now we have several new problems.

First of all, we have a lot of code to set meaningful defaults for multiplicity, naming and aggregation. This is ok as long as we have only one transformation, but what if we have more? This code should be in its own module, since such logic is not really related to the POJO generation.

And this creates a second problem - if we have 2 different transformations, adding of the default values and POJO generation, how can we trigger them one after another? For sure not manually.

Let's see how to define workflows next time.

Wednesday, July 1, 2015

Pragmatic MDD: Can we scale?

So till now our model looked really very limited: just 2 classes, no enums, no custom types..

Let's change it.

If you take a look at the original model, there are several things we need to consider:

- AccountState enum in Account class

- RFID data type for tag attribute in BookItem class

- And let's just for fun add one more custom type: Book.ISBN will become a String with length 13

And this is how we will model it in JSON: nothing surprising, just an additional list of custom types.

So how shall we implement it?

First, we extend the UML transformations to see some nice pictures: 3 more lines of code to copy the enums and we are done.

Now let's proceed to the object transformations. Not a big change here too:

- m2m: resolving of custom types and enums

- m2t: additional template for enums

- runner: every enum needs to be written in its own file

And this is already all, now we have a fully working model!