Path: redb/redb.Templates/templates/route-xml/redb-route-beans/Beans/PriceCalculator.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Templates/templates/route-xml/redb-route-beans/Beans/PriceCalculator.cs
----
using System.Globalization;
using redb.Route.Abstractions;
namespace RouteModule.Beans;
///
/// A bean of the module: logic that does not belong in markup. context.xml builds one instance
/// from configuration (constructor argument and property); the routes call its methods through the
/// <bean ref="#pricing" method="..."/> step, the bean:#pricing?method=... URI and the
/// bean:pricing.Method(...) expression function.
///
public sealed class PriceCalculator
{
/// Creates the calculator with the VAT rate, for example 0.20.
public PriceCalculator(decimal vatRate)
{
if (vatRate < 0)
throw new ArgumentOutOfRangeException(nameof(vatRate), "The VAT rate cannot be negative.");
VatRate = vatRate;
}
/// The VAT rate the gross prices include.
public decimal VatRate { get; }
/// The currency written on every order; set from configuration.
public string Currency { get; set; } = "EUR";
///
/// The gross amount of an order line. Called with method="Gross(${header.qty}, ${header.price})":
/// each argument is an expression, converted to the parameter type; the result becomes the body.
///
public decimal Gross(int qty, decimal price)
{
if (qty <= 0)
throw new ArgumentOutOfRangeException(nameof(qty), "The quantity must be positive.");
return Math.Round(qty * price * (1 + VatRate), 2, MidpointRounding.AwayFromZero);
}
/// The quantity from which an order line counts as bulk.
public int BulkQuantity { get; set; } = 3;
///
/// Whether an order line is a bulk line. Called in a condition as bean:pricing.IsBulk(header.qty):
/// a method the expression language calls must be synchronous, a Task is refused there.
///
public bool IsBulk(int qty) => qty >= BulkQuantity;
///
/// Writes the currency and the VAT rate into the headers. Called as method=Stamp: a method
/// that takes the exchange reads and changes it directly; a void method leaves the body alone.
///
public void Stamp(IExchange exchange)
{
exchange.In.Headers["currency"] = Currency;
exchange.In.Headers["vat"] = VatRate.ToString(CultureInfo.InvariantCulture);
}
}