Skip to content

Command and Query Responsibility Segregation pattern #3

Description

@dotnetjunkie

Migrated from: https://solidservices.codeplex.com/discussions/574373

LauriSaar wrote:

Hi,

There is one great blog post about Command-query separation which is located here.

I've played with this architecture for some time and one question pops up: how would you handle sorting through IQuery<TResult>?

Let’s say that I have MVC webapp that needs server-side sorting and I want that sorting and paging happens in DB.

Behind these interfaces it is matter of type parameters if single entity’s property must be sortable at DB level. But as soon as I want to sort by more than one property it might get somewhat unintuitive. And I haven’t figured out simple way to pass ordering of multi sorting (not sort direction).

At first I passed lambda of Expression<Func<TEntity,TKey>> together with sort direction, used separate class for that. Overall it got quite verbose because all of those type parameters, but it worked fine, Linq2Entity’s IQueryable<>.OrderBy() was happy.

But as soon as I moved to multi sorting I found out that I cannot use IEnumerable< Expression<Func<TEntity,TKey>>> because TKey can vary with different properties. Tried with Expression<Func<TEntity,object>> and dynamic -- Linq2Entity’s IQueryable<>.OrderBy() was unhappy.

Next thing that worked was “public SortDirection? SortAmount {get;set}”-like properties on query implementation. SortDirection is enum with 2 constants. Handler has “if” statements to check for which properties to call .OrderBy().

But this solution’s drawback is that I haven’t found intuitive way to pass ordering of .OrderBy() calls.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions