Implementing Paging and Sorting with Domain Driven Design

Somasundaram Sekar

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.

jgauffin

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/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Implementing Domain Driven Design Cost

From Dev

Implementing Domain Driven Design Cost

From Dev

Implementing Domain-Driven Design and Transactions

From Dev

Implementing Domain Driven Design Book Confusion

From Dev

Domain Driven Design for Rails App: Implementing a service in a basic example

From Dev

Layers in Domain Driven Design

From Dev

Domain Driven Design - Domain or Security

From Dev

Can you reference other aggregates in a factory when implementing domain driven design?

From Dev

Implementing multiple users within Identity and Access Bounded Context in Domain Driven Design

From Dev

Access Control in Domain Driven Design

From Dev

Domain Driven Design and batch processing

From Dev

Domain Driven Design Bounded Context Domain Objects

From Dev

Domain Driven Design: infrastructure concern or domain concern?

From Dev

Domain Driven Design: infrastructure concern or domain concern?

From Dev

Domain Driven Design. Entity type design

From Dev

Domain driven design repository implementation in infrastructure layer

From Dev

Domain Driven Design in Node.js Application

From Dev

Fetching associated aggregates in Domain driven Design

From Dev

Domain Driven Design - CQRS + ES usage

From Dev

Where is the call to persistence in the domain driven design

From Dev

Domain Driven Design (DDD) and database generated reports

From Dev

Domain-Driven-Design Entities and Value Objects

From Dev

Proper way to get aggregates in Domain Driven Design

From Dev

Domain Driven Design Auto Incremented Entity Key

From Dev

Fetching associated aggregates in Domain driven Design

From Dev

Where to find Domain Driven Design consultants?

From Dev

How to add scheduled jobs in domain driven design

From Dev

How to handle Domain Driven Design when domain is dynamic / changes

From Dev

Is Domain-Driven Design a right fit for a product in Enterprise Architecture Domain?

Related Related

  1. 1

    Implementing Domain Driven Design Cost

  2. 2

    Implementing Domain Driven Design Cost

  3. 3

    Implementing Domain-Driven Design and Transactions

  4. 4

    Implementing Domain Driven Design Book Confusion

  5. 5

    Domain Driven Design for Rails App: Implementing a service in a basic example

  6. 6

    Layers in Domain Driven Design

  7. 7

    Domain Driven Design - Domain or Security

  8. 8

    Can you reference other aggregates in a factory when implementing domain driven design?

  9. 9

    Implementing multiple users within Identity and Access Bounded Context in Domain Driven Design

  10. 10

    Access Control in Domain Driven Design

  11. 11

    Domain Driven Design and batch processing

  12. 12

    Domain Driven Design Bounded Context Domain Objects

  13. 13

    Domain Driven Design: infrastructure concern or domain concern?

  14. 14

    Domain Driven Design: infrastructure concern or domain concern?

  15. 15

    Domain Driven Design. Entity type design

  16. 16

    Domain driven design repository implementation in infrastructure layer

  17. 17

    Domain Driven Design in Node.js Application

  18. 18

    Fetching associated aggregates in Domain driven Design

  19. 19

    Domain Driven Design - CQRS + ES usage

  20. 20

    Where is the call to persistence in the domain driven design

  21. 21

    Domain Driven Design (DDD) and database generated reports

  22. 22

    Domain-Driven-Design Entities and Value Objects

  23. 23

    Proper way to get aggregates in Domain Driven Design

  24. 24

    Domain Driven Design Auto Incremented Entity Key

  25. 25

    Fetching associated aggregates in Domain driven Design

  26. 26

    Where to find Domain Driven Design consultants?

  27. 27

    How to add scheduled jobs in domain driven design

  28. 28

    How to handle Domain Driven Design when domain is dynamic / changes

  29. 29

    Is Domain-Driven Design a right fit for a product in Enterprise Architecture Domain?

HotTag

Archive