简体   繁体   中英

Spring data rest Domain Driven Design - Posting non aggregate root entities

I am making an on-line auction REST api using spring boot data REST. I am trying to use the Domain Driven Design appoach.

I have 2 entities....Listing and ListedItem, where Listed item is the item for sale and Listing is composed of the ListedItem and holds some other data about when the listing starts and ends, among other things.

I am feeling that the Listing must be the aggregate root in this situation so that will control the ListedItem and if I remove the listing the item is removed too.

So I have a repository for the aggregate root (Listing).

I will need to POST my ListedItem first so that I can then POST a listing with its linked ListedItem.

How can I now POST a ListedItem using spring data rest? No endpoint is exposed for this as it has no repository of its own.

I would expect to be able to POST ListedItem to /api/listed-item but I can't work out how to do this when using ddd if I only have a repository for each aggregate route.

Surely ListedItem needs its own repository if I am top persist it?

Here are my entities and repository in case it helps:

@Entity
@Table(name = "listed_item")
@Getter
@ToString
@EqualsAndHashCode
public class ListedItem extends BaseEntityModel {

    private String name;

    private String shortDescription;

    private String fullDescription;

}

@Entity
@Table(name = "listing")
@Getter
@ToString
@EqualsAndHashCode
public class Listing extends BaseEntityModel {

    @OneToOne
    private ListedItem listedItem;

    @Enumerated
    private PossibleListingState currentState;

    private long numBids;

    public Listing() {

    }

    public PossibleState getCurrentState() {
        return currentState;
    }

    public void setCurrentState(PossibleListingState currentState) {
        this.currentState = currentState;
    }
}



@RepositoryRestResource(collectionResourceRel = "listings", itemResourceRel = "listing")
public interface ListingRepository extends PagingAndSortingRepository<Listing, String> {
}

I will need to POST my ListedItem first so that I can then POST a listing with its linked ListedItem.

This is a misconception. If Listing is the aggregate root and cannot exist without it's ListedItem entity then both have to be created at the same time. Therefore, you'd most likely just POST to a /listings resource with the necessary data to create both, the Listing and it's ListedItem at once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM