在进行列表排序时,有个“上移”和“下移”操作,这个一般在内存里完成,然后统一提交到数据库中,对于上移与下移的设计,大叔在LIND.DDD.DOMAIN里有一个ISortBehavor接口,主要是说,如果实体对象支持排序功能,可以实现这个接口,而在扩展库中,将有为本地结果集动态排序(上移和下移)的方法,这个设计类似于ABP项目里的软删除,当然在大叔LIND里也有对删除的逻辑操作。

ISortBehavor内容

class Entity { public int ID{ get; set; } }

interface ISortBehavor { int SortNumber{ get; set; } }

而上移与下移我们使用一个泛型方法来接收参数,这样扩展性更好一些,开发人员只要把需要上下移的集合传到方法里,之后你的集合就可以被排序,当然这里面会进行各个元素的移动操作!

  /// <summary>
/// 算法帮助类
/// 作者:仓储大叔
/// </summary>
public class AlgorithmsHelper
{
/// <summary>
/// 排序,上移和下移
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <param name="id"></param>
/// <param name="newSort"></param>
public static void Sortable_Up_Down<T>(List<T> list, int id, int newSort) where T : class
{ var old = list.Find(i => (i as Entity).Id == id);
if (old == null || (old as ISortBehavor).SortNumber == newSort)
return; if ((old as ISortBehavor).SortNumber > newSort)
{
(old as ISortBehavor).SortNumber = newSort; foreach (ISortBehavor item in list.FindAll(i => (i as Entity).Id != id && (i as ISortBehavor).SortNumber >= newSort))
{ item.SortNumber += ;
} }
else
{
(old as ISortBehavor).SortNumber = newSort; foreach (ISortBehavor item in list.FindAll(i => (i as Entity).Id != id && (i as ISortBehavor).SortNumber <= newSort))
{ item.SortNumber -= ;
}
}
list.ForEach(i =>
{
Console.WriteLine((i as Entity).Id + "sort:" + (i as ISortBehavor).SortNumber);
}
);
}
}

我们测试一下程序,看一下神奇的结果

  class MainClass
{
public static void Main (string[] args)
{
List<Person> list = new List<Person> ();
list.Add (new Person {ID=,Name="zzl",SortNumber=});
list.Add (new Person {ID=,Name="zzl2",SortNumber=});
list.Add (new Person {ID=,Name="zzl3",SortNumber=}); Console.WriteLine ("Hello World!");
Extenstion.Sortable (list, , );
Extenstion.Sortable (list, , ); }
}
单元测试里的结果如图
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMYAAACWCAIAAAAKd2D9AAAKL0lEQVR4nO2d74sbxxnH85cMBO4v6KHQvEleBHKwCA6/CHkTcjgv8iLEuSyKhcm7gOiLvDqORTlfSkvTc1400PMhVF0owcEUmsY5jNiGc5tCjbDbgkOSQ/aUvEjdFyONRvNrZ3XP7Op83w+HWa3mx67242dmd2akpzgApDxV9wGAJw0oBYiBUoAYKAWIgVKAGCgFiIFSgBgoBYiBUoAYKAWIgVKAGCgFiIFSgBgoBYiBUoAYKAWIgVKAGCgFiKFRiilY303T1HzXld6aJiQxWAZClWKM9Xo9Nk+WZfJdz4UXSlnLdFliOgqlzgolopSpUZZlqmpmYpPwBHxeI39Keo4HK0pdl7b3FyumfWGF/Wzt6O6Yc75zZZ0xtn/7pDDXYLctcwUlni+2m2VBOeNQruGTGqn/WpXSciVJou2xBjOXUlYdSx15WQYfd4/ujvPDnc3tfV7yIg1227MLfDxYmV7v9oUVj5pzuThvX2wGKsU537myLvIKvdY2t54QpVzxQ1PKamFhDJPlWFPK2EmLVKoU7QsrqlLvpOn2wZHY8JQ2l2tRpUSNzTOhlPTAH6W0DfmuFqW0Ml1Rx7THWk48dKWmraEMA/nhztPiKEU7pTaXa5dOOOfHgyzL0u39/HAny7JJaVo5Zi7O2xeb6cXmrGSlLkvtasN3VpTiSjwIUYrP6+VSyiUTN6SUanqykKMp1U7Tk+n+7YMjzvnOL94R11tttrSYkX10sP7KZpZlt6elmeXouThvX1jZElWI/ffz9Vc2xduD3fbWwRG/n68nieylnUmleEBfKlAp5kVNI58+qNGuMqvmlLJ22O/n6z9/mjG2NTWDm0odHO1cWV/b3BqK0hwdf12pacMn9mtH0tzcGivqnGulVD9MLdQsIo14+rAUSilxQiK77TtX1re0eHM86N0+EUoNdtuXtvcnpdnK0XMZSvHjwcq0TRSlqeVovbczo5R6vbXusEcpV4PlV0q+VJ+RWpUS21G659NwMotASoARl1A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