DDD Sample
This page describes a port of the DDD Sample using Sculptor. It is a domain model based on the cargo example used in Eric Evans’ book.
Sculptor will help you with the static structure of the application. It can generate the data part of the domain objects and boilerplate code for services and repositories. It has a nice runtime framework for especially the persistence implementations, such as standard queries. However, to qualify as a true DDD application the domain objects must have complex behavior - business logic. With this sample we illustrate how to integrate manually written logic with the automatically generated parts.
Table of Contents:
Model
The definition of the domain model, using Sculptor’s textual DSL is separated in one file for each Module, plus one “empty” top file, which imports the other files.
The generated vizualization with GraphViz looks like this:
JUnit Tests
This sample has an extensive test suite, which illustrates how to write junit tests at different levels.
- logic in the domain objects is tested with ordinary junit tests, without any need for database emulation or spring container
- repositories and details of the persistence are tested with DBUnit tests and spring container and hibernate using in memory database
- services are tested in a similar way as the repositories, but also using easymock framework
Metrics
It is interesting to compare the fully hand written original DDD Sample with this partly generated port. The number of hand written lines of code in the Sculptor port is 783. Compared to 1179 in the original. The business logic is almost identical, but the Sculptor variant has less lines of code since much of the boring boilerplate code is generated. It has bean measured with JavaNCSS.
JUnit tests are not included, since they are very similar. Web application is not included, since it is not implemented in this Sculptor port.
Try It
If you interested in the source code you can find it as part of Sculptor’s examples:
- Install Sculptor and its requisites as described in the Installation Guide.
-
Get the source code from GitHub:
$ git clone git://github.com/sculptor/sculptor.git $ cd sculptor/sculptor-examples/DDDSample
- Build with
mvn clean install
. This will also run all JUnit tests located insrc/test/java
. Take a look at some of them and run them from Eclipse also. - Study the
.btdesign
files located insrc/main/resources
. - Study the hand written code in
src/main/java
and the generated code insrc/generated/java
.
Learn more about the capabilities of Sculptor by reading the Hello World and Advanced Tutorial
Source Code
The complete source code for this sample is available in GitHub https://github.com/sculptor/sculptor/tree/master/sculptor-examples/DDDSample.