Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic repository. Need suggestion

I'm thinking which way to go and can't decide which one is better. Maybe you can give me the killer idea :)

My problem: I have a generic repository built on top of NHibernate. It's interface is very simple, such as Get(object id), Save, Delete etc. Sometime this interface works fine, but sometime it doesn't. For example, several entities in my project has a property named Code (but not all). I want to be able to get an entity not only by primary key, but also with this property. Another situation is when I need to filter some entities with dates, or provide other filtering criteria which are specific to individual entities. Currently I'm thinking of the following solutions:

  • Linq - I can return an IQueryable object from Repository for each type, then filter the query using linq expressions. I don't like this idea, because if I go this way, I don't see the reason behind Repository.
  • Entity specific repository - Here I can build an entity specific on top of the generic repository. It will have methods such as Get(DateTime dateFrom, DateTime dateTo) etc. The drawback is that I'll have to create a repository class for each entity object. The good side is that it will be much easyer to persist/delete an object graph from database, because it knows entity's dependencies.
  • You tell me please :)
like image 442
Davita Avatar asked Feb 25 '23 23:02

Davita


1 Answers

I prefer entity-specific repositories, after putting a lot of effort into generic repositories.

This answer of mine (36 votes so far) elaborates on this stance and contains an approach you might be able to use:

Advantage of creating a generic repository vs. specific repository for each object?

like image 195
Bryan Watts Avatar answered Mar 02 '23 15:03

Bryan Watts