[英]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. 我正在使用Spring Boot数据REST制作在线拍卖REST API。 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. 我有2个实体。...Listing和ListedItem,其中Listed项目是待售项目,Listing由ListedItem组成,并且包含一些有关何时开始和结束上市的其他数据。
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. 我感觉清单在这种情况下必须是聚合根,这样就可以控制ListedItem,如果我删除清单,该项目也将被删除。
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. 我将需要首先发布我的ListedItem,以便随后可以发布带有链接的ListedItem的列表。
How can I now POST a ListedItem using spring data rest? 现在如何使用Spring Data Rest发布ListedItem? 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. 我希望能够将ListedItem POST到/ api / listed-item,但是如果我只有每个聚合路由的存储库,则在使用ddd时无法解决如何执行此操作。
Surely ListedItem needs its own repository if I am top persist it? 如果我坚持不懈,肯定ListedItem需要自己的存储库吗?
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.
我将需要首先发布我的ListedItem,以便随后可以发布带有链接的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. 如果
Listing
是聚合根,并且没有它的ListedItem
实体就不能存在,则必须同时创建两者。 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. 因此,您很可能只需将
POST
到具有必要数据的/listings
资源中,即可同时创建Listing
及其ListedItem
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.