Back to directory
If You encapsulate the method for obtaining data, you will always find some disadvantages. After thinking about it, it should be sorting, however, after reading Microsoft's orchard project, I felt that its sorting encapsulation was not good and there were many column sorting problems. So I improved it myself, first, we propose an IOrderable interface unrelated to layering. It aims to decouple the WEB, BLL, and DATA layers, and then adds ThenAsc and ThenDesc to support multi-column sorting, in the orchard project, parameters are previously implemented. A maximum of three columns can be sorted, and the mixed sorting cannot be upgraded or downgraded.
View an IOrderable Interface
IOrderable<T> IOrderable<T> Asc<TKey>(::System.Linq.Expressions.Expression<Func<T, TKey>> IOrderable<T> ThenAsc<TKey>(Expression<Func<T, TKey>> IOrderable<T> Desc<TKey>(::System.Linq.Expressions.Expression<Func<T, TKey>> IOrderable<T> ThenDesc<TKey>(Expression<Func<T, TKey>> ::System.Linq.IQueryable<T> Queryable {
OK. The following is an implementation of the interface. I put this implementation on the Data layer because it is related to the ORM architecture, the sorting Implementation of your linq and ado.net may be different, so you cannot place it on the entity layer. My habit is that all of the special functions of linq are placed on the Data layer of the linq architecture (for example, IQueryable, which is unique to linq, and list, IEnumerable, and other extensions should be placed on the entity layer)
Orderable<T> : IOrderable<T> IQueryable<T> Orderable(IQueryable<T>= IQueryable<T> { IOrderable<T> Asc<TKey>(Expression<Func<T, TKey>>= (_queryable IOrderedQueryable<T> IOrderable<T> ThenAsc<TKey>(Expression<Func<T, TKey>>= (_queryable IOrderedQueryable<T> IOrderable<T> Desc<TKey>(Expression<Func<T, TKey>>= IOrderable<T> ThenDesc<TKey>(Expression<Func<T, TKey>>= (_queryable IOrderedQueryable<T>
This sorting method can be used by the BLL layer and assembled by itself. It can also be directly used in the GetModel () method provided by the Data layer, because my architecture already has the sorting function fitting to the GetModel method, check the Code:
IQueryable<TEntity> GetModel(Action<IOrderable<TEntity>> linq = Orderable<TEntity>
If it is called at other layers, you can instantiate the Action <IOrderable <TEntity> object by yourself, such as the Code:
backgroundEntities1 db = <WebManageUsers> user = DbContextRepository<WebManageUsers><IOrderable<WebManageUsers>> orderBy = query => query.Asc(j =>=>=> + k.DepartmentID + +
The above code is to first sort the code in ascending order, and then sort it in descending order. Let's take a look at the result.
Back to directory