0

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); 
    ......
}
4
  • Think about it for a moment. Let's imagine you could do where T: string, char (which you cannot), how would those methods look like? if T is string...? Commented May 30, 2018 at 0:22
  • 1
    Surely this isn't a representation of your actual system though...? Can you post something that more closely resembles what you're working with? Commented May 30, 2018 at 0:26
  • Should IRepository really be ITransactionRepository or is something missing? Commented May 30, 2018 at 0:54
  • I'm curious why you would represent the same thing (Product) with Products, chars, and doubles. Couldn't you standardize that rather than worry about all the combinations? Commented May 30, 2018 at 0:55

1 Answer 1

1

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); 
    ......
}
Sign up to request clarification or add additional context in comments.

1 Comment

@ChiefTwoPencils My bad, copy paste error. Code adjusted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.