Java DTO作为数据请求模型 – 模型范围与层次

huangapple 未分类评论49阅读模式
英文:

Java DTO as Data Request Model - scope of model vs layers

问题

TL;DR
我能否在 @Controller 和多个 @Service 中使用同一个数据模型?

假设我有一个 Spring @Controller,它通过 @RequestBody 接收请求模型,例如 PersonModel model。我想要注册这个人。

我通过 service.register(personModel) 在端点调用服务。

PersonService 在 register 方法中执行一些操作,例如:
emailService.sendEmail(personModel)
statisticService.register(personModel),等等

问题:
如你所见,这个模型在许多不同层次的服务中被使用。这非常方便,但这样做是否可以?或者我应该“克隆”模型,并为每个层次创建一个模型,例如 PersonModelEmailPersonModelStatistic

非常欢迎提供一些相关书籍或文章的参考资料。

英文:

TL;DR
Can I use one data model for @Controller and several @Services?

Imagine I have spring @Controller, which expects request model via @RequestBody - eg PersonModel model. I want to register the person.

I am calling service from endpoint by service.register(personModel).

PersonService in the register method do some stuff, eg:
emailService.sendEmail(personModel)
statisticService.register(personModel), etc

Question:
As you see, the model is used in many services in many different layers. It's very convenient but is it ok? Or maybe I should "clone" model and create each model per layer, eg PersonModelEmail , PersonModelStatistic?

Some references to books or articles would be more than welcome.

huangapple
  • 本文由 发表于 2020年5月29日 13:53:04
  • 转载请务必保留本文链接:https://java.coder-hub.com/62079526.html
匿名

发表评论

匿名网友

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

确定