清洁体系结构中存储库中的非域数据

发布于 2025-02-02 02:11:13 字数 1175 浏览 2 评论 0原文

我想知道,如果在域层中描述了存储库接口,应该将DTO放在哪里,而该域层中的存储库界面除了域概念以外的任何内容,但我必须将一些信息传递给/来自这些存储库的信息实际上是实现细节。

我举一个例子。假设我们有前端应用程序或可能的后端查询外部服务/查询数据库,但必须从那里获取一些技术数据。

我们描述了一个存储库,可以说功能/商店/域/存储库。 我在这里跳过Eithers和其他东西。

abstract class ProductsRepository {
  Future<List<Product>> getProductsList(String category, int page);
}

尽管“页面”可能不是域概念,但我想现在这很好。 然后,如果我们需要传递一些额外的元数据以提出成功请求 - 我们可以将其添加到实施中的实际请求中。但这并不总是可能,有时没有其他方法,然后通过争论传递一些元数据。 而且,如果我们还需要从响应中提供一些元数据 - 我看不到在不描述域层中的DTO的情况下做到这一点的方法。这样:例如,

abstract class ProductsRepository {
  Future<GetProductsListResponseDTO> getProductsList(String category, int page);
}

class GetProductsListResponseDTO {
  final List<Product> products;
  final bool isLastPage;
  final int timestamp;

  GetProductsListResponseDTO(this.products, this.hasMore);
}

iSlastPage用于懒惰加载,例如,时间戳用于分析。 不是最好的,因为这些功能可以用其他方式实施,但这正是我想到的,假设我们对此有严格的要求。在实际应用中,可能会有很多这样的数据,甚至可能像某些元数据一样坚持实体本身,然后在其他地方用于技术目的/仅用于UI。 这将打破域层的定义,即知识的界限。

您对在这种情况下应该做什么有任何建议?

一般的问题是如何处理来自外部来源的非域目的数据,并应传递给更高级别,例如要处理的应用程序/视图。

I'm wondering where should DTOs be put if repository interfaces are described in the domain layer which shouldn't ideally know about anything besides domain concepts, but i have to pass some information to/from these repositories that is actually an implementation details.

I'll give an example. Lets assume we have frontend app or perhaps backend querying external service/querying database, but obliged to get some technical data from there.

We describe a repository in lets say features/shop/domain/repositories.
I skip eithers and other stuff here.

abstract class ProductsRepository {
  Future<List<Product>> getProductsList(String category, int page);
}

And that's quite fine for now i suppose, despite "page" may not be much of domain concept.
Then if we need to pass some additional metadata to make successful request - we can add it to the actual request in implementation. But it's not always possible, sometimes there's no other way then passing some metadata through arguments.
And if we need to also provide some metadata from response - i don't see a way to do it without describing a DTO in domain layer. Like that:

abstract class ProductsRepository {
  Future<GetProductsListResponseDTO> getProductsList(String category, int page);
}

class GetProductsListResponseDTO {
  final List<Product> products;
  final bool isLastPage;
  final int timestamp;

  GetProductsListResponseDTO(this.products, this.hasMore);
}

Where isLastPage is for lazy loading and timestamp is for analytics for example.
Not the best as these functionalities could be implemented in other way, but it's just what came into my mind, lets assume we have strict requirements on that. In real applications there may be plenty of that kind of data, maybe even sticked to entities themselves like some metadata then used elsewhere for technical purposes/for ui only maybe.
And that would sort of break a definition of domain layer, the boundaries of it's knowledge.

Do you have any suggestions on what should be done in this case?

The general question is how to deal with data of non domain purpose that comes from external sources and should be passed to higher levels like application/view to deal with.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

苏大泽ㄣ 2025-02-09 02:11:13

实际上,我对该主题有一些启蒙)
如果有人在寻找答案,则将元数据添加到模型中,我会喜欢这样的

// features/shop/data/models

class ProductMetadata {
  // whatever
}

class ProductModel extends Product {
  final ProductMetadata metadata;

  ProductModel(super.id, super.name, super.price, super.discount, super.imageUrl, this.metadata);
}

// features/shop/data/repositories
class ProductsRepositoryImpl implements ProductsRepository {
  @override
  Future<List<ProductModel>> getProductsList(String category, int page) {
    // TODO: implement getProductsList
    throw UnimplementedError();
  }
}

整个响应,我认为可以应用同样的响应,但是无论如何都应该在域层中创建一些响应类,然后在数据/基础架构中扩展。就像

class ProductsListResponse {
  final List<Product> products;

  ProductsListResponse(this.products);
}

abstract class ProductsRepository {
  Future<ProductsListResponse> getProductsList(String category, int page);
}

这可能不是最好的,而是一种解决方案,无论哪种方式,任何想法仍然都受到赞赏。

Actually, i've got some enlightment on that topic)
If someone's seeking for an answer, for adding metadata to models, i would do like that

// features/shop/data/models

class ProductMetadata {
  // whatever
}

class ProductModel extends Product {
  final ProductMetadata metadata;

  ProductModel(super.id, super.name, super.price, super.discount, super.imageUrl, this.metadata);
}

// features/shop/data/repositories
class ProductsRepositoryImpl implements ProductsRepository {
  @override
  Future<List<ProductModel>> getProductsList(String category, int page) {
    // TODO: implement getProductsList
    throw UnimplementedError();
  }
}

For the whole responses i think the same could be applied, but some response classes should be created in domain layer anyway to be then extended in data/infrastructure. Like

class ProductsListResponse {
  final List<Product> products;

  ProductsListResponse(this.products);
}

abstract class ProductsRepository {
  Future<ProductsListResponse> getProductsList(String category, int page);
}

Which may not be the best but a solution though, either way any ideas are still appreciated.

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