Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC: How many repositories?

I am in the process is designing a website in ASP.NET MVC and am perhaps a little confused as to the exact nature of a repository.

Following the NerdDinner example, my site should have one repository which serves up the entities as I need them. However, I have also heard that you should have different repositorys that deal with specific sets of related entities....?

In the case of my site, there will be a number of entities (around 15 tables) yet the majority are all related. Is it ok / advisable to have one repository that contains all the methods that I'll need for pulling / updating / deleting etc or should I split them down?

like image 595
Sergio Avatar asked Sep 24 '09 16:09

Sergio


3 Answers

I use a generic repository which is plenty for many entities.

For a more complex one, I simply extend this with what's needed. The best of both worlds really.

like image 145
dove Avatar answered Sep 21 '22 12:09

dove


In domain driven design, there's a rule that repositories are per aggregate root. You can read more about it here.

The more I read, the more I think that NerdDinner is too often seen as a collection of good practices, while it's absolutely not (see here for a discussion of, particularly, NerdDinner repository). That's why people often blame other MS examples like Oxite (and here:

Developers will flock to it, praise it, and blindly accept it as gospel because it comes from Microsoft (it's already well on its way). Sadly, any developer which adopts its spirit will be left with an unmaintainble, untestable and unreadable mess

).

like image 20
queen3 Avatar answered Sep 22 '22 12:09

queen3


If you use a generic repository which accepts types then I don't see any reason to use more than one.

we use an interface like this:

public interface IRepository
{
    void Save<ENTITY>(ENTITY entity)
        where ENTITY : Entity;

    void Delete<ENTITY>(ENTITY entity)
        where ENTITY : Entity;

    ENTITY Load<ENTITY>(int id)
        where ENTITY : Entity;

    IQueryable<ENTITY> Query<ENTITY>()
        where ENTITY : Entity;

    IList<ENTITY> GetAll<ENTITY>()
        where ENTITY : Entity;

    IQueryable<ENTITY> Query<ENTITY>(IDomainQuery<ENTITY> whereQuery)
        where ENTITY : Entity;

    ENTITY Get<ENTITY>(int id) where ENTITY : Entity;

    IList<ENTITY> GetObjectsForIds<ENTITY>(string ids) where ENTITY : Entity;

    void Flush();
}

then use in code like this:

var returnedObjects =  repository.GetAll<ObjectClass>();
var singleObject =  repository.Get<ObjectClass>(id);
like image 21
Richard Avatar answered Sep 22 '22 12:09

Richard