Fork me on GitHub

Aggregators

The aggregator takes the results of the individual expressions defined by ByEvaluating and aggregates them into a single result that is passed back to the strategy and in case you use the default strategy back to the questioner.

Built-in Aggregators

ExpressionAggregator

When you use the expression aggregator, you define a seed and an aggregate function that are used to aggregate the expressions:

The result would be extremely super fantastic.

The expression aggregator exists for questions with and without a parameter.

AggregateWithExpressionAggregator is actually just a shortcut for AggregateWith(new Aggregators.ExpressionAggregator<TExpressionResult, TAnswer, TParameter>(seed, aggregateFunc)) to make the solution definition better readable.

SingleExpressionAggregator

The single expression aggregator works only if there is exactly one expression defined. Otherwise it will throw an exception. The single expression aggregator simply returns the result of the expression as the result of the aggregation.

Aggregators for validation

See Validation for a description of aggregators for validation.

Write your own Aggregator

To write your own aggregator, you have to implement the interface IAggregator:

If you don't have a parameter, then specify Missing as the generic type argument and ignore the method argument parameter:

The aggregator typically loops over all passed in expressions, evaluates them and combines the individual results into a single result. This result will then be passed back to the strategy.

Take a look at the aggregators in the source code to see samples.

Implement Extension Methods to simplify Syntax

When using Aggregators directly in the solution definition, the syntax can get really complicated because of the heavy use of generics. Either make your aggregators non-generic or write an extension method that takes care of the generics. For example the extension method for the expression aggregator looks like:

And because types can be infered automatically, the generic types don't have to be specified in the syntax: