简体   繁体   中英

C# Interface Different Variable Types in Repository Methods

I would like to implement a interface and repository pattern. My different repositories can have type data types,

First one is (String-int, char-long)

Second one is (Char-double, double-string). Pattern keeps going, we have around 50 different methods.We are changing our interface/repositories to another database system, etc.

How would I edit the edit the interface below, to allow different data types? Thanks,

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

Define your interface as generic and specify the actual types in the implementation,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM