Last post Sep 19, 2012 11:57 AM by abhisheks
Aug 04, 2012 06:34 AM|sundeep_38|LINK
I am using different technologies for querying data and CRUD operations. So, I am thinking of using repository pattern for the model to allow plug-in kind of architecture using dependecy injection i.e., controllers call interface rather calling repository
I have come up with two approaches, please suggest the better one or any other alternative for easy maintainability.
Create a base repository interface IRepository and creates a class inheriting this interface for each entity as below. I created only five methods in IRepository, 2 for select and 3 for CRUD operations. In future if there is any change in data access methodologies,
I can just implement this interface and plug-in new repositories. But I was wondering how to accomodate if there are any new methods to be added for a individual repository. This approach seems to be simple but not flexible enough to accomodate any changes
i.e, I don't see a way to include if there is any new method to be added to individual repositories. It needs to be added to IRepository and all repositories must be implementing even though its not required for them.
public interface IRepository
List<T> GetAll<T>() where T is IBaseEntity;
T Get<T>() where T is IBaseEntity;
public class UserRep: IRepository
public List<T> GetAll<T>() where T is IBaseEntity
//implement the method
Likewise, all repositories are inherited from IRepository.
From controller it will be kind of
List<User> objList = IRepository.GetAll<User>();
Created repository interface for each entity and implement it to get called from respective controller. This approach seems to be flexible to accomodate any new methods to be added to individual repositories. But for each controller, a interface must be
public interface IUserRep
List<User> GetAllUsers() ;
public class UserRep: IUserRep
public List<User> GetAllUsers() ;
Likewise, each object will have its interface and implementation class seperately.
List<User> objList = IUserRep.GetAll();
I hope I was able to explain what I intended to, any help with this is much appreciated.
Sep 17, 2012 04:27 AM|DMW|LINK
Have you considered doing
public interface IRepository<T> where T : IBaseEntity
// common queries
public interface IUserRepository : IRepository<User>
// any User specific queries
This should give you the best in maintainability and specificity for the different entity types.
Sep 19, 2012 11:57 AM|abhisheks|LINK
Expanding on earlier reply
When you talk 'bt CRUD methods, why would you need to add any other methods than the basic CRUD methods? Usually, insert, update and delete is straight forward. When it comes down to Read, you can read various ways. Rather than having GetUserById(id), GetUserByName(name),
you could have Search(criteria), could be pretty generic that the generic repository can interpret.