Day 10: Structuring and Circulating Data (DTO & Read Flow)
From unstructured input to a complete data lifecycle
Introduction
At this stage, the system starts to feel different. Until now, data was created in a somewhat direct way.
With Day 10, the flow becomes explicit and structured:
input → mapping → storage → retrieval
This is the point where the system begins to resemble a real production pipeline, rather than a simple request-response experiment.
🔄 Updated System Flow
- Input (Day 10 – DTO): The user submits a structured request (JSON) containing a name and a message.
- Packaging (Mapping): The system converts the request into a database-ready entity.
- Storage (DB): The data is persisted in the database.
- Retrieval (Day 10 – GET): The user requests the full list, and the system returns all stored records.
1️⃣ Input Model: HelloRequest (DTO)
This class acts as a delivery box. Instead of accepting loose values, the system now requires a defined structure.
namespace HelloFlow.Models
{
// Data Transfer Object (DTO)
// Used only for incoming requests from the client.
public class HelloRequest
{
public string Name { get; set; }
public string Message { get; set; }
}
}
By introducing a DTO, the server gains control over what input is allowed.
2️⃣ Controller: Orchestrating Write and Read (Day 10)
The Controller now acts as a logistics center. It accepts incoming packages, stores them, and later retrieves them.
using Microsoft.AspNetCore.Mvc;
using HelloFlow.Data;
using HelloFlow.Models;
namespace HelloFlow.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class HelloController : ControllerBase
{
private readonly AppDbContext _context;
public HelloController(AppDbContext context)
{
_context = context;
}
// ==========================================
// Day 10: Read (GET)
// ==========================================
[HttpGet]
public IActionResult GetAllMessages()
{
var messages = _context.HelloResponses.ToList();
return Ok(messages);
}
// ==========================================
// Day 10: Write (POST)
// ==========================================
[HttpPost]
public IActionResult CreateHello([FromBody] HelloRequest request)
{
if (request == null || string.IsNullOrEmpty(request.Message))
{
return BadRequest("Message content is missing.");
}
// Mapping: DTO → Entity
var newResponse = new HelloResponse
{
Message = $"[{request.Name}] {request.Message}",
CreatedAt = DateTime.UtcNow
};
_context.HelloResponses.Add(newResponse);
_context.SaveChanges();
return Ok(newResponse);
}
}
}
🚀 Local Testing Guide (Rider)
1️⃣ Writing Data (POST)
Use Swagger and send the following request body:
{
"name": "Sabin",
"message": "Now I am sending structured data."
}
Confirm that the response status is 200 OK.
2️⃣ Reading Data (GET)
Call GET /api/Hello and confirm that all previously stored entries
are returned as a list.
🧠 What Changed on Day 10
- Input is no longer free-form; it must follow a defined structure (DTO).
- Data flows in a full cycle: request → database → response.
- The system feels less like a collection of endpoints and more like a coherent pipeline.
🧠 One-Sentence Summary
Day 10 completes the data loop by introducing structured input and consistent retrieval.
✍️ My Notes & Reflections
- As more components are added, the system becomes more complex, but it also feels more organized.
- I am starting to understand how a single factory slowly comes together, with data flowing through clearly defined stages.
Top comments (0)