# C# without the class hierarchy

> A pricing rule, a few records, and a thin HTTP handler. C# works well with functions and explicit data, without a service interface for every calculation.

- Author: Deyan Peev (https://deyanpeev.com)
- Published: 2022-01-09
- URL: https://deyanpeev.com/posts/csharp-without-class-hierarchy

A small rule can be surprisingly hard to find in a C# application.
The controller calls a service, the service delegates to a strategy, and
the strategy resolves a calculator through a factory. Eventually there's
a multiplication, and changing its rounding rule takes longer than
understanding the arithmetic should.

I don't think the cure is a ban on objects. I just want the number of
abstractions to follow the problem. Records, functions, and
[.NET 6 minimal APIs](https://deyanpeev.com/posts/dotnet-six-small-services) make that a comfortable
way to work in C#. It does not feel like a workaround against the language.

By “NoOOP” I mean less unnecessary object-oriented ceremony. There will still
be types and objects below. What I ask of an abstraction is whether it protects
a rule or a resource, or whether it just gives one line of code another address.

# A rule that fits in a function

Suppose a demonstration quote calculator applies a ten percent discount at
ten units. Prices are in one currency, EUR, and tax and delivery are outside
this example. It accepts a quantity and a hypothetical unit price, validates
them, and returns a rounded total.

It's a calculator, not a checkout API. In a real checkout the server would
load the authoritative price and wouldn't trust the client's. I am saying so
up front so we can focus on the rule, without pretending that multiplying
user-supplied numbers is a complete sales system.

Use SDK 6.0.101 and .NET 6.0.1, from the
[December 2021 servicing release](https://devblogs.microsoft.com/dotnet/december-2021-updates/).
Create `global.json` in an empty `Quotes` directory:

```json
{
  "sdk": {
    "version": "6.0.101",
    "rollForward": "disable"
  }
}
```

The complete `Quotes.csproj` is:

```xml
<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <LangVersion>10.0</LangVersion>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>
</Project>
```

Put the data and calculation in `Pricing.cs`:

```csharp
namespace Quotes;

public record QuoteRequest(int Quantity, decimal UnitPrice);
public record PricingPolicy(int DiscountFrom, decimal DiscountRate);
public record Quote(decimal Subtotal, decimal Discount, decimal Total, string Currency);
public record QuoteResult(Quote? Value, string? Error);

public static class Pricing
{
    public static QuoteResult Calculate(QuoteRequest request, PricingPolicy policy)
    {
        if (request.Quantity is < 1 or > 1000)
            return new(null, "Quantity must be between 1 and 1000.");

        if (request.UnitPrice is < 0.01m or > 1000000m)
            return new(null, "Unit price must be between 0.01 and 1000000.");

        if (decimal.Round(request.UnitPrice, 2) != request.UnitPrice)
            return new(null, "Unit price must have at most two decimal places.");

        if (policy.DiscountFrom < 1 || policy.DiscountRate is < 0m or > 1m)
            throw new ArgumentException("Invalid pricing policy.", nameof(policy));

        var subtotal = request.Quantity * request.UnitPrice;
        var rate = request.Quantity >= policy.DiscountFrom ? policy.DiscountRate : 0m;
        var discount = decimal.Round(subtotal * rate, 2, MidpointRounding.AwayFromZero);

        return new(new Quote(subtotal, discount, subtotal - discount, "EUR"), null);
    }
}
```

The static class is only there to give the function a name. It holds no global
state. Every input that affects the result is a parameter, and the same
inputs produce the same result. No clock, no configuration lookup, no
database call or random number hiding inside it.

The quantity and price bounds also make overflow a non-issue within this
example's accepted range. Rounding is an explicit business choice. We round
the discount to cents, away from zero at a midpoint, then subtract it from
the exact subtotal. Other businesses will need other rules, but ours can at
least be read and tested.

# Let HTTP translate, rather than calculate

Create `Program.cs`:

```csharp
using Quotes;

if (args.Contains("--self-test"))
{
    PricingChecks.Run();
    return;
}

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton(new PricingPolicy(10, 0.10m));
var app = builder.Build();

app.MapPost("/quotes", (QuoteRequest request, PricingPolicy policy) =>
    ToResponse(Pricing.Calculate(request, policy)));

app.Run();

static IResult ToResponse(QuoteResult result)
{
    if (result.Value is null)
        return Results.BadRequest(new { error = result.Error });

    return Results.Ok(result.Value);
}
```

ASP.NET Core binds the JSON body to `QuoteRequest` and resolves the registered
policy from dependency injection. The handler calculates and translates the
result into HTTP, and the pricing module doesn't know what a status code is.
This uses the handler support introduced in the
[ASP.NET Core 6 release](https://devblogs.microsoft.com/dotnet/announcing-asp-net-core-in-net-6/).

The policy is an immutable record registered as a singleton. There's no
mutable cart shared between requests. A database connection with per-request
ownership would be a different dependency with a different lifetime, and “small
application” is no reason to register everything as a singleton.

Expected invalid input becomes a result. An invalid internal policy throws,
because that means a programming or configuration error, and we should not
turn every internal failure into a cheerful HTTP 400 and blame the caller.

`QuoteResult` is intentionally simple, although its type permits combinations
we don't produce, such as both a value and an error. If this result ever
becomes a public library contract, stronger construction rules may be worth
adding. That would be a concrete reason for another type design. Adding a
generic result framework before the second use exists isn't one.

# Test the rule without starting a server

Add `PricingChecks.cs` so the self-test branch has this implementation:

```csharp
namespace Quotes;

public static class PricingChecks
{
    public static void Run()
    {
        var policy = new PricingPolicy(10, 0.10m);
        Check(Pricing.Calculate(new(2, 12.50m), policy).Value?.Total == 25m,
            "No discount below threshold");
        Check(Pricing.Calculate(new(10, 12.50m), policy).Value?.Total == 112.50m,
            "Discount at threshold");
        Check(Pricing.Calculate(new(10, 0.05m), policy).Value?.Discount == 0.05m,
            "Discount in cents");
        Check(Pricing.Calculate(new(1, 0.05m), new(1, 0.10m)).Value?.Total == 0.04m,
            "Midpoint rounding");
        Check(Pricing.Calculate(new(0, 1m), policy).Error is not null,
            "Reject zero quantity");
        Check(Pricing.Calculate(new(1001, 1m), policy).Error is not null,
            "Reject oversized quantity");
        Check(Pricing.Calculate(new(1, 1.001m), policy).Error is not null,
            "Reject fractional cents");
        Console.WriteLine("Pricing checks passed.");
    }

    private static void Check(bool condition, string message)
    {
        if (!condition) throw new Exception(message);
    }
}
```

Run `dotnet run -- --self-test`. A failed check terminates with an exception
and success prints the message. The harness is this tiny so the article has
no test-package prerequisites. In a repository that already has a test runner
I'd write these as normal tests there.

What I like is that we can exercise the policy without mocking an
HTTP context or constructing a service container. For the transport boundary,
start the server with
`dotnet run --no-launch-profile --urls http://127.0.0.1:5081` and send:

```bash
curl -i http://127.0.0.1:5081/quotes \
  -H 'Content-Type: application/json' \
  -d '{"quantity":10,"unitPrice":12.50}'

curl -i http://127.0.0.1:5081/quotes \
  -H 'Content-Type: application/json' \
  -d '{"quantity":0,"unitPrice":12.50}'
```

The first returns 200 with subtotal 125, discount 12.5, and total 112.5 EUR.
The second returns 400. Malformed JSON is a seperate binding failure and it's
handled before our calculation runs. Testing both layers keeps that visible.

# Compare that with the layers we did not need

A controller, an `IQuoteService`, a `QuoteService`, an `IPricingStrategy`, and
a factory could all wrap this function. They wouldn't make the calculation
any more replaceable than passing another policy already does. They would
just give the reader more code to get through before finding the rule.

I'd introduce a boundary when a real dependency shows up. A price catalog
that performs I/O could expose a focused interface, or a delegate if the
contract is one operation. The caller would fetch the authoritative data
and pass the resulting values into the calculation, so the calculation stays
deterministic while the I/O can fail, retry, or be cancelled explicitly.

That separation helps most when a quote becomes an order. An order needs
persistence, concurrency handling, and transaction decisions, and hiding
all of that behind a vaguely named service doesn't settle any of them.
I want the operation that commits an order to make its consistency boundary
obvious, even if that means more code than the quote calculator needs.

I also wouldn't create dozens of one-line static classes to simulate a
different kind of framework. A module can contain related functions. Group
them around the rules they share, and keep the dependency direction
understandable.

# Objects are useful when they protect something

A connection pool manages lifetime and concurrency. A domain object can
prevent an invalid state transition, and an interface can isolate a remote
provider with a stable contract. Those are all proper jobs for an object.

Records are a good fit for the immutable scalar data here, but they don't
make an application functional by themselves. As discussed in the
[C# 9 article](https://deyanpeev.com/posts/csharp-nine-dotnet-five), a record can still contain
mutable references, and a static method can still reach into global state.
The design comes from controlling effects and making inputs explicit.

I would also be wary of claims that removing these layers automatically
improves memory usage. Some abstractions allocate, some disappear into
ordinary calls, and the database payload may dwarf both. If performance is the
reason for changing them, measure. Readability and testability are reason
enough when those are the problems you have.

My starting point for a small C# service is data, functions, and a narrow
transport boundary. When an object has a real invariant or a resource to own,
give it that responsibility. In this example nothing does yet, so the discount
is still one function in `Pricing.cs`.
