I'm trying to understand the Repository Pattern
, while developing an ASP.NET MVC application (using .NET 3.5, ASP.NET MVC 1.0 and Entity Framework). I've gotten far enough to get the dependency injection and all working with one Controller and one Entity type, but now I've gotten as far as to implementing support for a relation between different types, and I'm stuck.
In all examples I've seen, the repository interface is called something like IContactsRepository
, and contains (CRUD) methods that are concerned with Contact
items only. I want to implement grouping of Contacts
, so I have an entity type called Group
, and an IGroupRepository
interface for handling (CRUD) operations on groups.
- Where does a method belong that is concerned with more than one entity type (in this case for example the method
AddToGroup
, that adds aContact
to aGroup
)?
I made an attempt at a larger inheritance structure for repositories, where I created the following interfaces:
ITypedRepository<T>
{
IEnumerable<T> GetAll();
T Get(int id);
bool Add(T newObj);
bool Edit(T editedObj);
bool Delete(int id);
}
IContactsRepository : ITypedRepository<Contact> { }
IGroupsRepository : ITypedRepository<Group> {
bool AddToGroup(int contactId, int groupId);
}
IRepository : IContactsRepository, IGroupsRepository
I then tried to create a master repository that inherits IRepository
, as follows:
public class EntitiesRepository : IRepository
{
IEnumerable<Contact> IRepository<Contact>.Get()
{
throw new NotImplementedException();
}
IEnumerable<Group> IRepository<Group>.Get()
{
throw new NotImplementedException();
}
// Etc. All methods were generated by hitting [Ctrl]+[.] with the cursor on
// the interface inheritance reference to IRepository and selecting
// "Explicitly implement IRepository"
}
As soon as I try to call one of the methods in the Repository from my Controller with this code
var contacts = _repository.Get();
I get a build error message about ambiguity between Get<Contact>()
that was inherited via IContactsRepository
and Get<Group>()
that came via IGroupsRepository
. I have understood that this is not allowed because the IRepository
inherits the same generic interface with different types (see example 5 in the linked article).
Now, since I inherit via other interfaces, is there any chance I could "override the names" of these methods, for example like below?
IContactsRepository : ITypedRepository<Contact> { IEnumerable<Contact> GetContacts = ITypedRepository<Contact>.Get(); ... }
That way, I can access it from IRepository.Getcontacts
without any ambiguity. Is it possible, or is there any workaround to this problem?
And a new question, for clarification:
Is there anyway to specify in the call from the controller which of the
Get()
methods I want?What is the best way to tackle my initial problem - the need of a repository that handles a lot of things, instead of just one entity type?
EDIT: Added code example of Repository
class and call from Controller
.