views:

194

answers:

2

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....

A: 

How about:

cri = cri.CreateAlias( "Customer", "CustomerAlias" );
string sortProperty = gridField.Replace("Customer.", "CustomerAlias.");
cri.AddOrder( Order.Asc(sortProperty) );
John Rayner
this is all still about magic strings - I'm thinking about something more systematic...
Buthrakaur
at least you don't need to code in the actual field name used in the sort <shrug/>
John Rayner
+1  A: 

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.

Mufasa