The Micro Framework ApproachThe demand for Grails and Groovy is clearly raising these days - at least here in Austria and Germany. Although most of my workshops have their individual adaptions (depending on the previous knowledge and programming language experience of participants) there are parts which can more or less be found unmodified in every workshop: Groovy essentials/advanced topics and what I call micro framework examples. This article is about the idea behind micro framework examples and why I find them that useful as workshop examples.
What is a Micro Framework Example?I strongly believe that true understanding on patterns behind frameworks like Hibernate and Spring can't easily be treat in a bunch of slides. Explaining patterns is one thing but to actually see how those are applied is another one. One approach I've found to be really useful in workshops is the use of micro framework examples. A micro framework example implements the core functionality behind a specific framework - reduced to the very fundamentals. One advantage to implement a micro framework example together with participants is to force triggering a thinking process of what functionality is needed and how it can be implemented. Another side-effect is that it allows to slightly introduce the original frameworks ubiquitous language simply by using the same class and method names. Let me give you an example. The most threatening topic for many of my clients is to understand Hibernate and its persistence patterns. One approach to create a better understanding would be to implement a micro Hibernate framework example. This can be done in a simple Groovy script MicroHibernate.groovy which defines two classes and a simple test case. The first class implements the registry pattern and is called
SessionFactoryacts as the main access point to get a reference to some storage connection. In the micro framework example this will simply be a
Map. Dealing with SQL or even a real database would uselessly complicate the example and we want to concentrate on the core essentials. Let's go on to the next class which implements the persistence context pattern: A
Sessionobject can be used to retrieve already persistent objects and to persist so-called transient objects. I like to start by implementing the
loadmethod which loads a persistent object from the storage connection of the current session factory. Of course, this is not an example for Groovy beginners but with a little knowledge of MOP and with some programming guidance it should not be a big thing to understand what is going on. At the end let's define some test case which shows how both classes are actually used: Interestingly, even without considering SQL, DB connection handling, threading issues etc. participants already get a feeling of several Hibernate gotchas beginners otherwise often struggle with:
- the first level cache
- the need for proxies or MOP modifications
- Hibernate's use of object snapshots
- the IdentityMap pattern
- repeatable read transaction isolation level
Sessionfunctionality gets than extended by
deletefunctionality. If programmers have been through the process of implementing such a micro Hibernate example they often get a basic and fundamental understanding of how an orm framework could work and what the main challenging problems are. By keeping the class and method names in sync with the concrete Hibernate implementations participants learn the framework's basic domain language.