我应该把下面的方法放在哪里?

发布于 2024-12-11 02:34:37 字数 485 浏览 0 评论 0原文

我有 2 个具有以下关系的表:

Purchase 1:m Payment
  • 每次购买都有一个主键“purchaseId”。
  • 每笔支付都有一个主键“transactionId”和一个外键“purchaseId”;

我为每个表都有一个 DAO 类。现在我想实现一个函数

List<Payment> findPaymentsByPurchaseId(int purchaseId)

为了让API感觉更直观,我是否应该将此函数放在

  1. PurchaseDAO中以指示“如果我想根据Purchase信息查找任何内容,我只需调用PurchaseDAO中的函数”
  2. PaymentDAO以指示“如果我想查找Payment信息,我就调用PaymentDAO中的一个函数”

你觉得哪一个更直观呢?

I have 2 tables with the relationship:

Purchase 1:m Payment
  • Each purchase has a primary key "purchaseId".
  • Each payment has a primary key "transactionId" and a foreign key "purchaseId";

I have a DAO class for each table. Now I want to implement a function

List<Payment> findPaymentsByPurchaseId(int purchaseId)

To make the API feels more intuitive, should I put this function in

  1. PurchaseDAO to indicate "if I want to find anything based on Purchase information, I just call a function in PurchaseDAO"
  2. PaymentDAO to indicate "if I want to find Payment information, I just call a function in PaymentDAO"

Which one do you feel more intuitive?

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

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

发布评论

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

评论(3

小嗷兮 2024-12-18 02:34:37

我会将其放入 PaymentDAO 中,因为它会返回付款,但我也会使其更加通用

List<Payment> findByPurchase(Purchase purchase)

I'd put it in PaymentDAO as it's returning payments but I'd also make it more generic

List<Payment> findByPurchase(Purchase purchase)
终遇你 2024-12-18 02:34:37

它可以去多个地方。

在购买 DAO 中,您需要:

List<Payment> getPaymentsFor(Purchase) // or find..., or findByPurchase, or...

在支付 DAO 中,您需要:(

Purchase getPurchaseFor(Payment)

无论是 ID 还是对象更具哲学性而非技术性;使用任何对您有用的东西。暴露 ID 可能会被认为是一种有漏洞的抽象,尽管它更多的是一种认知性的,因为拥有 PK 并不意味着它一定是 DB PK,它可能只是一个 GUID。)

It could go multiple places.

In a purchase DAO, you'd want:

List<Payment> getPaymentsFor(Purchase) // or find..., or findByPurchase, or...

In a payment DAO, you'd want:

Purchase getPurchaseFor(Payment)

(Whether it's an ID or the object is more philosophical than technical; use whatever works for you. Exposing an ID might be considered a leaky abstraction, although it's more a cognitive one, since having a PK doesn't mean it must be a DB PK, it could just be a GUID.)

↙温凉少女 2024-12-18 02:34:37

采购“知道”其付款 - 所以询问它。又名“不要打电话给我,我会打电话给你”

我个人创建了一个类层次结构,例如

Purchases
Purchase 
PurchaseDAO

Payments 
Payment
PaymentDAO


Purchases purchases = Purchases.find(criteria); 
// user selects one of the purchases (say)
Purchase = purchase.load(purchaseId);
// calls 
PurchaseDAO.load(purchaseId);
// which calls
Payments payment = Purchase.findPayments(); 
// calls 
Payments.find(purchaseId);
// calls 
PaymentDAO.find(purchaseId);

最后您将拥有一个购买对象,您可以询问其付款情况。
如果购买时可能有很多付款,您可以推迟加载付款,直到需要为止。

这看起来像是额外的工作(确实如此),但它允许业务和持久性逻辑的清晰分离,并且我发现创建额外类的小额投资得到了回报。

A Purchase'knows' about its payments - so ask it. aka 'don't call me I'll call you'

Personally I create a class hierarchy like

Purchases
Purchase 
PurchaseDAO

Payments 
Payment
PaymentDAO


Purchases purchases = Purchases.find(criteria); 
// user selects one of the purchases (say)
Purchase = purchase.load(purchaseId);
// calls 
PurchaseDAO.load(purchaseId);
// which calls
Payments payment = Purchase.findPayments(); 
// calls 
Payments.find(purchaseId);
// calls 
PaymentDAO.find(purchaseId);

At the end you'll have a Purchase object which you can interrogate about its payments.
If there are likely to be many payments against a purchase you can defer loading the payments until needed.

This seems like extra work (and is) but it allows a clean seperation of business and persistence logic and I find pays back the small investment of creating the extra classes.

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