设计问题: 如何实现一个能够利用不同接口实现的函数。

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

Design question: how to implement a function which utilizes different interface implementations

问题

我最近在开发一个相当大的Web应用程序,对底层概念不太熟悉。

所以我的问题是:

任务:

  • 删除用户收集的所有数据(设置、用户数据(用户名和密码)以及保龄球统计数据(比赛、投球系列和单次投球数据)。
    • 用户名和密码
    • 用户设置
    • 有关保龄球的统计数据,如比赛、投球系列和单次投球数据

迄今为止已经实施的代码:

  • 接口 UsersService.kt,在 UsersServiceImpl.kt 中实现
  • 接口 ShotsService.kt,在 ShotServiceImpl.kt 中实现
  • 接口 SettingsService.kt,在 SettingsService.kt 中实现

与给定任务相关的参考:

我想扩展 UsersServiceImpl.kt 中的删除函数的实现逻辑,以便在这种情况下,当调用该函数时,它不仅会删除相应的用户名和密码,还会首先删除设置和球员统计数据。

这样做的正确方式是在 UsersServiceImpl.delete() 函数内部调用其他接口的实现吗?

override fun delete(userId: UserId) {
    // 我的附加代码
    shotServiceImpl.deleteShots(userID)
    shotServiceImpl.deleteSeries(userID)
    shotServiceImpl.deleteMatches(userID)
    settingsServiceImpl.deleteSettings(userID)

    // 之前只有以下两行存在。
    usersRepository.deleteById(userId)
    userSecretsRepository.deleteById(userId)
}

还是有其他推荐的解决方法?

英文:

I'm working on a quite big Web-Application as of late and I am unfamiliar with the underlying concepts.

So my question is:

TASK:

  • delete all data gathered by a user (settings, userdata (username & password) and bowling stats (games, shot series and single shot data))
    • username and password,
    • Settings set by user
    • stats about bowling like games, shot series, single shot

Existing implemented code so far:

  • Interface UsersService.kt, implemented in UsersServiceImpl.kt

  • Interface ShotsService.kt, implemented in ShotServiceImpl.kt

  • Interface SettingsService.kt, implemented in SettingsService.kt

Reference to given task:

I want to extend the logic of the implementation of the delete-function in UsersServiceImpl.kt, so that in this case when this function is called, it does not only delete the corresponding username and password, but settings and player stats first.

Is the correct way to do this to call the implementations of the other interfaces inside the UsersServiceImpl.delete()-function?

 override fun delete(userId: UserId) {
    // my additional code
    shotServiceImpl.deleteShots(userID)
    shotServiceImpl.deleteSeries(userID)
    shotServiceImpl.deleteMatches(userID)
    settingsServiceImpl.deleteSettings(userID)

    // previously only this following two lines existed.
    usersRepository.deleteById(userId)
    userSecretsRepository.deleteById(userId)

}

Or is there another way which is recommended to solve this?

答案1

得分: 1

保持关注点分离。UsersService 中的 delete 函数应该只做一件事:删除用户。

删除所有与用户相关的设置是高级策略的一部分。您可以实现一个了解所有接口并按正确顺序执行它们相应的删除函数的服务。

我还建议您查看代码库的其他位置,以确定是否已经使用了特定的设计模式来处理这类任务。

英文:

Keep the concerns separated. The delete function in the UsersService should do one thing: deleting users.

Deleting all user related settings is part of a higher policy. You could implement a service that knows all interfaces and executes their corresponding delete functions in the correct order.

I would also advice you to look at other locations of the code base to figure out if specific design patterns for those kind of tasks are used already.

huangapple
  • 本文由 发表于 2023年3月7日 16:51:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659756.html
匿名

发表评论

匿名网友

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

确定