NHibernate + ASP.Net MVC - 如何根据用户选择的字段以强类型人工方式对数据进行排序
我正在通过远程排序和分页在网格(ExtJS)中为用户呈现数据。 假设我有一个包含一些订单的网格。 订单实体类似于 Order{OrderNumber, Customer, Date, Address{Street, City, PostCode}}。 客户被 NH 映射为关系,地址被映射为组件。 网格中显示的数据被展平为如下命名的列:OrderNumber、Customer.Number、Customer.Name、Date、Address.Street、Address.City、Address.PostCode。
用户选择他想要排序的列,网格将字段名称发送到服务器。 现在在服务器端,我需要向后恢复哪些实体属性属于网格字段名称,并确定它是否只是组件,或者是否是关系,并使用 CreateAlias + AddOrder 等构建条件。这个逻辑充满了如下代码:
if (gridField=="Customer.Name"){
cri = cri.createAlias("Customer", "customerAlias");
cri.AddOrder(Order.Asc("customerAlias.Name"));
}
这很简单,但是现在看起来肯定是这样的。 我正在寻找一些通用的更智能的解决方案。 有什么想法吗? 我现在面临的问题是,我可以有一个转换实体属性(包括嵌套组件和关系)的约定,但我需要有一种方法来确定字段是否像组件或关系一样映射。 这会是相当沉重的......
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....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
会很重。 我没有看到一个简单的解决方案,但是如果您计划多次重复使用它,或者需要非常强大的东西,您可以构建一个基于反射的系统。
另一种可能性是使用一些 T4 模板,但这只能解决“字符串”问题,而不能解决关联问题。
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.
怎么样:
How about: