Created
February 28, 2026 11:48
-
-
Save davepcallan/5d011f4ee50e6a96c9eba37ad217df35 to your computer and use it in GitHub Desktop.
Vertical Slice all in one file example for creating a patient
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace MyApp.UseCases.Patients.CreatePatient; | |
| // 1. ENDPOINT | |
| [ApiController] | |
| public sealed class Endpoint(Handler handler) : ControllerBase | |
| { | |
| [HttpPost("patients")] | |
| public async Task<ActionResult<Response>> Post(Request req, CancellationToken ct) | |
| { | |
| var res = await handler.HandleAsync(req, ct); | |
| return Created($"/patients/{res.PatientId}", res); | |
| } | |
| } | |
| // 2. CONTRACTS | |
| public sealed record Request(string FirstName, string LastName, DateTime DateOfBirth, string Email); | |
| public sealed record Response(int PatientId, string FullName, string Email); | |
| // 3. VALIDATOR | |
| public sealed class Validator : AbstractValidator<Request> | |
| { | |
| public Validator() { /* Rules... */ } | |
| } | |
| // 4. HANDLER | |
| public sealed class Handler(HospitalDbContext db, IValidator<Request> validator) | |
| { | |
| public async Task<Response> HandleAsync(Request req, CancellationToken ct) | |
| { | |
| await validator.ValidateAndThrowAsync(req, ct); | |
| // Map request -> entity | |
| var patient = new Patient { /* ... */ }; | |
| // Save patient to database | |
| db.Patients.Add(patient); | |
| await db.SaveChangesAsync(ct); | |
| return new Response( | |
| patient.PatientId, | |
| $"{patient.FirstName} {patient.LastName}", | |
| patient.Email); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment