簡體   English   中英

C#接口存儲庫方法中的不同變量類型

[英]C# Interface Different Variable Types in Repository Methods

我想實現一個接口和存儲庫模式。 我不同的存儲庫可以具有類型數據類型,

第一個是(String-int,char-long)

第二個是(字符雙,雙字符串)。 模式不斷發展,我們有大約50種不同的方法,我們正在將接口/存儲庫更改為另一個數據庫系統等。

如何編輯下面的界面,以允許不同的數據類型? 謝謝,

public interface ITransactionRepository
{
    void SearchTransactionbyCategoryCustomerId(Category, CustomerId ); // what should I write here?
    void SearchTransactionbyProductDepartment(Product, Department); 
    ......
}


public class TransactionRepository1: IRepository
{
    void SearchTransactionbyCategoryCustomerId(string Category, int CustomerId);
    void SearchTransactionbyProductDepartment(char Product, long Department); 
    ......
}

public class TransactionRepository2: IRepository
{
    void SearchTransactionbyCategoryCustomerId(char Category, double CustomerId);
    void SearchTransactionbyProductDepartment(double Product, string Department); 
    ......
}

將您的接口定義為通用接口,並在實現中指定實際的類型,

public interface ITransactionRepository<TCategory, TCustomerId, TProduct, TDepartment>
{
    void SearchTransactionbyCategoryCustomerId(TCategory Category, TCustomerId CustomerId );
    void SearchTransactionbyProductDepartment(TProduct Product, TDepartment Department); 
    ......
}


public class TransactionRepository1: ITransactionRepository<string, int, char, long>
{
    void SearchTransactionbyCategoryCustomerId(string Category, int CustomerId);
    void SearchTransactionbyProductDepartment(char Product, long Department); 
    ......
}

public class TransactionRepository2: ITransactionRepository<char, double, double, string>
{
    void SearchTransactionbyCategoryCustomerId(char Category, double CustomerId);
    void SearchTransactionbyProductDepartment(double Product, string Department); 
    ......
}

暫無
暫無

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

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