Implementing paging and sorting using domain-driven development

This refers to my decision to implement paging and sorting in a Driven Driven Design project with the intention of not polluting domain models and storage contracts ,

Base class for a REST request

    public class AbstractQueryRequest {
        ...

        private int startIndex;
        private int offset;

        ...
    }

Interceptor to retrieve request metadata and store it in a ThreadLocal container

    public class QueryInterceptor extends HandlerInterceptorAdapter {




        @Override
        public boolean preHandle(HttpServletRequest request,
                                 HttpServletResponse response, Object handler) throws Exception {

                                    ...

                                    QueryMetaData.instance().setPageMetaData(/*Create and set the pageable*/);

                                 }

    }

Container for request metadata

    public class QueryMetaData {

        private static final ThreadLocal<QueryMetaData> instance = new ThreadLocal<>() {
                protected QueryMetaData initialValue() {
                    return new QueryMetaData();
                }
        };

        public static QueryMetaData instance() {
            return instance.get();
        }

        private ThreadLocal<Pageable> pageMetadata = new ThreadLocal<>();

        public void setPageMetaData(Pageable pageable) {
            pageMetadata.set(pageable);
        }

        public Pageable getPageMetaData() {
            return pageMetadata.get();
        }

        //clear threadlocal


    }

I intend to get this ThreadLocal value in the repository if it is available with queries in the data warehouse.

I hope this may not be a very dirty solution, but want to know if there is a more widely used template for this.

+4
1

. . , .

, / ? , .

, . DTO.

- :

public class YourAppService
{
    public YourAppService(ISomeRepository repos)
    {
    }

    public IList<UserDto> ListUsers(string sortColumn = "", bool ascending = true, int pageNumber = 1, pageSize = 30)
    {
        var querySettings = new QuerySettings(sortcolumn, ascending, pageNumber, pageSize);
        var users = _repos.FindAll(querySettings);
        return ConvertToDto(users);
    }
}

:http://blog.gauffin.org/2013/01/11/repository-pattern-done-right/

+2

Source: https://habr.com/ru/post/1625069/


All Articles