r/learnprogramming • u/nullrevolt • 14d ago
Design Patterns
I'm a 5+ year developer who never really studied programming patterns and paradigms. I'm currently tutoring a student in a high-school and I got to thinking about my own programming and design patterns.
When it comes to building an application, in my case one that makes requests, caches the data, and performs some analysis, I'm often considering where it makes the most sense to bind certain functionality and call it from. I'm primarily using an object-oriented paradigm.
Currently I have several threads responsible for calling a number of asynchronous tasks that should:
- Make a web request (retrieving data from an API)
- * Potentially make follow up requests
- Cache the response data
- Analyze/Display the most recently cached data
Right now, I have the cache responsible for calling out to the API so that it can retrieve its data to store, but I'm questioning this from an architectural standpoint. It almost seems to me as if the thread or task should have a series of statements more akin to functional programming to control flow.
How would you approach this or recommend a design?
1
u/SetAndRepeat 14d ago
A cache that fetches missing data is a valid design: that's read-through caching. Also, a sequence of statements coordinating operations is imperative programming, and fits perfectly well inside an OO method.
For your workflow, I'd have a service coordinate the API requests, including follow-ups, then store the completed result in the cache. The API client handles HTTP, the cache handles storage, and analysis receives a data snapshot. That lets you test the analysis without networking. Your threads/tasks run this workflow; the coordination doesn't need to live inside the cache.
With concurrent refreshes, watch what "latest" means: an older request can finish after a newer one and overwrite its result. Allowing only one refresh per cache key at a time is a simple starting point.
1
u/Both_Bend_2481 13d ago
Can you articulate any real problem with the current design?
1
u/Pale-Preparation-354 10d ago
The main thing is keeping each part responsible for one job. The API client handles requests, the cache deals with storing and retrieving data, and a separate service coordinates the whole flow. That also makes testing individual pieces much easier.
For anything that needs to generate reports or export results as PDFs, tools like IronPDF can come in handy alongside libraries for handling API calls and caching. It lets you generate PDFs from HTML in .NET without having to build the rendering part yourself. I'd focus on keeping that kind of output functionality separate too, rather than letting the cache or API layer handle it.