DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Understanding MCP Architecture: LLM + API vs Model Context Protocol
  • Designing Docling Studio: Key Architecture Decisions
  • Designing Production-Grade AI Tools: Why Architecture Matters More Than Models
  • A Developer-Centric Cloud Architecture Framework (DCAF) for Enterprise Platforms

Trending

  • When 12 Hours of Offline Changes Collide: Designing Conflict-Safe Mobile Sync
  • Decoupling a Data Pipeline From the Tables It Was Never Supposed to Depend On
  • Member Spotlight: Raghava Dittakavi
  • Building IoT Time-Series Applications With Java and Apache IoTDB

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Tools
  4. Enhancing Software Governance and Quality With jMolecules: Fighting Software Erosion

Enhancing Software Governance and Quality With jMolecules: Fighting Software Erosion

jMolecules uses annotations to enforce architectural rules, prevent software erosion, and simplify validation with tools like ArchUnit. It supports DDD, CQRS, and more.

By 
Otavio Santana
DZone Core CORE ·
Nov. 27, 24 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
5.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Software erosion — gradual decay in software quality due to unmanaged technical debt and architectural drift — is a persistent challenge in software development. To combat this, jMolecules emerges as a robust, annotation-driven framework that simplifies the expression and enforcement of architectural principles within your codebase. By explicitly defining architectural concepts, jMolecules facilitates governance, quality assurance, and architectural integrity, ensuring your software remains robust.

In this article, we'll explore jMolecules's fundamentals, its benefits, and a practical example of integrating it with ArchUnit to enforce Domain-Driven Design (DDD) principles. For further details, you can visit the jMolecules GitHub repository.

Why jMolecules?

jMolecules provides developers with annotations that make architectural evidence explicit in the code, ensuring clarity and structure. It achieves this through:

  • Expressive Code: Architectural concepts are explicitly represented, aiding in code readability and maintainability.
  • Domain Isolation: Domain-specific code remains free from technical dependencies.
  • Boilerplate Reduction: Simplifies repetitive tasks, enabling developers to focus on logic.
  • Tool Integration:
    • Augmenting code with tools like ByteBuddy for Spring and JPA integration.
    • Enforcing architectural rules with tools like ArchUnit and jQAssistant.
  • Documentation Generation: Automatically generates architectural documentation and validates implementation.

Modular Design for Flexibility

jMolecules provides modules tailored to different architectural styles, allowing developers to adapt the framework to their needs. Some notable modules include:

  • jmolecules-cqrs-architecture: For Command-Query Responsibility Segregation (CQRS).
  • jmolecules-layered-architecture: For traditional layered architectures.
  • jmolecules-onion-architecture: For Onion architecture.
  • jmolecules-hexagonal-architecture: For Hexagonal architecture.
  • jmolecules-ddd: This defines DDD building blocks like entities, value objects, and aggregates.
  • jmolecules-events: For representing domain events.

A Practical Example: Ensuring DDD Compliance With ArchUnit

Defining a Domain Entity

Let's start by defining a simple domain entity — a credit card. Using the @Entity annotation from jMolecules, we define our entity as follows:

Java
 
@Entity
public class CreditCard {

    private BigInteger id;

    private String number;

    private String name;

    private YearMonth expiry;
}


Validating DDD Compliance

To ensure the entity adheres to DDD principles, we can use jMolecules with ArchUnit. There are two approaches: manual validation and annotation-driven validation.

Manual Validation

Java
 
public class JMoleculesDddUnitTest {

    @Test
    void checkTheLayerIntegration() {
        String packageName = "expert.os.examples";
        JavaClasses classes = new ClassFileImporter().importPackages(packageName);
        JMoleculesArchitectureRules.ensureLayering().check(classes);
    }

    @Test
    void checkDDDIntegration() {
        String packageName = "expert.os.examples";
        JavaClasses classes = new ClassFileImporter().importPackages(packageName);
        JMoleculesDddRules.all().check(classes);
    }
}


Annotation-Driven Validation

The integration becomes more concise with annotations:

Java
 
@AnalyzeClasses(packages = "expert.os.examples")
public class IntegrationSampleTest {


    @ArchTest
    private ArchRule dddRules = JMoleculesDddRules.all();

    @ArchTest
    private ArchRule layering = JMoleculesArchitectureRules.ensureLayering();


    @ArchTest
    void detectsViolations(JavaClasses classes) {
        EvaluationResult result = JMoleculesDddRules.all().evaluate(classes);
        assertThat(result.hasViolation()).isFalse();

    }

}


Running the tests may reveal violations, such as:

  • Type e.o.e.CreditCard must declare a field or a method annotated with org.jmolecules.ddd.annotation.Identity!

This error aligns with Eric Evans' DDD definition of an entity, emphasizing the importance of identity. To resolve this, we annotate the id field with @Identity:

Java
 
@Entity
public class CreditCard {

    @Identity
    private BigInteger id;

    private String number;

    private String name;

    private YearMonth expiry;
}


With this adjustment, the tests pass, ensuring our entity adheres to DDD principles.

Conclusion

jMolecules offers a comprehensive suite of validations and tools that are invaluable in real-world projects. While this article briefly introduces the framework's capabilities extend far beyond the scope covered here. Explore the project repository to dive deeper into jMolecules and its integration.

By leveraging jMolecules, developers can effectively combat software erosion, maintain architectural integrity, and build high-quality, sustainable software systems.

Video


Architecture Domain-driven design Tool Framework

Opinions expressed by DZone contributors are their own.

Related

  • Understanding MCP Architecture: LLM + API vs Model Context Protocol
  • Designing Docling Studio: Key Architecture Decisions
  • Designing Production-Grade AI Tools: Why Architecture Matters More Than Models
  • A Developer-Centric Cloud Architecture Framework (DCAF) for Enterprise Platforms

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook