Skip to content

Querying with explicit property specification (without attributes) #4

@tflanitzer

Description

@tflanitzer

I would like to be able to (optionally) explicitly specify which properties are used for the queries instead of using the Sieve attribute. Ideally, this "specification" would also allow the definition of a mapping between a query term to an actual property on the model class.

My arguments supporting this request:

  1. Different APIs for the same underlying model may support filtering or sorting on different properties.
  2. By directly using names of properties in my model classes, the domain is coupled to the query endpoint. This e.g. means that it is not possible to rename a property on a model class without a breaking change in the API.
  3. I don't want to "pollute" my domain classes with attributes that are only relevant for querying them via a REST API.

Here is an example of how the changed call to SieveProcessor could look like:

` var result = _dbContext.Posts.AsNoTracking();

        var sieveProperties = new[]
        {
            SieveProperty<Post>.For(_ => _.Title, Allow.Filter, "name"), 
            SieveProperty<Post>.For(_ => _.CommentCount, Allow.SortAndFilter), 
            SieveProperty<Post>.For(_ => _.LikeCount, Allow.Sort), 
            SieveProperty<Post>.For(_ => _.DateCreated, Allow.SortAndFilter), 
        };

        result = _sieveProcessor.ApplyAll(sieveModel, result, sieveProperties);

        return Json(result.ToList());

I have implemented a draft of the necessary changes and will open a PR. Feedback is welcome!

BTW: Great work so far!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions