在哪一层DTO翻译上可以实现?
例如,我有一个服务:
@Service
class OrderService {
OrderDto getOrder(String id) {
return OrderMapper.toOrderDto(orderRepository.getOrder(id))
}
}
orderdto
具有字段字符串描述
我需要根据语言环境(EN,ES等)返回正确的描述
。那个翻译?我应该在ordermapper
中这样做吗?如果是这样,OrderMapper应该是弹簧组件。也许我应该创建翻译人员?
OrderController -> OrderService 1 -> orderRepository
2 -> OrderMapper
我应该添加一个新的翻译层吗?看起来,映射器不是一个好地方(SRP)
For example, I have a service:
@Service
class OrderService {
OrderDto getOrder(String id) {
return OrderMapper.toOrderDto(orderRepository.getOrder(id))
}
}
OrderDto
has the field String description
I need to return the correct description depending on locale (en, es, etc.)
Where should I do that translation? Should I do that in the OrderMapper
? if so, OrderMapper should be a spring component. Or maybe I should create a translator?
OrderController -> OrderService 1 -> orderRepository
2 -> OrderMapper
Should I add a new layer for translation? It looks, that Mapper is not a good place for that(SRP)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
[更新]:
您可以创建一个单例映射器,在服务层中使用它来进行翻译,并在映射器中实现映射逻辑,因为我建议使用 mapstruct
示例:
不要忘记实现了getters and setters,因为我建议使用 Lombok 。
[Update]:
You can create a Singleton Mapper, use it in the service layer to do the translation, and implement the mapping logic in the mapper, for that I suggest the use of MapStruct
Example:
Do not forget to have the getters and setters implemented, for that I suggest the use of Lombok.