在哪一层DTO翻译上可以实现?

发布于 2025-01-24 22:46:05 字数 530 浏览 3 评论 0原文

例如,我有一个服务:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扭转时空 2025-01-31 22:46:05

[更新]:
您可以创建一个单例映射器,在服务层中使用它来进行翻译,并在映射器中实现映射逻辑,因为我建议使用 mapstruct

示例:

@Mapper
public interface OrderMapper {

  @Mapping(target = "descritpion", source = "descr")
  OrderDto orderToOrderDto(Order order);

  @Mapping(target = "descritpion", source = "descr")
  Order orderDtoToOrder(OrderDto orderDto);
}

不要忘记实现了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:

@Mapper
public interface OrderMapper {

  @Mapping(target = "descritpion", source = "descr")
  OrderDto orderToOrderDto(Order order);

  @Mapping(target = "descritpion", source = "descr")
  Order orderDtoToOrder(OrderDto orderDto);
}

Do not forget to have the getters and setters implemented, for that I suggest the use of Lombok.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文