A c# Generics question involving Controllers and Repositories

Posted by UpTheCreek on Stack Overflow See other posts from Stack Overflow or by UpTheCreek
Published on 2010-05-28T08:02:36Z Indexed on 2010/05/28 8:22 UTC
Read the original article Hit count: 244

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about asp.net-mvc