[C# NEST] Best way of accessing properties of IAggregate object

(Narcis Madern) #1

Hi all,

I'm trying to map the aggregations returned by ES through NEST into our own model, but I see that those aggregates within NEST are defined as being of type IAggregate.

Since different types of aggregates (for example BucketAggregate, MatrixAggregate, etc) have different properties (specifically, BucketAggregate has Items property, while the IAggregate interface does not have it), I coded an ugly conditional to check the actual type of the Aggregate, and mapping some properties or others depending on that type.
A similar thing happens with the Buckets themselves: different implementations of IBucket have different properties, so I need to check the type, and based on that, map some properties or other ones.

Something like the following:

 if (aggr.Value.GetType() == typeof(Nest.BucketAggregate))
 	foreach (IBucket bucket in ((Nest.BucketAggregate) aggr.Value).Items)
 		if (bucket.GetType() == typeof(KeyedBucket<object>))
 			object valueKey = ((KeyedBucket<object>) bucket).Key;

This indeed works, but it seems a really bad looking code to me.
Is there a more elegant way of achieving the same thing without having to put those type conditions one by one?




(Narcis Madern) #3

Although you probably had a laugh when replied that, the response was not very helpful, as you can imagine.

I'll try to be more specific with my question: do you know a more elegant (or better) way of achieving the same I posted in my initial comment?

Please, if you are not going to help but only to make a new joke, you better don't write anything.


(system) #4

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.