Tracks
/
FHIR
FHIR
/
Exercises
/
Build a FHIR Patient (.NET)
Build a FHIR Patient (.NET)

Build a FHIR Patient (.NET)

Easy

Instructions

A FHIR Patient resource is a small JSON document. Before a server can store or exchange one, your code has to build it and serialize it correctly β€” the key order and the presence/absence of optional fields all matter to strict consumers.

This kata is the offline, deterministic version of Batch-1 Day 5, Week 1, Assignment 3 (the .NET Spark FHIR server): instead of standing up a server, you build a Patient in memory with a fluent builder and emit compact FHIR JSON.

Implement FhirPatient with a fluent builder:

var json = FhirPatient.Builder()
    .Id("123")
    .Family("Smith")
    .AddGiven("John")
    .AddGiven("Q")
    .Gender("male")
    .BirthDate("1980-05-15")
    .Build()
    .ToJson();
  • static Builder Builder() β€” start a new builder.
  • Builder.Id(string) β€” set the logical id (optional).
  • Builder.Active(bool) β€” set the active flag (optional).
  • Builder.Family(string) β€” set the family (last) name (required).
  • Builder.AddGiven(string) β€” append a given (first/middle) name (optional, repeatable).
  • Builder.Gender(string) β€” set the administrative gender (required).
  • Builder.BirthDate(string) β€” set the birth date (required).
  • Builder.Build() β€” validate and return an immutable FhirPatient.
  • FhirPatient.ToJson() β€” emit compact FHIR JSON.

JSON shape

ToJson() returns compact JSON (no whitespace) with keys in exactly this order, omitting optional keys when they were never set:

{"resourceType":"Patient"[,"id":"<id>"][,"active":<true|false>],"name":[{"use":"official","family":"<family>"[,"given":["<g1>",...]]}],"gender":"<gender>","birthDate":"<birthDate>"}
  • resourceType is always "Patient".
  • id appears only if it was set.
  • active appears only if it was explicitly set (as a JSON boolean, not a string).
  • name is always an array with one object; use is always "official".
  • given appears only if at least one given name was added.
  • family, gender, and birthDate are always present.

Rules

  • Build() throws ArgumentException when:
    • family is null or empty;
    • gender is not one of male, female, other, unknown;
    • birthDate does not match ^\d{4}-\d{2}-\d{2}$.
  • String values must be JSON-escaped: a backslash becomes \\ and a double quote becomes \".

Source

Batch-1 Day 5 Week 1 Assignment 3 (.NET Spark FHIR server), refactored to an offline build-and-serialize kata.
Edit via GitHub The link opens in a new window or tab
FHIR Exercism

Ready to start Build a FHIR Patient (.NET)?

Sign up to Exercism to learn and master FHIR with 9 exercises, and real human mentoring, all for free.