Partitioning NHibernate IRepository to achieve better SRP level (hopefully!)
As you might know for some time now I’ve been working on my own NHibernate implementation. Largely it is influenced by Ayende’s great Rhino.Commons for NHibernate, but what I like to think is that it took a little bit different direction, and that the details of implementation are what it makes really different (not better or worse… just different).
With this in mind I am working on my own IRepository implementation and have found that it actually has at least 3 reponsibilities it must fullfill:
- It must be able to Save/Update/Delete/Query Entities
- It must be able to Query for DTO’s (via projections)
- It must be able to call Stored procedures
Now, with Ayende’s REALLY great implementation all this is a part of the main IRepository interface, what I’ve decided is that in my implementation they will be split:
- IEntityRepository
- IStoredProcedureRepository
- IProjectionRepository
This allows me to split the responsibilities more accurately over them. Some of the differences:
- IEntityRepository<EntityT> is full featured - it can both query and save/update/delete mapped entities
- IStoredProcedureRepository contains only methods to call stored procedures
- IProjectionRepository also only contains querying possibilities, it can query very similary to Entity repository, difference being that here you define the projections as well and thus tranform the result to a DTO or something else. Also, instead of defining the resulting type on the repository level (IRepository<T>), here you define it on a query level (.Find<T>….)
What do you think of a such idea?
I have to mention that the first two repositories are already made (Entity and Stored procedure), and are fully tested (spec’d out).
Ill describe how the current ones look in a series of upcoming posts, but they do contain some neat things.
Cheers!
