Class TimeSpanBuilder<TOtherBuilder>
- Namespace
- CSF.Screenplay.Performables
- Assembly
- CSF.Screenplay.Abstractions.dll
A supplementary builder type which enables the collection of TimeSpan instances.
public class TimeSpanBuilder<TOtherBuilder> : IProvidesTimeSpan where TOtherBuilder : class
Type Parameters
TOtherBuilder
The builder type for which this builder will supplement
- Inheritance
-
TimeSpanBuilder<TOtherBuilder>
- Implements
- Inherited Members
Examples
The example below shows how the time span builder is intended to be used. It is consumed from within another builder,
which needs to include a developer-configurable time span.
See the documentation for writing performable builders for more information
about the makeup of the EatLunchPerformableBuilder
.
public class EatLunchPerformableBuilder : IGetsPerformable
{
IProvidesTimeSpan? timeSpanBuilder;
protected string? FoodName { get; init; }
IPerformable IGetsPerformable.GetPerformable()
=> new EatLunch(FoodName, timeSpanBuilder?.GetTimeSpan() ?? TimeSpan.Zero);
public TimeSpanBuilder<EatLunchPerformableBuilder> For(int howMany)
{
var builder = TimeSpanBuilder.Create(this, howMany);
timeSpanBuilder = builder;
return builder;
}
public static EatLunchPerformableBuilder Eat(string foodName) => new EatLunchPerformableBuilder() { FoodName = foodName };
}
The sample builder above would be used to build an instance of a (fictitious) EachLunch
performable, which derives from
IPerformable. The fictitious performable requires two parameters; the name of the food being eaten for lunch
and how long the lunch break lasts. The time span builder is used for that second parameter.
A consumer which uses this builder in an IPerformance, or another performable,
might consume it as follows.
using static EatLunchPerformableBuilder;
// ...
actor.PerformAsync(Eat("Sandwiches").For(30).Minutes(), cancellationToken);
A note for developers with access to the source code for this library.
There is a small integration test which sets up and exercises the example above; it is named TimeSpanBuilderTests
.
Remarks
When consuming Performable objects it is recommended to use the builder pattern to create them. A commonly-used 'parameter' which may be specified in builders is 'an amount of time', IE a TimeSpan.
This builder is intended to supplement another builder, for the purpose of specifying an amount of time. The 'other' builder is passed as a constructor parameter to this builder, along with an absolute amount. The consumer should then execute one of the methods of this type, which selects the unit of time and thus determines the TimeSpan value. The method which determines the units then returns that other builder instance, allowing the building process to continue with that other builder.
Whilst it is possible to create instances of this type via its public constructor, it is often easier to create
instances using the static
TimeSpanBuilder class.
Constructors
TimeSpanBuilder(TOtherBuilder, int)
Initializes a new instance of TimeSpanBuilder<TOtherBuilder>.
public TimeSpanBuilder(TOtherBuilder otherBuilder, int value)
Parameters
otherBuilder
TOtherBuilderThe other builder which shall be supplemented by this
value
intThe absolute value of time, but without units
Exceptions
- ArgumentNullException
If
otherBuilder
is null.- ArgumentOutOfRangeException
If
value
is less than zero.
Methods
Days()
Configures the contained time span to be measured in days, then returns the contained builder.
public TOtherBuilder Days()
Returns
- TOtherBuilder
Hours()
Configures the contained time span to be measured in hours, then returns the contained builder.
public TOtherBuilder Hours()
Returns
- TOtherBuilder
Milliseconds()
Configures the contained time span to be measured in milliseconds, then returns the contained builder.
public TOtherBuilder Milliseconds()
Returns
- TOtherBuilder
Minutes()
Configures the contained time span to be measured in minutes, then returns the contained builder.
public TOtherBuilder Minutes()
Returns
- TOtherBuilder
Seconds()
Configures the contained time span to be measured in seconds, then returns the contained builder.
public TOtherBuilder Seconds()
Returns
- TOtherBuilder