簡體   English   中英

使用Ado.Net在此通用存儲庫中是否有代碼氣味?

[英]Is there a code smell in this Generic Repository using Ado.Net?

我一直在閱讀這篇文章,以刷新我對存儲庫模式的理解,並發現我的存儲庫可以是適用於使用該存儲庫的業務層的多種方法的組合。 因此,有了這種了解,我想到了UserRepository的此類和接口

public class UserRepo : IUserRepo 
{

    public TheUser RegisterUser(UserRegistrationDetails details)
    {
        //The details here are what is required for inserting to the user table plus a
        //few other related tables which are decided and populated at the Business layer.
    }

    //example method returning data from a combination of multiple tables
    //and a dto created specially for that purpose
    public BaggageDetails GetBaggageDetailsForUser(string username)
    {
        //implementation here
    }

    //example method returning data from a combination of multiple 
    //different tables
    public TravelDetails GetTravelDetailsForUser(string username)
    {
        //implementation here
    }


 }

在這里,這些方法基本上返回一個自定義dto ,在每種情況下,該dto具有[Users]表的全部或某些字段,以及不屬於[Users]表的一堆其他字段。

然后,我碰到了這篇文章 ,其中談到了一個通用存儲庫,所以現在我的類和接口看起來像這樣

public class UserRepo : IDisposable, IUserRepo, IGenericRepo<TheUser> //,IGenericRepo<Users>
{

    public TheUser RegisterUser(UserRegistrationDetails details)
    {
        //
    }

    //example method....
    public BaggageDetails GetBaggageDetailsForUser(string username)
    {
        //implementation here
    }

    //example method....
    public TravelDetails GetTravelDetailsForUser(string username)
    {
        //implementation here
    }



    //New methods added for the generic repository
    public IList<TheUser> GetAll()
    {
        //
    }

    public TheUser GetById(int id)
    {
        //
    }

    public void Save(TheUser saveThis)
    {
        //
    }

    public void Delete(TheUser deleteThis)
    {
        //
    }

    //Should I implement this here?
    public void Dispose()
    {
        //TODO
    }
}

我可以通過許多這樣的接口實現UserRepo嗎? 通用存儲庫具有與存儲庫的聚合根相關的方法(如果我使用的是“正確”一詞)。

問題

  1. 我應該像在IGenericRepo<Users> IGenericRepo<TheUser>上面所做的那樣,將屬於聚合根的dto放到IGenericRepo<Users>還是將更大的dto IGenericRepo<Users>比更新[Users]表所需的信息更多的信息上。
  2. 現在,如果我出於測試目的模擬了UserRepo ,則不確定要使用哪個接口

編輯除了上述問題的答案以外,我還在尋找有關創建存儲庫的想法。 我知道這屬於“取決於”類別,但是很想聽聽大家對這種構建存儲庫的想法。 在現場項目中創建和使用存儲庫的人做了什么?

通用存儲庫只有在您以通用方式處理數據時才有意義。 經典的存儲庫模式會建立Get(GetAll / GetById等),Create和Delete,但是可能會跳過其中的一些。 這非常取決於您的應用程序。 重要的是要考慮到存儲庫為您的數據存儲創建了抽象級別,以便能夠用另一種替換現有的數據存儲,並且您更有可能使用一些模擬數據進行測試。

在您的示例中,您實現了非常特定的用戶存儲庫,而我看不到您的應用程序中還需要多少數據。 因此,您似乎在嘗試通過實現通用存儲庫接口來強制用戶存儲庫執行根本不需要的操作。

如果您確實具有基於數據處理的通用基礎,我建議使用通用方法。

對於一個特定的用例,DTO(數據傳輸對象)應在用戶端(例如UI)上提供所需的大量數據。 這實際上取決於您要一次可視化/使用多少數據,並且應該與數據傳輸成本和服務呼叫次數保持平衡。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM