存儲庫模式問題 (Repository Pattern Question)


問題描述

存儲庫模式問題 (Repository Pattern Question)

I have two repository classes below, MoneyTransferRepository class is used by other classes in my project. I have designed it this way ‑ is it right? If not, what is the best way?

Thanks

public interface IMoneyTransferRepository
{
    void UpdateBalance();
}

public interface IOrderRepository
{
    void Checkout();
    void SaveOrder();
}

public class MoneyTransferRepository : IMoneyTransferRepository
{
    DBDataContext DB;

    public MoneyTransferRepository(IDbConnection connection)
    {
        DB = new DBDataContext(connection);
    }

    public void UpdateBalance()
    {
        //do something DB.Table1.Update
    }

}


public class OrderRepository : IOrderRepository,IMoneyTransferRepository
{

    DBDataContext DB;
    IMoneyTransferRepository moneyTransferRepository;

    public OrderRepository()
    {
        DB = new DBDataContext();
        moneyTransferRepository = new MoneyTransferRepository(DB.Connection);
    }

    public void Checkout()
    {
        TransactionOptions transactionOptions = new TransactionOptions();
        transactionOptions.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;

        using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required, transactionOptions))
        {
            try
            {
                UpdateBalance();
                SaveOrder();
                transactionScope.Complete();
            }
            catch
            {

            }

        }
    }

    public void UpdateBalance()
    {
        moneyTransferRepository.UpdateBalance();
    }

    public void SaveOrder()
    {
        //do something DB.SaveOrder.Update ......
        //   DB.Updatestock .....

    }
}

‑‑‑‑‑

參考解法

方法 1:

This is most definitely not the correct usage of the repository pattern.  I find the easiest way to think about the repository is to imagine every Order in your system is available in a big collection (the repository).  That said you probably want to add a way to query for specific objects from the repository (either via a Query pattern or direct method calls).

In our system we would have a service layer method called Checkout that would look like this...

    public void Checkout(int customerId)
    {
        if(CustomerHasOpenOrder(customerId) == false)
        { 
           // do something depending on your standards.  For us we'd 
           // throw an exception
        }

        Order orderToCheckout = m_OrderRepository.FindOpenOrderForCustomer(customerId);

        orderToCheckout.Checkout();

        m_OrderRepository.Save(orderToCheckout);
    }

In our system we don't actually use int's for identifying Orders but you get the idea...

方法 2:

Don't do transaction management in Repository. Thats why you get confused.

More info is here: Transactions in the Repository Pattern

(by tobiasShane Courtrilleercu)

參考文件

  1. Repository Pattern Question (CC BY‑SA 3.0/4.0)

#repository #model-view-controller






相關問題

開發代碼時如何使用 Internet Subversion 存儲庫? (How to use an Internet Subversion respository when developing code?)

在線庫報告的錯誤 (Online repository for reported bugs)

如何在Subversion儲存庫歷史記錄中查找文件? (How Do I Find a File in a Subversion Repository History?)

如何從 Nexus Professional 遷移到 Nexus OSS (How to migrate from Nexus Professional to Nexus OSS)

如何查看存儲庫? (How to see repositories?)

將遠程 github 存儲庫中的更改合併到本地存儲庫 (Merge changes from remote github repository to your local repository)

如何為 Xcode 項目設置 Git 存儲庫? (How to set up a Git repository for an Xcode project?)

如何簽入從不同存儲庫簽出的代碼 (How to check in code which is checked out from different repository)

如何使 Hg/Git 成為遠程倉庫? (How to make Hg/Git a remote repo?)

繼承是一種“is-a”關係;總結“界面”關係的好方法是什麼?——總結其他關係? (inheritance is an 'is-a' relationship; What is a good way to summarize the 'interface' relationship? -- Summarizing other relationships?)

存儲庫模式問題 (Repository Pattern Question)

為 Mac 和 Windows 設置 React Native 代碼庫 (setting up react native code repository for Mac & Windows)







留言討論