mercredi 19 août 2015

Dictionary in Attribute workaround

I have a REST Web Api endpoint, which can receive query parameters.

Some of these query parameters are used to generate a LINQ expression with the inner model's properties. For instance:

http://ift.tt/1HWF13r

The query parameter episode generates a LINQ expression:

s => s.Episode == 32

Which property to use for the comparison is specified in an attribute that each query parameter has:

public class SceneQueryData
{
     [PropertyFilter("Episode")]
     public int? Episode { get; set; }
}

I am saying: "Hey, use this "episode" query parameter as a filter for the "Episode" property of the model".

Now, the generated expression is a simple equality comparison (=), so I'm in need of more complex operations (<, <=, >, >=), for that I can set an additional member in the attribute:

[PropertyFilter("Episode", QueryOperations = new Dictionary<string, QueryOperation>()
{
    { "le", QueryOperation.LessThanOrEquals },
    { "lt", QueryOperation.LessThan },
    { "ge", QueryOperation.GreaterThanOrEquals },
    { "gt", QueryOperation.GreaterThan }
}]

This will allow me to make queries such as scenes?episode.le=20, which would be translated to

e => e.Episode <= 20.

However, I cannot pass in a dictionary as an attribute argument, so I need another way to accomplish this using attributes.





Aucun commentaire:

Enregistrer un commentaire