Class JMoleculesDddRules

java.lang.Object
org.jmolecules.archunit.JMoleculesDddRules

public class JMoleculesDddRules extends Object
A set of ArchUnit rules that allow verification of domain models. In short the rules here verify:
  • Aggregates only refer to entities that are declared to be part of it.
  • References to other aggregates are established via Associations or identifier references.
  • Annotated identifiables do have an identifier.
  • Value objects and identifiers do not refer to identifiables.
Those rules are mostly driven by what's been presented by John Sullivan in his blog post here.
Author:
Oliver Drotbohm, Torsten Juergeleit
  • Constructor Details

    • JMoleculesDddRules

      public JMoleculesDddRules()
  • Method Details

    • all

      public static com.tngtech.archunit.lang.ArchRule all()
      An ArchRule that's composed of all other rules declared in this class.
      Returns:
      See Also:
    • entitiesShouldBeDeclaredForUseInSameAggregate

      public static com.tngtech.archunit.lang.ArchRule entitiesShouldBeDeclaredForUseInSameAggregate()
      An ArchRule that verifies that fields that implement Entity within a type implementing AggregateRoot declare the aggregate type as the owning aggregate.

       class Customer implements AggregateRoot<Customer, CustomerId> { … }
       class Address implements Entity<Customer, AddressId> { … }
      
       class LineItem implements Entity<Order, LineItemId> { … }
       class Order implements AggregateRoot<Order, OrderId> {
      
         List<LineItem> lineItems; // valid
         Address shippingAddress; // invalid as Address is declared to belong to Customer
       }
       
      Returns:
      will never be null.
    • aggregateReferencesShouldBeViaIdOrAssociation

      public static com.tngtech.archunit.lang.ArchRule aggregateReferencesShouldBeViaIdOrAssociation()
      An ArchRule that ensures that one AggregateRoot does not reference another via the remote AggregateRoot type but rather via their identifier type or an explicit Association type.

       class Customer implements AggregateRoot<Customer, CustomerId> { … }
      
       class Order implements AggregateRoot<Order, OrderId> {
      
         Customer customer; // invalid
         CustomerId customerId; // valid
         Association<Customer> customer; // valid
       }
       
      Returns:
      will never be null.
    • annotatedEntitiesAndAggregatesNeedToHaveAnIdentifier

      public static com.tngtech.archunit.lang.ArchRule annotatedEntitiesAndAggregatesNeedToHaveAnIdentifier()
      Verifies that classes annotated with AggregateRoot or Entity declare a field annotated with Identifier.
      Returns:
      will never be null.
    • valueObjectsMustNotReferToIdentifiables

      public static com.tngtech.archunit.lang.ArchRule valueObjectsMustNotReferToIdentifiables()
      Verifies that value objects and identifiers do not refer to identifiables.
      Returns:
      will never be null.