使用TRIMM 为EA项目产生基于Axon的CQRS代码

TRIMM 1.0.1 Snapshot is available with support for CQRS and AxonFramework

EA是企业架构师Enterprise Architect的简称,如果你玩过Borland的Together,两者都差不多。(Enterprise Architect UML 9.3 官方安装中文版(含注册码))

在EA中可打开AddressBook model.eap,这个项目其实就是Axon AddressBook example的UML设计项目。

项目在EA中图示:

聚合根是Contact,围绕聚合根有一系列Command和Event,如下图:

通过EA的输出 XMI 可以将这个项目输出为AddressBook.xml.

通过Maven配置TrimmCQRSMavenPlugin 加入到这个java项目:


<plugin>
<groupId>dk.tigerteam</groupId>
<artifactId>TrimmCQRSMavenPlugin</artifactId>
<version>1.0.1-SNAPSHOT</version>
<executions>
<execution>
<id>generate</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<yamlFile>model/CQRSConfiguration.yml</yamlFile>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
</plugin>

配置 CQRSConfiguration.yml:

# The path to the XMI model file. e.g. model/MyModel.xml
xmiModelPath: model/AddressBook.xml
# The UML tool used. Example: EA, MagicDraw16, MagicDraw17 or FQCN/.groovy script (implementing the XmiReader interface)
umlTool: EA
# The optional base package name that should be appended to all generated code. Example: dk.tigerteam.myproject
basePackageName: dk.tigerteam.cqrs.axon

# Paths
# The required path where base-classes (classes generated each time) will generated to.
generateBaseClassesToPath: target/generated-sources/model
# The required path where extension-classes (classes generated only once and only if missing) will generated to.
generateExtensionClassesToPath: src/main/model-extensions
# The path where interfaces (similar to base-classes, generated every time) will generated to.
generateInterfacesToPath: target/generated-sources/model

加入Axon包依赖:

<dependency>
<groupId>org.axonframework</groupId>
<artifactId>axon-core</artifactId>
<version>2.0.5</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>

最后,运行:
mvn generate-sources

将你的EA用UML设计的CQRS项目转变为Axon框架支持的Java源码,产生的代码项目如下:

以上配置可以在 trimm-examples找到。

这虽然一个将设计文档自动转为代码的案例,但是对于指导我们如何编写开发设计文档还是有借鉴作用,如果你的设计文档能够落地转为代码,说明是实用的,当然如果遵循DDD去设计,当然更加优质。以MDD模型驱动方式编写文档是一种趋势。

像EA MagicUML或Borland together这样的UML设计工具,也可以进行传统的数据建模,但是对于复杂的中大型项目,DDD等为主的模型驱动方式还是能让系统和文档更加贴近和敏捷。
[该贴被banq于2013-11-04 19:27修改过]