Generic repository for Java (Part 1)

Persisting data is part of most apps and there are several ways of doing persistence. In this blog post I’m sharing some of my techniques and personal oppinions. Maybe it doesn’t fit for everybody but it works for me and maybe this is helpful for some people.

When it comes to persistence my favorite is EventSourcing: Instead of persisting the current state of your data, only events of changes to your data are persisted. The current state can then be (re-)calculated from the events. In my oppinion this is the most natural and elegant way of handling persistence but, to be honest, it’s not the easiest to implement, especially in Java.

But this is another story. Today I want to further investigate persistence for use cases where EventSourcing is not an option and/or classical state-based persistence is needed. In such cases I’m typically using the repository pattern. The key of this design pattern is to define a common interface for persistence functionality that is independent from the underlaying persistence technique. For each specific persistence technique (for example a database, the filesystem or a cloud storage) an implementation of the interface can be provided. This way you can exchange the persistence technique without touching the rest of your application. Additionally this improves the testability because you can provide a mock implementation of the repository interface for unit tests.


Released mvvmFX 1.1.0

Yesterday we released the new version 1.1.0 of mvvmFX. This time we have lots of new features that can make the development of JavaFX applications easier.

Some of the new features:


The most interesting new feature are the Commands. With them you can now encapsulate actions in the ViewModel and provide them to the View. Each Command has boolean properties showing if it is running at the moment and if it isExecutable. The latter can for example be used in the View to bind the visibility of a button. With CompositeCommands you can compose many commands so that all sub-commands are executed when the composite command is triggered. See the wiki page for a detailed explanation.


The second big feature is the ModelWrapper. This class can simplify the mapping between the Model and the ViewModel for CRUD like applications. Instead of duplicating the fields of a model class in the ViewModel you can now use the ModelWrapper that support reloading (copy values from Model to ViewModel), resetting (set ViewModel to default values) and committing (copy values from ViewModel to Model). In the wiki you can see a detailed example.

Using mvvmFX views as items of ListView

You can now use the CachedViewModelCellFactory as CellFactory for the javaFX ListView component. This way you can use mvvmFX Views as items of the ListView. See a more detailed description in the wiki:

Inject ResourceBundle in the ViewModel

Until now it was only possible to inject the ResourceBundle in the View. But for some use cases it would be useful to have the ResourceBundle available in the ViewModel too. With the new version this is possible via the annotation @InjectResourceBundle. In addition we have added a live cycle method for the ViewModel like it was possible in the View before. We use the same naming conventions for this: If the ViewModel has a method with the signature public void initialize() it will be called by the framework after the injection of the ResourceBundle is done.

public void MyViewModel implements ViewModel {
    ResourceBundle resourceBundle;

    private StringProperty title = new SimpleStringProperty();

    public void initialize() {

Utility modules

Additionally we have introduced two more Maven modules:

mvvmfx-utils contains utilities that are not directly related to MVVM but that are more generally useful for JavaFX development. We moved the SizeBindingsBuilder and the ListenerManager from the core module to this new module to improve the cohesion of the core module.

mvvmfx-testing-utils contains utils that we used for testing purposes internally for some time. Now these utils are available for the public. The most interesting is the GCVerifier that we are using for testing situations where Garbage Collection is important.

See the whole list of changes at github:


Exception in ChangeListener

Today I had an interesting issue with JavaFX ChangeListeners and Exceptions. I was writing a unit test to reproduce a simple bug in mvvmFX. The issue was a NullPointerException that was thrown under some specific conditions. No big deal. I had written the test case that reproduces the exception in no time but something was still wrong. The exception was visible in the console window of the IDE but the test was still green.

green test and still an exception


JavaFX: Data Model for TreeTable

When you use a ListView or a TableView in JavaFX you can easily separate the definition of the List/Table and the data that is displayed by the component.

For example when you use a TableView you could create an ObservableList containing your data objects and set CellValueFactories to define what value will be displayed by a specific column. Look at the code from the Oracle docs for a simple example.

The key point is: When you add/remove an instance of your data in the observable list (your data model) the table will update itself accordingly. You don’t need to manually add a new row to the table or anything like that. This is a realy cool way to separate the visualization of the table from the data to be displayed.


mvvmFX 1.0.0

Some days ago we have released the first stable release of our mvvmFX library that helps you to implement the Model-View-ViewModel pattern with JavaFX.

The new release can be used with Maven/Gradle:

compile 'de.saxsys:mvvmfx:1.0.0'

Additionally Alex and me have written an article for This is a german version. An english version of the article will follow next week.


JUG-GR: Model-View-* - UI design patterns in detail

On december 3, 2014 I gave another talk at the Java-User-Group Görlitz with the title “Model-View-* : UI-Design-Patterns im Detail”.

The talk was all about the Model-View-Controller pattern and it’s variants like Model-View-Presenter (in different flavours) and Model-View-ViewModel. The example code was written in JavaFX. You can find the Code here on github. Of course I also presented the usage of our own MVVM framework mvvmFX.

Additionally I spoke about the scope of UI patterns: On the one hand side you can see “MVC” at the abstraction level of a single UI control. In the talk I used the example of a TextField control.

On the other hand you will often here people talking about “MVC” when they talk about the overall application architecture. I compared the terms of MVC with the well-known Three-tier Architecture and the less common CQRS architecture.


EasyDI Example

This tutorial will show you the usage of Easy-DI. All files for this example can be found in the github-project in the package /src/test/java/eu/lestard/easydi/examples/coffee

Let’s assume this class diagram for a coffee machine application:


In this class diagram we can see the dependencies of each class: The CoffeeMachine has a reference to the WaterTank and the CoffeePowderProvider. The CoffeePowderProvider has a reference to the BeanContainer and to the Mill.


Dependency Injection in less then 50 lines of code

I’m a big fan of the Dependency Injection design pattern and I’m using it a lot. I’m saying “design pattern” because when I’m thinking and talking about “DI” I don’t think about frameworks, big containers and a lot of magic. Instead, for me DI is a simple concept of how you write your classes and put them together.

In the past I’ve used many DI frameworks like Guice, CDI/Weld, EJB and Spring. In other use cases (for example my SnakeFX game) I’ve done the DI by hand:

Simply put all dependencies of a class into the constructor of this class. In the main method you can now start to instantiate the classes you need starting by the ones that have no dependencies and can therefore be instantiated without constructor parameters needed. It’s easy and this way you can’t create cyclic dependencies by accident. Look at an old version of the DependencyInjector class of SnakeFX to see what I mean.

Learning how to do the DI by hand was a real eye-opener for me. You can learn that DI is no magic at all and you will lose all your fear from the big buzzword “Dependency Injection”. It’s nothing special. Simply a design pattern.


New release of Advanced-Bindings

Last week I’ve released a new minor version of my Advanced-Bindings library. In this post I will describe the new features in more detail.

Logic Bindings

In the standard JavaFX Bindings API there are methods to create bindings for the logical OR and AND operations. The drawback of these methods is that they can only be used to combine exactly two observable boolean values.

In the new LogicBindings class there are now methods to create OR and AND bindings with a variable number of arguments.

ObservableBooleanValue a = ...
ObservableBooleanValue b = ...
ObservableBooleanValue c = ...
ObservableBooleanValue d = ...

BooleanBinding and = LogicBindings.and(a,b,c,d);

BooleanBinding or = LogicBindings.or(a,b,c,d);


Adjustable UI Brightness with JavaFX

Maybe some of you know or have already worked with Adobe products like Premier Pro or After Effects.

From the perspective of a UI developer there are many cool things about these products. For example you can seamlessly fade the brightness of the UI and it looks great in every brightness level (which seems to be a hard task).

This is cool because there are good reasons for users to prefer either a dark or light interface. For example some creative artists like to work in a dimmed office so that no light from outside is overlaying the colors picture/video they are working on. In this situation a dark user interface is better then a light one because it’s more comfortable for the eyes.

On the other hand I know people how prefer a light and friendly user interface because a dark one would give them a depressive fealing.

So it can be a big plus for the usability of your application when you allow the user to adjust the brightness of your app.

Adjustable Brightness in JavaFX

Something like this can be done with JavaFX too. With JavaFX 8 there was a new default stylesheet created known as modena.

In this stylesheet there is a color definition named -fx-base that is, as the name suggests, the base for all other color definitions in the stylesheet.

When you adjust the value of -fx-base all other colors will be adjusted automatically in relation to -fx-base.


The first thing to do is to create an IntegerProperty that will hold the brightness level in a range from 0 to 100%.

IntegerProperty brightness = new SimpleIntegerProperty();

After that we create a StringExpression (which is a base class of StringBinding) that contains the style definition of -fx-base with the value of our brightness property.

DoubleBinding colorValue = brightness.multiply(2.55);
StringExpression styleString = 
    Bindings.format("-fx-base:rgb(%1$.0f , %1$.0f, %1$.0f)", colorValue);

For this task we can use the Bindings.format method that takes a format string and an observable value. In the example I’m using the rgb css method to define the color. This method takes the color information for every channel in a range from 0 to 255. For that reason I’m creating a DoubleBinding that contains the multiplication of our brightness property (from 0 to 100) with 2.55 so that the value range is correct.

The last step is to bind the style property of the root container of your application to the StringExpression with our style definition.

Parent root = ...


This way the styling of the application is adjusted everytime the brightness property changes.

I’ve created a small example app that shows the usage of this binding. In the app I have bound the brightness property to a slider so you can change the brightness level without interruption. To demonstrate the effect I created a dummy form with some JavaFX controls.







You can find the example code on github: Feel free to use it in your own applications!