jMolecules – Architectural Abstractions for Java
Have you ever wished for a way to implement complex domain models using plain and straightforward Java? The jMolecules project offers a set of libraries designed to simplify this process for developers. Let's explore how it transforms domain modeling with ease and clarity.
Ideas Behind jMolecules
jMolecules was conceived to make architectural concepts explicit, rendering code easier to read and write. It ensures that domain-specific code is devoid of unnecessary technical dependencies and minimizes the amount of boilerplate code developers need to draft. Furthermore, it serves the dual purpose of automatically generating valuable documentation and validating both implementation structures and architecture.
Goals
The fundamental goal of jMolecules is to simplify developers' lives significantly. It provides a clear method to express if a piece of code, whether it's a package, class, or method, implements a specific architectural concept. It's designed so that architects and developers alike can quickly determine the architectural role of given code. To help with this, jMolecules supports integration with various tools, enhancing code via augmentation and ensuring compliance with architectural rules.
Use Case: Expressing Domain-Driven Design Concepts
Annotation-Based Model
Consider a banking application. Rather than verbose names like BankAccountEntity
or CurrencyVO
, jMolecules encourages the use of concise, domain-specific names such as BankAccount
and Currency
.
Using jMolecules annotations like @Entity
, @Identity
, @ValueObject
, and @Repository
, developers can cleanly express concepts from Domain-Driven Design (DDD) without clutter:
import org.jmolecules.ddd.annotation.*;
@Entity
class BankAccount {
@Identity
final IBAN iban;
/* ... */
}
@ValueObject
class IBAN { /* ... */ }
@ValueObject
record Currency { /* ... */ }
@Repository
class Accounts { /* ... */ }
Type-Based Model
For those who prefer, jMolecules offers a type-based model built on standard interfaces. These make relationships between building blocks explicit within the type system, allowing the Java compiler to verify model correctness:
Identifier
for identifier typesEntity
,AggregateRoot
, andAssociation
to define entities and relationships- Enforced identifier types for each aggregate
Use Case: Expressing Architectural Concepts
jMolecules provides annotations to articulate higher-level architectural concepts like Layered, Onion, and Hexagonal Architectures:
- Annotate packages to represent architectural layers, rings, and adapters.
- Annotate individual classes for direct integration.
Annotations cover CQRS architecture (@Command
, @QueryModel
), Layered architecture (@DomainLayer
, @ApplicationLayer
), Onion architecture (@DomainRing
), and Hexagonal architecture (@Application
, @Adapter
).
Generating Boilerplate Code
jMolecules annotations and interfaces bridge the gap between your Java code and various technologies, automatically generating necessary technical code. It incorporates seamlessly with tools like Spring, Data JPA, and more.
Verification and Documentation
jMolecules aids in verifying architectural rules and generating comprehensive documentation. It integrates with tools like jQAssistant and ArchUnit to uphold architectural integrity and create visual representations of code architecture.
Installation
Implementing jMolecules in your project is straightforward. Dependencies can be easily declared in build tools like Maven and Gradle:
Maven
<dependency>
<groupId>org.jmolecules</groupId>
<artifactId>jmolecules-ddd</artifactId>
<version>1.9.0</version>
</dependency>
Gradle
repositories {
mavenCentral()
}
dependencies {
implementation("org.jmolecules:jmolecules-ddd:1.9.0")
}
In essence, jMolecules is a game-changer for developers seeking organized and clearly defined architecture within Java applications.