EF architecture ~ A truly encapsulated sorting method that supports multi-column sorting

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.