单向 vs 双向

huangapple go评论73阅读模式
英文:

Unidirectional vs Bi-directional

问题

以下是翻译好的部分:

我有一个基本的日记项目,以及与两个实体User和DiaryEntry之间的一对多关系。问题是我有两种添加数据的方式,一种是双向的,另一种是单向的。双向方法的问题是我无法获取最近添加到数据库中的日记对象的ID以发送给用户,但我不确定这实际上有多重要。另一种方法是使用单向方法,它允许我直接获取添加的日记对象并将其返回给用户,但大多数在线建议使用双向方法。任何建议都将不胜感激,因为我只是想熟悉Spring Rest应用程序的最佳方法。

public void createDiaryEntryNoResult(Long uId, DiaryEntryDTO diaryEntryDTO) {

     userRepository.findById(uId)
            .map(user -> {
                DiaryEntry diaryEntry = 
     diaryEntryMapper.diaryEntryDTOToDiaryEntry(diaryEntryDTO);
                user.addDiaryEntry(diaryEntry);
                userRepository.save(user);
                return user;
            })
            .orElseThrow(ResourceNotFoundException::new);

}
public DiaryEntryDTO createDiaryEntryWithResult(Long uId, DiaryEntryDTO diaryEntryDTO) {

    DiaryEntry diaryEntry = diaryEntryMapper.diaryEntryDTOToDiaryEntry(diaryEntryDTO);
    User user = userRepository.findById(uId).orElseThrow(ResourceNotFoundException::new);
    diaryEntry.setUser(user);
    DiaryEntry savedDiaryEntry = diaryEntryRepository.save(diaryEntry);
    return diaryEntryMapper.diaryEntryToDiaryEntryDTO(savedDiaryEntry);
}
英文:

I have a basic diary project and a one to many relationship with two entities, User and DiaryEntry. The problem is I have two ways to add data, one being bi-directional and the other uni. The trouble with the bi-directional approach is that I can not get the id off the diary object recently added to the database to send to the user but I am not sure how important this actually is. The other approach is to use a uni directional which allows me to get the diary object added directly and return it to the user but most advice online is to use bi-directional approaches. Any advice appreciated as I am just trying to get familiar with the best approaches to spring rest apps.

public void createDiaryEntryNoResult(Long uId, DiaryEntryDTO diaryEntryDTO) {

         userRepository.findById(uId)
                .map(user -> {
                    DiaryEntry diaryEntry = 
         diaryEntryMapper.diaryEntryDTOToDiaryEntry(diaryEntryDTO);
                    user.addDiaryEntry(diaryEntry);
                    userRepository.save(user);
                    return user;
                })
                .orElseThrow(ResourceNotFoundException::new);

}
public DiaryEntryDTO createDiaryEntryWithResult(Long uId, DiaryEntryDTO diaryEntryDTO) {

        DiaryEntry diaryEntry = diaryEntryMapper.diaryEntryDTOToDiaryEntry(diaryEntryDTO);
        User user = userRepository.findById(uId).orElseThrow(ResourceNotFoundException::new);
        diaryEntry.setUser(user);
        DiaryEntry savedDiaryEntry = diaryEntryRepository.save(diaryEntry);
        return diaryEntryMapper.diaryEntryToDiaryEntryDTO(savedDiaryEntry);
}

答案1

得分: 1

"双向方法的问题在于我无法获取最近添加到数据库中的日记对象的 id,以便将其发送给用户,但我不确定这实际上有多重要。"

在调用 save 方法后,id 应该存在于 savedDiaryEntry 中...

没有"正确"的方法 - 这取决于使用 API 的人。

他们是否在此刻的响应中需要该 id,还是仅用于信息目的?如果出于某种原因,您需要该 id 来调用其他一些 API 方法,那么正确的方法当然是在响应中返回该 id,否则返回无内容即可。

如果他们此刻不需要该 id,则仅返回"200 OK"或"201 No content"的响应,而无需返回任何主体,这是正确的方法,一切取决于调用 API 的人在当时需要什么或什么对他们更方便...

英文:

"The trouble with the bi-directional approach is that I can not get the id off the diary object recently added to the database to send to the user but I am not sure how important this actually is."

The id should be present in savedDiaryEntry after calling save method...

There is no "correct" approach - it depends on the person using the API.

Do they need the id in the response at that moment or is it for information purpose only? If for some reason you need that id to call some other API methods then correct approach would be of course to return the id in the response otherwise it's ok to return no content.

If they don't need the id at the moment then returning only "200 OK" or "201 No content" response with no body is correct approach, it all depends on what the person calling the API will need at the time or what's more convenient for them...

huangapple
  • 本文由 发表于 2020年9月11日 05:26:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63837907.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定