Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One big repository vs. many little ones?

I have several product tables in my database:

  • ProductTypes
  • ProductCategories
  • ProductCategoryItems
  • ProductInventory

The way I see it now, I can make IProduct which would have methods such as:

  • FindAllTypes()
  • FindAllCategories(int typeId)
  • FindAllItems(int categoryId)

Or, I can separate each to mimic the table structure: IProductType, IProductCategory, etc.

Is there a reason to go with one over another?

like image 383
Mike Avatar asked Jun 09 '11 21:06

Mike


2 Answers

The idea of repositories is to delegate each one with responsibility for a single entity. In this case making a repository for each entity is recommended. You can go for the big repository one as well, but is not the best solution. In the end you'll get a HUGE class with lots of methods and really tight coupled. Also difficult to give maintenance to.

like image 197
Luis Aguilar Avatar answered Nov 16 '22 02:11

Luis Aguilar


I don't think having a huge repository is really a good idea, then you'd basically have a data access god class that does everything.

I like to have a base Repository<T> which does common operations such as GetById and GetAll. I normally have all my repositories inherit this base class to get the common methods for free, so I don't have to keep rewriting the same code.

like image 45
Rex Morgan Avatar answered Nov 16 '22 04:11

Rex Morgan