liveBook · Manning

Entity selector pattern

As explained in Effective Behavior-Driven Development

A pattern that uses business-readable phrases to invoke automation code applying domain logic to select an appropriate entity.

An entity selector lets a business-readable phrase, such as “the earliest order,” invoke domain logic that chooses the intended entity. It keeps scenarios focused on what matters while automation resolves the phrase against available data. section 13.4 section 13.0

A selector connects scenario language to reusable automation:

  • Write the phrase that expresses the choice the scenario needs, rather than spelling out incidental details such as an order’s timestamp. section 13.4
  • Resolve the phrase by querying scenario context or another data source, then pass the selected entity to the step. section 13.4
Listing 13.12 Scenario with multiple orders
*** Features/OrderProcessing.feature ***
Scenario: Start work on orders in order of arrival
  Given the following orders have been placed
    | placed at |
    | 13:33:47  |
    | 13:45:30  |
  When a kitchen staff member asks for an order to work on
  Then the earliest order received should be taken

Compare the scenario’s explicit timestamp with “the earliest order received,” which expresses the selection in domain language. section 13.4

Listing 13.13 Extend parameter type to handle earliest order
*** Support/CustomParameterTypes.cs ***
[Binding]
public class CustomParameterTypes(OrderingContext orderingContext)
{
  [StepArgumentTransformation("the order|the placed order|the new order",
      Name = "order")]                                 #A
  public int ConvertOrder()
  {
    return orderingContext.ChosenOrderNo ??           
      throw new InvalidOperationException("No current order");
  } 

  [StepArgumentTransformation(
      "the earliest order received", Name = "order")]  #B
  public int ConvertEarliestOrder()
  {
    return orderingContext.PlacedOrders
      .OrderBy(o => o.PlacingTime)
      .FirstOrDefault()?.OrderNo ?? 
      throw new InvalidOperationException("No orders available");
  }
}

The order parameter type matches the selector phrase to a transformation that sorts placed orders by time and returns the first order number. section 13.4

How Effective Behavior-Driven Development covers it

How deep it goes
Defines the pattern and works through an order-selection scenario, including custom parameter transformations and selection logic.
Examples
Replaces an order timestamp with “the earliest order received” and selects the first order by placing time.
What you can do after
Apply it by extending an order parameter transformation to resolve “the earliest order received” from stored orders.
How it connects
Extends Introduce and Refer with custom parameter types that query scenario context or other data sources.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Effective Behavior-Driven Development. The section links point to the parts of the book it draws on.