views:

397

answers:

3

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.

A: 

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

IGroupRepository : ITypedRepository<Group>
{
    bool AddContactToGroup(Group g, Contact C);
}
n8wrl
Thanks! That I have already, but it's the inheritance chain that is not working out for me when I try this.
Tomas Lycken
A: 

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();
    }
}
Thomas Levesque
That is very good, indeed! But it doesn't really solve my problem: if I do it this way, don't have anything in the interface IRepository that makes sure this method exists (unless I define every single named method, but that's what I wanted to avoid in the first place...). That means I can't decouple the Controller from the Repository via the interface, and thus I won't be able to test it in any easy way...
Tomas Lycken
Why do you need to make sure it exists ? It only needs to exist if you're going to call it explicitly. Anyway, the Get method always exists as an explicit implementation of IRepository<T>. In the controller, you will declare the repository as a IContactsRepository or IGroupRepository, not as a IRepository, so there will be no ambiguity.
Thomas Levesque
A: 

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.

Serkan