我应该把下面的方法放在哪里?
我有 2 个具有以下关系的表:
Purchase 1:m Payment
- 每次购买都有一个主键“purchaseId”。
- 每笔支付都有一个主键“transactionId”和一个外键“purchaseId”;
我为每个表都有一个 DAO 类。现在我想实现一个函数
List<Payment> findPaymentsByPurchaseId(int purchaseId)
为了让API感觉更直观,我是否应该将此函数放在
- PurchaseDAO中以指示“如果我想根据Purchase信息查找任何内容,我只需调用PurchaseDAO中的函数”
- 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
- PurchaseDAO to indicate "if I want to find anything based on Purchase information, I just call a function in PurchaseDAO"
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会将其放入
PaymentDAO
中,因为它会返回付款,但我也会使其更加通用I'd put it in
PaymentDAO
as it's returning payments but I'd also make it more generic它可以去多个地方。
在购买 DAO 中,您需要:
在支付 DAO 中,您需要:(
无论是 ID 还是对象更具哲学性而非技术性;使用任何对您有用的东西。暴露 ID 可能会被认为是一种有漏洞的抽象,尽管它更多的是一种认知性的,因为拥有 PK 并不意味着它一定是 DB PK,它可能只是一个 GUID。)
It could go multiple places.
In a purchase DAO, you'd want:
In a payment DAO, you'd want:
(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.)
采购“知道”其付款 - 所以询问它。又名“不要打电话给我,我会打电话给你”
我个人创建了一个类层次结构,例如
最后您将拥有一个购买对象,您可以询问其付款情况。
如果购买时可能有很多付款,您可以推迟加载付款,直到需要为止。
这看起来像是额外的工作(确实如此),但它允许业务和持久性逻辑的清晰分离,并且我发现创建额外类的小额投资得到了回报。
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
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.