Categories
Home
About Collection Agency
Collection Agency Classification
Nationwide Collection Agency
Site Map
 
 
   
view collection sorting in model layer ... good or bad?

Question:
view collection sorting in model layer ... good or bad? I've been spending way too much time trying to sort a collection of results from a datastore query. Today, I realized this might be the responsibility of the view layer. Currently I have implemented the sort in the compareTo method of my business object, as it implements the Comparable interface. I have seen javascript libraries that provide slick interfaces for sorting and filtering of collections. I suspect this might also be managed by jstl or java scriptlets in my JSP, though I don't know. I try to avoid java scriptlets in the JSP to keep it clean. The sorting logic is a bit complicated. My users want a weighted resultset. They have identified six cases, each with a unique weight. I want to keep the business logic in the model layer but the sorting is purely presentational. My rookie question is this ... what is the optimal solution and why?


Answer:
Here are my thoughts on the matter: I'm more in the domain driven philosophy boat. If your domain says that the collection needs to be sorted, then in the model class what I do is model it so that when the collection is retrieved within the model it is going to be returned with the proper sort order, call this "default sort order". When the need arises for a different sort order, the decision is made to either make this the new sort order, or to extract out the sorting to make it more flexible via a strategy or similar design pattern. If sorting has nothing to do with the model, then I simply return it as is. This then pushes the responsibility of sort order to the view layer. That way, if you do ever have different views hitting the same model, you won't be forcing the requirement of sorting upon each of them (though if each of them do have the requirement maybe this is a hint that it should be part of the model?) Another valid option for setting a "default" sort order is to let the database do the sorting when you first get the data. This isn't always available though if you aren't populating from a database. As you can see, there are lots of different ways to go about this. I wouldn't say that any specific way is the "right" way.



Submit your comment or answer


 

Home About Collection Agency Collection Agency Classification Nationwide Collection Agency Site Map


Privacy Policy