Skip to content

Reduce verbosity of object initializer syntax #1435

@tuespetre

Description

@tuespetre

First let me say that I am very pleased that you offer both fluent and object initialization syntaxes. There are times where I will use one or the other.

That being said, I feel that there can be some excessive verbosity when writing a complex query using the object initializer syntax. The following example illustrates all three of the points I have noticed so far:

var aggregations = new Dictionary<string, IAggregationContainer>
{
    {
        "my_aggregation", new AggregationContainer
        {
            Filter = new FilterAggregator
            {
                Filter = (FilterContainer)new QueryFilter
                {
                    Query = (QueryContainer)new QueryStringQuery
                    {
                       Query = "Just an example"
                    }
                }
            }
        }
    }
};
  • PlainQuery will implicitly cast to QueryContainer but not IQueryContainer
  • PlainFilter will implicitly cast to FilterContainer but not IFilterContainer
  • Aggregators must be wrapped in an AggregationContainer even if you are only specifying that aggregator without any subaggregations or other options; there is no cast from a single aggregator to a container like there is for queries and filters, let alone a cast to IAggregationContainer

If such casting was offered by the library, the above snippet could look like this:

var aggregations = new Dictionary<string, IAggregationContainer>
{
    {
        "my_aggregation", new FilterAggregator
        {
            Filter = new QueryFilter
            {
                Query = new QueryStringQuery
                {
                    Query = "Just an example"
                }
            }
        }
    }
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions