C#接口存储库方法中的不同变量类型

时间:2018-05-30 00:14:55

标签: c# asp.net-core interface repository

我想实现一个接口和存储库模式。我的不同存储库可以有类型数据类型,

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

第二个是(Char-double,double-string)。 模式不断发展,我们有大约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); 
    ......
}

1 个答案:

答案 0 :(得分:1)

将您的界面定义为通用,并在实现中指定实际类型,

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); 
    ......
}