dimanche 23 octobre 2016

Appy dynamic ordering using different names than table-columns

I'm trying to build some API-endpoints applying the REST-principles as good as possible. I would like to offer my clients the ability to order and paginate the results. Because of performance, I want to pass this directly to the database-query so I don't need to fetch more than actually needed/requested.

Problem is that my resource-properties are not named the same as my database-fields/columns, so ordering gives me some headaches.

My resource has properties like EventName, Start and End whilst the underlying database-columns are named EventName, EventStart and EventEnd. The clients do not have any knowledge of the "real" names and should not.

I want them to pass the ordering like GET /Events?order=-eventname,start,end to order by name desc, start and end.

How can I now convert the given names (properties of the resource) to my names as they are used by database?

I thought something like attributes would do the job. Unfortunately, C# does not yet support generic attributes as I would need something that would return me an Expression<Func<TableName, TKey>> which can be passed into LINQs OrderBy.

I would then decorate my resource-property (name is known by the clients) with this attribute to get the correct orderby-expression to pass in before the query is executed against the database.





Aucun commentaire:

Enregistrer un commentaire