I’m searching for hours and I’ve read several articles on constructing generic repositories (GR). As far as I’ve understood, GRs are used in cases when similar codes are present. For instance, to fetch a single row from table by its id, or the whole table. However, still, I cannot understand how to realize this.
public interface IEntity<T> where T : class{
IQueryable<T> getAll();
T GetById(int id);
}
public class Repository<T> where T : IEntity<T>{
northWindDataContext nwdc = new northWindDataContext();
public IQueryable<T> getAll()
{
//code to retrive the whole table
}
public T GetById(int id)
{
//code to retrieve a single row (don't know how to do)
}
}
Then, I want to do something similar:
Repository<User> rep = new Repository<User>();
IQueryable<User> = rep.getAll<User>;
User user = rep.GetById(35);
Please anyone can explain me how to accomplish this task?
This is just a pseudocode to create a strongly typed generic repository.
Then you can create a specific repository interface say
Don’t know if this is what you’re looking for. This is if you are using EF.