New Java Features Enable Domain-Driven Design

Java has (fairly) recently seen the addition of many new features, whose power is sometimes overlooked. If you have avoided Domain-Driven Design because it involved specific and sometimes archaic editors and clumsy code generation. Now might be the time to take a second look. Annotations, JPA, dependency injection are here to make things a lot easier and with less overhead.

If you’re an experienced software developer who follows and experiments with the latest Java features, you might want to skip this post. Basically, a few pieces of a larger puzzle have come together that, I believe, have given Java a better future compared to some of the new languages/frameworks. But you already knew that.

Java, revitalized

If you look at some “new” Java code, annotations are probably the most striking change in comparison to older Java code. Yet for their prevalence, they are easily written off as just a way to add some meta-data. If you don’t look much further, you’ll easily overlook their true power, which is the insertion of crosscutting concerns in your programs. Typical crosscutting concerns you’ll encounter in an enterprise app are logging, security and transactions.

In the past, if you wanted to factor out a crosscutting concern, there were aspects and there was some nasty juggling with static helper classes. Spring’s dependency injection improved that a bit.

Now most of this is built into Java Enterprise Edition itself. It’s all been streamlined, improved and made ready for mass consumption.

Domain Driven Design

Again, in the past if you wanted to do domain driven design and keep it manageable, there weren’t many options. MDA tools are one, but I’ve never really found one that clicked with me. You could do it by hand, but those crosscutting concerns I previously mentioned would interfere. There were things like xDoclet, which helped a lot.

But right now, you can just start from your pojo and build from there. You want persistence? Add JPA annotations. You want some domain logic, you can just start programming (no need to first build a UML model, than generate, than code). Security? There are annotations for that. Need dependencies, like helper classes, injected? Spring does it elegantly without interfering in your code, either via XML or annotations.

If you’re getting started, it’s a lot, but a JEE6 technology a day keeps the frustrated developer away.

And now we just need to get rid of those getters, setters and other boilerplate (tip).

The New Style

Basically what it comes down to is this: In the past there were layers, layers and than some layers. In the minimal configuration, you had DAO classes to load your data, service/manager classes to process the data, controllers/actions to prepare and process the view and finally JSPs or whatever view technology your using.

If you program something in Vaadin or many other new component-based frameworks you have your domain objects and your view. The great thing with Vaadin (and others) is that almost everything is written in the same language. Domain objects are even reusable as form data sources. No need for JSP tags, EL expressions, SQL queries (sadly there still is JPAQL), even the XML configuration has been reduced to minimal proportions.

It’s liberating to be able to focus on just one thing and do it well.

(image credit)

  • Jon

    nice post – it's refreshing to read something so positive.


  • Thanks for the kind reply, John.
    It's probably clear from the post, but I think it's a great moment to be a Java developer. I hardly ever wrote Java code in my spare time the last few years, but lately that has changed. I liked Java for its simplicity (compared to C++) and thankfully, that simplicity is slowly returning while giving us developers much greater power.

  • Kevin

    I am going to agree with you. I am blown away at the ease of JEE6. I am more blown away how many companies are still stuck on JEE 2/3/4 and are missing out on a huge improvement in technology and more so development time. It's quite easy to build a full scale front to back web application. Another technology I am quite impressed with is JSF. I've shied away from it for a few years, but the recent 1.2 and more so 2.0 with AJAX aware components that allow you to drag/drop components that then do incremental page updates without refreshing the page all while still being web crawler friendly is quite impressive. Couple that with theming and a large library of components, and you have a very powerful visual display tool coupled to a very powerful scalable server side technology. Most types of sites would be very easy to build these days compared to just a couple years ago. Lastly, give a shout out to RESTful apis. Java Jersey, Reslet, etc are making building RESTful APIs extremely easy. It's a really good time to be in the java industry. Not only are we the most sought after developers, but if you're lucky enough to get to work with at least JEE5, if not 6, you're going to be loving life.

  • Kevin, I completely agree on the REST APIs, I've worked a lot with JAX-RS and yes, that takes care of yet another layer.
    On the JSF front, I too haven't really taken the time to properly learn its ways. In the past I've heard a number of negative comments that tainted my vision. Most importantly, many people found it too complicated. I should give it a proper chance one of these days, it's just that I'm really liking Vaadin right now.

  • Pingback: Google App Engine for Java Loading Requests()

  • Bhanuprakash

    can u pls suggest me the nice book or site for jee6 to [email protected].I want to learn new recent technologies and i need some guidance.

  • Bhanuprakash

    can u pls suggest me the nice book or site for jee6 to [email protected].I want to learn new recent technologies and i need some guidance.