title: NHibernate - truly dynamic sorting
body: <p>Using NHibernate, I need to be able to configure my application to sort a specific collection of entities exactly as needed.  </p>
  
  <p>The configurable sort:</p>
  
  <ul>
  <li>can involve multiple properties</li>
  <li>can specify the sorted properties in any order</li>
  <li>can specify asc/desc on the sorted properties</li>
  <li>can sort by custom properties (i.e. there is no corresponding SQL/C# property - it is calculated)</li>
  </ul>
  
  <p>This functionality is inherited from an existing app where parts of the SQL are specified by an administrator and the SQL statement is built/executed dynamically.</p>
  
  <p>Every time I try thinking through a solution I start getting in muddy waters with all kinds of alarms going off in my head regarding maintainability, performance, scalability, security, etc..</p>
  
  <p>For example, I figure the admin can specify a comma delimited string like so:</p>
  
  <p>"Date asc, FirstName asc, LastName desc"</p>
  
  <p>I can split the string and go through a loop matching the property/sort pairings in a case statement and calling .AddOrder(Order.Asc("FirstName")) as necessary.  But then, how do I handle custom properties?  I could allow the user to specify SQL for calculating custom properties and then allow the user to sort on those like they would FirstName, but I'm seemingly back at dirty/kludge again.</p>
  
  <p>Is there a clean/appropriate way to handle this requirement?</p>