Question

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 a Contact to a Group)?

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.

Was it helpful?

Solution

I think you don't need all this complexity. When you add a contact to a group you are changing the group. I assume you have a collection of contacts on Group class. After adding a contact into a group, simply you need to save the group. So do you really need a AddContactToGroup method, just a Save method on Group repository interface would do the same job. Also you don't need a new method for every new relational property on Group.

For the implementation of the Save method itself, if you use NHibernate, only thing you need to do is to call relevant method.

OTHER TIPS

You will probably have specialized methods on your IGroupRepository in addition to the CRUD methods:

IGroupRepository : ITypedRepository<Group>
{
    bool AddContactToGroup(Group g, Contact C);
}

Add methods with non-ambiguous names that call the explicit implementations :

public class EntitiesRepository : IRepository
{
    IEnumerable<Contact> IRepository<Contact>.Get()
    {
        // Return something
    }
    IEnumerable<Group> IRepository<Group>.Get()
    {
        // Return something
    }

    public IEnumerable<Contact> GetContacts()
    {
        return (this as IRepository<Contact>).Get();
    }

    public IEnumerable<Group> GetGroups()
    {
        return (this as IRepository<Group>).Get();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top