Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate + Paging + Ordering

I'm not quite sure of the most elegant solution for what I am trying to do.

I have a page which lists music listings, there can be thousands of these so they are paged in batches of 20, I also have 4 links at the top of the page to change the way these listings are ordered.

The sort by properties could be located on different entities such as Audio.AudioStats.NumComments or Audio.Artist.NumProfileViews.

I am using the repository pattern, and a service layer. My controllers can only access the service layer, then the service layer accesses my repositories.

I can do the paging fairly easily, i simply pass in my current page, and the page size to my data layer...but how would i safely let the user decide on the ordering of my entities.

I am using S#arp Architecture 1.5.2 if that makes any difference.

Thank you in advance.

like image 683
Paul Hinett Avatar asked Nov 14 '22 10:11

Paul Hinett


1 Answers

You are going to have to map the users' desires to an order by clause somehow.

Presumably you're doing something like skip(n).take(m) which will need an orderby() clause too.

Given that you have a fixed set of (known) possibilities, you can map those to an enum or similar which you then translate to the relevant orderby() call.

This means you don't expose the properties at the UI layer but only pass through the intent to the repository layer (as a Sortby.ArtistProfileViews value or whatever). What how that intent is mapped to the properties on you domain objects is isolated in your repository layer.

like image 91
nickd Avatar answered Dec 03 '22 16:12

nickd