A c# Generics question involving Controllers and Repositories
- by UpTheCreek
I have a base repository class which contains all the common repository methods (as generic):
public abstract class BaseRepository<T, IdType> : IBaseRepository<T, IdType>
My repositories from this base e.g.:
public class UserRepository : BaseRepository<User, int>, IUserRepository
I also have a base controller class containing common actions, and inherit from this in controllers. The repository is injected into this by DI. E.g.
public class UserController : BaseController<User>
{
private readonly IUserRepository userRepository;
public UserController (IUserRepository userRepository)
{
this.userRepository= userRepository;
}
My question is this: The base controller needs to be able to access the repository methods that are defined in the base repository. However I'm passing in via DI a different repository type for each controller (even though they all inherrit from the base repository). How can the base controller somehow access the repository that is passed in (regardless of what type it is), so that it can access the common base methods?