NHibernate + ASP.Net MVC - how to order data in strongly typed manned according to user selected field

StackOverflow https://stackoverflow.com/questions/1064118

Question

I'm presenting data for users in a grid (ExtJS) with remote sorting and paging. Let's say I have a grid with some Orders. Order entity looks like Order{OrderNumber, Customer, Date, Address{Street, City, PostCode}}. Customer is mapped by NH as relation, Address is mapped as component. Data presented in the grid are flattened to columns named like this: OrderNumber, Customer.Number, Customer.Name, Date, Address.Street, Address.City, Address.PostCode.

User selects a column which he'd like to sort by and the grid sends the field name to server. Now on server side I need to restore backwards what entity property belongs to grid field name and decide if it's just component or if it's relation and build Criteria with CreateAlias + AddOrder etc. This logic is full of code like:

if (gridField=="Customer.Name"){
  cri = cri.createAlias("Customer", "customerAlias");
  cri.AddOrder(Order.Asc("customerAlias.Name"));
}

This is much simplified, but it neccesarily looks like this at the moment. I'm looking for some generic smarter solution. Any thoughts? The problem I'm facing now is that I can have a convention for transforming entity properties (including nested components and relations), but than I need to have a method how to determine if the field is mapped like component or relation. This would be quite heavy....

Was it helpful?

Solution

It would be quite heavy. I don't see a simple solution, but if you are planning on re-using this a lot, or need something very robust, you could build a system based on reflection.

Another possibility would be to use some T4 templates, but that would only help the 'string' issue, not the association issue.

OTHER TIPS

How about:

cri = cri.CreateAlias( "Customer", "CustomerAlias" );
string sortProperty = gridField.Replace("Customer.", "CustomerAlias.");
cri.AddOrder( Order.Asc(sortProperty) );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top