I have an IRepository interface with several methods.
public interface IRepository<T> where T : class
{
Task<IEnumerable<T>?> GetAllAsync();
Task<int> InsertAsync(T obj);
Task<int> UpdateAsync(T obj);
Task<int> DeleteAsync(T entity);
}
I want to add a method to retrieve data based on Id or key . The problem is that some data has more than one key, it could be two keys, three keys and so on, and the data type for each data can be different. Is there a solution in C# so that the parameters of a method can be flexible in terms of amount and data type?
Previously I tried using TKey on the method and overloading the method. I don’t think this is a good solution.
public T GetById<TKey>(TKey key);
public T GetById<TKey1, Tkey2>(TKey1 key1, TKey2 key2);
public T GetById<TKey1, TKey2, TKey3>(TKey1 key1, TKey2 key2, TKey3 key3);
Are there any other ideas or solutions? Thank you for your help.
1