I am using a generic repository interface which allows me to add, find, update and delete objects of different types. I have then implemented two concrete repositories and can switch them around without changing the application. Everything is wonderfully disconnected. But I have hit a snag. The code inside my repository methods just feels wrong and I cannot figure out how to do it better. Here is a snippet of my code:
public class OracleRepository<T> : IRepository<T> where T : new()
{
public IQueryable<T> GetAll()
{
if (typeof(T) == typeof(Object1))
{
return (IQueryable<T>)DataAccess.GetAllObject1().AsQueryable();
}
if (typeof(T) == typeof(Object2))
{
return (IQueryable<T>)DataAccess.GetAllObject2().AsQueryable();
}
throw new NotImplementedException();
}
}
The problem is that my DataAccess (creates objects from a datareader) is not generic and has specific methods for each type of object that it supports.
Can I rewrite the above so that I avoid the list of if-then-elseif using generics or otherwise?