ReOrderable收集和QUOT;实行&QUOT最好的办法;并坚持到数据库 [英] Best way to implement "ReOrderable Collection" and Persist it to database

查看:123
本文介绍了ReOrderable收集和QUOT;实行&QUOT最好的办法;并坚持到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的域对象:

public class MyDomainObject
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public int DisplayOrder { get; set; }
}



假设样本数据:

Assuming sample data :

var list = new List<MyDomainObject>()
               {
                   new MyDomainObject {Name = "Element1", DisplayOrder = 0},
                   new MyDomainObject {Name = "Element2", DisplayOrder = 1},
                   new MyDomainObject {Name = "Element3", DisplayOrder = 2},
                   new MyDomainObject {Name = "Element4", DisplayOrder = 3},
               };

现在我改变元素3,从2比1。我的目录应该看起来像的DisplayOrder

Now i change the DisplayOrder of the "Element3" from 2 to 1. My list should looks like that :


  • 元素1(DisplayOrder = 0)

  • 元素3(DisplayOrder = 1)

  • 元素2(DisplayOrder = 2)

  • 元素4(DisplayOrder = 3)

  • Element1 (DisplayOrder = 0)
  • Element3 (DisplayOrder = 1)
  • Element2 (DisplayOrder = 2)
  • Element4 (DisplayOrder = 3)

现在我删除元素3


  • 元素1(DisplayOrder = 0)

  • 元素2( DisplayOrder = 1)

  • 元素4(DisplayOrder = 2)

那么什么是最好的方式坚持这一机制的数据库?

So what's the best way to persist this mechanism to database ?

基本上我需要一个ReOrderableCollection将从数据库的排序依据DisplayOrder填充那里收集索引匹配DisplayOrder,并持续通过分配从集合指数DisplayOrder回物品。

Basically i need a "ReOrderableCollection" which will be populated from database with an OrderBy "DisplayOrder" where Collection Index Match "DisplayOrder", and persist back items by assigning DisplayOrder from Collection Index.

推荐答案

从你的例子似乎你总是希望的顺序是没有差距,从零开始。但是,这意味着除去第一元件将需要更新在数据库中的行为在列表中的每一个项目。很简单,它会工作(这些都是好东西),但它并不总是理想。既然你问了最好的办法并没有真正指定你的意思是什么,不过我建议另一种方法:

From your examples it seems that you always want the sequence to be without gaps, starting from zero. But this means that removing the first element will require updating the row in the database for every single item in your list. It's simple and it will work (and these are good things) but it's not always ideal. Since you asked for "the best way" without really specifying what you mean by that, allow me to suggest an alternative method:

什么是真正有DisplayOrder重要的不是实际值,但它们的相对顺序。如果要改善与数据库的性能,可以考虑放宽的要求,即不应该有间隙,然后试图找出改变DisplayOrders的最小的数,以保证正确的顺序被存储,即使存在间隙在所得序列。如果这样做,然后添加,移除或移动一个项目将典型地仅需要在数据库中的更新的一行,与该偶尔其他物品将被移动到创建,其中必须另外两个之间插入一个项的间隙之处在于有连续DisplayOrders

What really matters with a DisplayOrder is not the actual values but their relative order. If you want to improve performance with the database, you could consider relaxing the requirement that there should be no gaps and then try to find the smallest number of changes to the DisplayOrders to ensure that the correct order is stored, even if gaps are present in the resulting sequence. If you do this then adding, removing or moving a single item will typically only require updating one row in the database, with the exception that occasionally other items will have to be moved to create a gap where an item must be inserted between two others that have consecutive DisplayOrders.

您也可以最小化的次数,一个间隙不可用DisplayOrder 100,200,300开始以后,允许例如用插入之间(或者使用真正的/浮动式,而不是一个整数)DisplayOrder 150

You can also minimize the number of times that a gap is not available by starting with DisplayOrder 100, 200, 300 and later allowing for example an insertion with DisplayOrder 150 in between (or perhaps use a real/float type instead of an integer).

本方法的另一个优点是如果使用一个数据库中的数据进行比较的工具来观察数据库的当前版本和旧版本之间的变化会更容易地看到已对显示顺序进行了哪些修改。你只能看到在项目的显示顺序实际上已被用户移动的改变,而不是一半的列表中的每个项目被删除时间而改变。如果使用增量备份策略,这也将减少备份的大小。

Another advantage of this method is if you use a database data comparison tool to observe changes between the current version of the database and older versions it will be easier to see what modifications have been made to the display order. You will only see changes in the display order of items that have actually been moved by the user, rather than half the list change each time an item is removed. It will also reduce the size of backups if you use an incremental backup strategy.

我说虽然这这些优势都没有超过大多数情况下,天真的方法显著优势。这取决于你的系统是否值得实施这个系统或者只是保持简单。如果有疑问,保持它的简单。对于小名单,一些修改,并在您不关心的更改历史记录,覆盖新DisplayOrders每次大概会就好了整个列表系统。

I'd say though that these advantages are not significant advantages over the naive method for most cases. It depends on your system whether it is worth implementing this system or just keeping it simple. If in doubt, keep it simple. For systems with small lists, few modifications and where you don't care about the change history, overwriting the entire list with new DisplayOrders each time will probably be just fine.

这篇关于ReOrderable收集和QUOT;实行&QUOT最好的办法;并坚持到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆