Skip to content

RFC: Enhancements to ResourceDefintion #402

Closed
@jaredcnance

Description

@jaredcnance

Description

The ResourceDefinition class exposes developer friendly hooks into how their resources are exposed. It is intended to improve the experience and reduce boilerplate for commonly required features.

As with the prior implementation, the ResourceDefinition will be resolved from the container and can accept any dependencies that have been registered.

Custom Filters

  • Introduce QueryFilters type which is just an "alias" to Dictionary in an attempt to improve DX
public class QueryFilters
  : Dictionary<string, Func<IQueryable<TEntity>, IQueryable<TEntity>>
{ }
  • The repository becomes responsible for loading the QueryFilters and applying them
  • Any keys present in the QueryFilters will not be applied using the default behavior

Example

public class FooResource : ResourceDefinition<Foo>
{
  protected override QueryFilters GetQueryFilters() => new QueryFilters { 
            { "key1", (query, value) => query.Select(x => x) },
            { "key2", (query, value) => query.Select(x => x) },
        };
}
 

Should be used to handle requests like:

/foos?filter[key1]=value1

Default Sort

Allow a resource to have a default sort if no sort key is provided.

  • Introduce PropertySortOrder type which is just an "alias" to ReadOnlyList in an attempt to improve DX
public class PropertySortOrder<T>
  : ReadOnlyList<(Expression<Func<T, dynamic>>, SortDirection)>
{ }
  • The repository becomes responsible for applying the default sort if no other sort has been requested

Example

public class FooResource : ResourceDefinition<Foo>
{
  public override PropertySortOrder<Foo> DefaultSortOrder = new { 
     (foo => foo.Bar, SortDirection.Ascending),
     (foo => foo.Baz, SortDirection.Descending),
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    RFCRequest for comments. These issues have major impact on the direction of the library.enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions