4

This is with respect to my solution of implementing Paging & Sorting in Domain Driven Design with intention of not to pollute the Domain models and repository contracts,

Base class for REST Request

    public class AbstractQueryRequest {
        ...

        private int startIndex;
        private int offset;

        ...
    }

Interceptor to retrieve the Query Meta data and store it in 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 Query Meta data

    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 retrieve this ThreadLocal value in the repository, if available use it with the queries to the datastore.

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

Somasundaram Sekar
  • 5,244
  • 6
  • 43
  • 85
  • Possible duplicate of [Paging in NHibernate](http://stackoverflow.com/questions/1605368/paging-in-nhibernate) – theDmi Jan 20 '16 at 07:19
  • 1
    I sincerely hope it may not be so., my question was more related to conceptual aspect of DDD to not pollute domain model with infra(UI) concern of performance. and wanted to validate my solution to this problem. – Somasundaram Sekar Jan 20 '16 at 07:32

1 Answers1

4

don't use your solution. It's quite complex and will be hard to debug and maintain. If you start using async methods it won't even work.

I don't understand why you can't have sorting/paging properties in your repository methods? If you want a cleaner solution you might create a new object which carries the sort and paging settings.

When you want to fetch data you typically do it through your application services. And those can use your repositories directly before converting the result to DTOs.

then you got something like:

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);
    }
}

you can read more about sorting and paging in my article here: http://blog.gauffin.org/2013/01/11/repository-pattern-done-right/

Shade
  • 9,936
  • 5
  • 60
  • 85
jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • That was some valuable information. Thank you, I'm running into another problem in this same regard, one of the domain concept is to search a catalog based on given criteria and return the list of result(paginated ofcourse). Here I'm thinking of implementing the Search as a domain service(Am I wrong here?). Doing so requires that I pass the Pagination through domain service to the underlying repository. Found an sample application that does load data from repo from within Domain service ... Continued below – Somasundaram Sekar Jan 29 '16 at 07:47
  • ..Continued `Product suggestEquivalent(Product problematicProduct, Client client) { List expiringProducts = productRepository.findProductWhereBestBeforeExpiredIn(5);` How do I handle pagination here?, provided we shoouldn't be passing our pagination concern into the Domain models – Somasundaram Sekar Jan 29 '16 at 07:48