DDD - 返回数据库密钥的存储库模式?

发布于 2024-07-27 17:04:44 字数 491 浏览 2 评论 0原文

这里有一个很大的设计缺陷,但我很难解决它:

业务需求有点复杂,所以我会尽量保持简单。 我们有一张购买表和一张退货表。 当退货时,我们必须找到返回数据库中最旧购买的匹配项,并将其记录在“已应用退货”表中。

因此,当我在该交易中插入退货时,我需要将退货应用于购买记录。

就目前而言,我们有一个调用存储库进行插入的服务。 服务需要知道插入的记录的键是什么,以便它可以通过使用该键插入“已应用”记录来完成事务。

我们基本上陷入困境,因为我的理解是存储库不应返回此类数据。 这是否违背了存储库作为集合的想法?

还有什么选择呢?

澄清:

我们有一个购买表、一个退货表和一个已应用表。

已应用表如下所示 buyId returnId qtyReturned

因此,当插入退货时,我需要购买的 ID(由某些业务规则决定)和新插入的退货的 ID。

There is a big design flaw here, but I'm having trouble solving it:

The business need is a little involved so I'll try to keep this simple.
We have a table with purchases, and a table for returns. When a return is made, we have to find match that return to the oldest purchase in the db, and record that in a "returns applied" table.

So, when I insert a Return, within that transaction, I need to apply the return to a purchase record.

As it stands now, we have a service that calls the repository for the insert. The service needs to know what the key is of that inserted record, so that it can finish the transaction by inserting an "applied" record using that key.

We're basically stuck because my understanding is that a repository should not return this kind of data. Doesn't this defeat the idea of the Repository being a collection?

What is the alternative?

CLARIFICATION:

We have a Purchase table, a Return table, and an Applied table

The applied table looks like this
purchaseId returnId qtyReturned

So when a return is inserted I need the id of a purchase (decided by some business rules) and the id of the newly inserted return.

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

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

发布评论

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

评论(1

别理我 2024-08-03 17:04:44

根据你的问题我猜想如下:

public class Purchase {

   // ReturnRepository plays the role of collaborator
   public Return returnMe(PurchaseRepository purchaseRepository, int quantity) {
       return purchaseRepository.returnPurchase(this, quantity);
   }

}


public class PurchaseRepositoryImpl implements PurchaseRepository {

    // returnd Purchase object has its id set up
    public Purchase getOldestPurchase() {
        // logic in order to get oldest purchase
    }

    public Return returnPurchase(Purchase purchase, quantity) {
        // logic in order to save a return record
        // Some ORM frameworks returns ids when a record is saved. In my case, Hibernate or NHibernate (.NET) fulfill ths requirement

        // Then purchaseId, returnId and quantity is saved in "returns applied" table
    }

}


public class PurchaseServiceImpl implements PurchaseService {

   // Injected through dependency injection
   private PurchaseRepository purchaseRepository;

   // Spring transaction boundary, for example
   // Notice returnPurchase method returns a Return object
   public Return returnPurchase(int quantity) {
       Purchase purchase = purchaseRepository.getOldestPurchase();

       return purchase.returnMe(purchaseRepository, quantity);
   }

}

I suppose the following according to your question:

public class Purchase {

   // ReturnRepository plays the role of collaborator
   public Return returnMe(PurchaseRepository purchaseRepository, int quantity) {
       return purchaseRepository.returnPurchase(this, quantity);
   }

}


public class PurchaseRepositoryImpl implements PurchaseRepository {

    // returnd Purchase object has its id set up
    public Purchase getOldestPurchase() {
        // logic in order to get oldest purchase
    }

    public Return returnPurchase(Purchase purchase, quantity) {
        // logic in order to save a return record
        // Some ORM frameworks returns ids when a record is saved. In my case, Hibernate or NHibernate (.NET) fulfill ths requirement

        // Then purchaseId, returnId and quantity is saved in "returns applied" table
    }

}


public class PurchaseServiceImpl implements PurchaseService {

   // Injected through dependency injection
   private PurchaseRepository purchaseRepository;

   // Spring transaction boundary, for example
   // Notice returnPurchase method returns a Return object
   public Return returnPurchase(int quantity) {
       Purchase purchase = purchaseRepository.getOldestPurchase();

       return purchase.returnMe(purchaseRepository, quantity);
   }

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