Interface Repository<A extends AggregateRoot<I>,​I>

  • Type Parameters:
    A - the type of the aggregate root class.
    I - the type of identifier of the aggregate root class.

    @DomainRepository
    public interface Repository<A extends AggregateRoot<I>,​I>
    A repository is responsible for consistently storing and retrieving a whole aggregate. It has a simple collection-like global interface and optionally domain-specific methods.

    A repository is responsible for storing and retrieve a whole aggregate. It manipulates the aggregate through its root entity. It cannot directly store or retrieve parts of the aggregate.

    A repository provides the illusion of an in-memory collection of all objects that are of the corresponding aggregate root type.

    A repository implements a well-known interface that provides methods for adding, removing and querying objects.

    A repository optionally implements methods that select objects based on criteria meaningful to domain experts. Those methods return fully instantiated objects or collections of objects whose attribute values meet the criteria.

    Example:

     public interface SomeRepository extends Repository<SomeAggregate, SomeId> {
         // Optional business-meaningful methods
         List<SomeAggregate> objectsByCategory(String category);
     }
    
     public class SomeJpaRepository implements SomeRepository {
        @Override
         public List<SomeAggregate> objectsByCategory(String category) {
             // implement specific query
         }
     }
     
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Interface Description
      static interface  Repository.Option
      Marker interface for options that can be used to alter the results of some repository methods.
    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      void add​(A aggregate)
      Adds an aggregate to the repository.
      default A addOrUpdate​(A aggregate)
      Adds an aggregate to the repository or updates it if it already exists.
      default void clear()
      Removes all aggregates from the repository.
      default boolean contains​(A aggregate)
      Checks that the specified aggregate is present in the repository.
      default boolean contains​(I id)
      Checks that the aggregate identified by the specified identifier is present in the repository.
      default boolean contains​(Specification<A> specification)
      Check if at least one aggregate satisfying the specified specification is present in the repository.
      default long count​(Specification<A> specification)
      Count the number of aggregates in the repository satisfying the given specification.
      default Optional<A> get​(I id)
      Gets an aggregate identified by its identifier.
      Stream<A> get​(Specification<A> specification, Repository.Option... options)
      Finds all aggregates in the repository satisfying the given specification.
      Class<A> getAggregateRootClass()
      Returns the aggregate root class managed by the repository.
      Class<I> getIdentifierClass()
      Returns the aggregate root identifier class managed by the repository.
      SpecificationBuilder getSpecificationBuilder()
      Access to the specification builder.
      default boolean isEmpty()
      Return true if the repository is empty (i.e.
      default void remove​(A aggregate)
      Removes the specified aggregate from the repository.
      default void remove​(I id)
      Removes the existing aggregate identified with the specified identifier.
      long remove​(Specification<A> specification)
      Removes all aggregates in the repository satisfying the given specification.
      default long size()
      Returns the number of aggregates in the repository.
      default A update​(A aggregate)
      Updates an existing aggregate with the specified instance.
    • Method Detail

      • get

        Stream<A> get​(Specification<A> specification,
                      Repository.Option... options)
        Finds all aggregates in the repository satisfying the given specification. Options can be specified to alter the returned stream (order, limits, ...).
        Parameters:
        specification - the specification aggregates must satisfy.
        options - result options.
        Returns:
        a stream of aggregates.
        See Also:
        Repository.Option
      • get

        default Optional<A> get​(I id)
        Gets an aggregate identified by its identifier.
        Parameters:
        id - the aggregate identifier.
        Returns:
        an optional of the corresponding aggregate.
      • contains

        default boolean contains​(Specification<A> specification)
        Check if at least one aggregate satisfying the specified specification is present in the repository.
        Parameters:
        specification - the specification.
        Returns:
        true if at least one aggregate satisfying the specification is present, false otherwise.
      • contains

        default boolean contains​(I id)
        Checks that the aggregate identified by the specified identifier is present in the repository.
        Parameters:
        id - the aggregate identifier.
        Returns:
        true if the aggregate is present, false otherwise.
      • contains

        default boolean contains​(A aggregate)
        Checks that the specified aggregate is present in the repository. The check is only done on the aggregate identity, so this method is equivalent to calling contains(aggregate.getId() ).
        Parameters:
        aggregate - the aggregate identifier.
        Returns:
        true if the aggregate is present, false otherwise.
      • count

        default long count​(Specification<A> specification)
        Count the number of aggregates in the repository satisfying the given specification.
        Parameters:
        specification - the specification aggregates must satisfy.
        Returns:
        the number of aggregates in the repository satisfying the specification.
      • size

        default long size()
        Returns the number of aggregates in the repository.
        Returns:
        the number of aggregates in the repository.
      • isEmpty

        default boolean isEmpty()
        Return true if the repository is empty (i.e. contains no aggregate) and false otherwise (i.e. contains at least one aggregate).
        Returns:
        true if repository is empty, false otherwise.
      • remove

        long remove​(Specification<A> specification)
             throws AggregateNotFoundException
        Removes all aggregates in the repository satisfying the given specification.
        Parameters:
        specification - the specification aggregates must satisfy.
        Returns:
        the number of aggregates removed from the repository.
        Throws:
        AggregateNotFoundException - if the repository doesn't contain the aggregate.
      • remove

        default void remove​(I id)
                     throws AggregateNotFoundException
        Removes the existing aggregate identified with the specified identifier.
        Parameters:
        id - the identifier of the aggregate to remove.
        Throws:
        AggregateNotFoundException - if the repository doesn't contain the aggregate.
      • remove

        default void remove​(A aggregate)
                     throws AggregateNotFoundException
        Removes the specified aggregate from the repository.
        Parameters:
        aggregate - the aggregate to remove.
        Throws:
        AggregateNotFoundException - if the repository doesn't contain the aggregate.
      • update

        default A update​(A aggregate)
                  throws AggregateNotFoundException
        Updates an existing aggregate with the specified instance.
        Parameters:
        aggregate - the aggregate to update.
        Returns:
        the update aggregate.
        Throws:
        AggregateNotFoundException - if the repository doesn't contain the aggregate.
      • addOrUpdate

        default A addOrUpdate​(A aggregate)
        Adds an aggregate to the repository or updates it if it already exists. This operation can be useful in some circumstances but operations with clearer semantics like add(A) and update(AggregateRoot) should be preferred.
        Parameters:
        aggregate - the aggregate to update.
        Returns:
        the update aggregate.
        Throws:
        AggregateNotFoundException - if the repository doesn't contain the aggregate.
      • clear

        default void clear()
        Removes all aggregates from the repository.
      • getAggregateRootClass

        Class<A> getAggregateRootClass()
        Returns the aggregate root class managed by the repository.
        Returns:
        the aggregate root class.
      • getIdentifierClass

        Class<I> getIdentifierClass()
        Returns the aggregate root identifier class managed by the repository.
        Returns:
        the aggregate root identifier.
      • getSpecificationBuilder

        SpecificationBuilder getSpecificationBuilder()
        Access to the specification builder.
        Returns:
        the specification builder.