jMolecules Documentation

repository·main·Indexed 23 days ago

https://github.com/xmolecules/jmolecules

A suite of Java libraries providing architectural abstractions to explicitly express Domain-Driven Design (DDD) and architectural patterns such as Layered, Onion, Hexagonal, and CQRS. It offers annotation-based and type-based models to define Value Objects, Entities, and Aggregate Roots, while keeping domain code free from technical dependencies. Includes specialized modules like jmolecules-ddd, jmolecules-architecture, and jmolecules-integrations for Spring and JPA.

Tokens
1.3K
Snippets
3
Records
6
Agent score
32%

What's inside jMolecules

  1. Overview of jMolecules

    main

    jMolecules is a set of libraries designed to help developers implement domain models in plain Java by explicitly expressing architectural concepts. It aims to keep domain-specific code free from technical dependencies, reduce boilerplate, and enable automatic documentation and architectural validation.

    Key capabilities include:

    • Expressing DDD concepts: Using annotations or a type-based model to define Value Objects, Entities, and Aggregate Roots.
    • Expressing architectural styles: Marking code as part of Layered, Onion, or Hexagonal architectures.
    • Tool integration: Enabling automated augmentation (e.g., Spring/JPA integrations) and architectural rule checking (e.g., ArchUnit, jQAssistant).
  2. Express DDD concepts using the Type-Based Model

    main

    The type-based model uses interfaces to express relationships between building blocks directly within the Java type system. This allows the compiler to help verify model correctness and makes the information easily accessible via reflection.

    Core interfaces include:

    • Identifier: Represents types acting as identifiers.
    • Identifiable<ID>: Anything exposing an identifier.
    • Entity<T extends AggregateRoot<T, ?>, ID>: An entity declaring its parent AggregateRoot and its identifier.
    • AggregateRoot<T extends AggregateRoot<T, ID>, ID extends Identifier>: An aggregate root that is also an Entity belonging to itself.
    • Association<T extends AggregateRoot<T, ID>, ID extends Identifier>: An explicit association to a target AggregateRoot.
  3. Express DDD concepts using the Annotation-Based Model

    main

    The annotation-based model allows you to use standard annotations to mark building blocks from Domain-Driven Design (DDD). This approach allows you to use clean domain names (e.g., BankAccount instead of BankAccountEntity) while still expressing their architectural role.

    Common annotations provided by jmolecules-ddd include:

    • @Entity
    • @Identity
    • @ValueObject
    • @Repository
    import org.jmolecules.ddd.annotation.*;
    
    @Entity
    class BankAccount {
    
    @Identity
        final IBAN iban;
    
    /* ... */
    
    }
    
    @ValueObject
    class IBAN { /* ... */ }
    
    @ValueObject
    record Currency { /* ... */ }
    
    @Repository
    class Accounts { /* ... */ }
  4. Install jMolecules

    main

    To use jMolecules, declare a dependency in your project. It is recommended to use the jmolecules-bom to manage versions across all modules.

    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")
    }
  5. Express architectural concepts using annotations

    main

    jMolecules provides annotations to describe high-level architectural styles like Layered, Onion, and Hexagonal. You can annotate entire packages via package-info.java or annotate individual classes directly.

    Layered Architecture (jmolecules-layered-architecture)

    • @DomainLayer
    • @ApplicationLayer
    • @InfrastructureLayer
    • @InterfaceLayer

    Onion Architecture (jmolecules-onion-architecture)

    Classic Style:

    • @DomainModelRing
    • @DomainServiceRing
    • @ApplicationServiceRing
    • @InfrastructureRing

    Simplified Style:

    • @DomainRing
    • @ApplicationRing
    • @InfrastructureRing

    Hexagonal Architecture (jmolecules-hexagonal-architecture)

    • @Application
    • @PrimaryAdapter / @SecondaryAdapter
    • @PrimaryPort / @SecondaryPort

    CQRS Architecture (jmolecules-cqrs-architecture)

    • @Command
    • @CommandDispatcher
    • @CommandHandler
    • @QueryModel
    // Annotating a package via package-info.java
    @DomainLayer
    package org.acmebank.domain;
    
    import org.jmolecules.architecture.layered.*;
    
    // Annotating classes directly
    import org.jmolecules.architecture.layered.*;
    import org.jmolecules.ddd.annotation.*;
    
    @DomainLayer
    @Entity
    public class BankAccount { /* ... */ }
    
    @ApplicationLayer
    @Service
    public class TransferMoney { /* ... */ }
  6. Available jMolecules Libraries

    main

    jMolecules is split into several specialized libraries:

    • jmolecules-ddd: Annotations and interfaces for DDD building blocks (Value Objects, Entities, etc.).
    • jmolecules-events: Annotations and interfaces for expressing events.
    • kmolecules-ddd: A Kotlin-based version of jmolecules-ddd to improve Kotlin/Java interoperability with the type-based model.
    • jmolecules-architecture: Annotations for architectural styles (Layered, Onion, Hexagonal, CQRS).
    • jmolecules-integrations: Provides support for Spring, Data JPA, Data MongoDB, Data JDBC, and Jackson.