How does the caching work? If the method is called again with the same parameters, does it load from the cache or fetch the data from the database into the cache again?
C Sharp
A community about the C# programming language
Getting started
Useful resources
- C# documentation
- C# Language Reference
- C# Programming Guide
- C# Coding Conventions
- .NET Framework Reference Source Code
IDEs and code editors
- Visual Studio (Windows/Mac)
- Rider (Windows/Mac/Linux)
- Visual Studio Code (Windows/Mac/Linux)
Tools
Rules
- Rule 1: Follow Lemmy rules
- Rule 2: Be excellent to each other, no hostility towards users for any reason
- Rule 3: No spam of tools/companies/advertisements
Related communities
Fetches from the database again, it's just a temporary bundle of data rather than a persistent cache. We have caching for commonly-read/rarely-updated entities but its not feasible for everything ofc.
I'm not sure how you do it at the moment or already know this since you mention repository pattern. But here's how I know.
A pattern I encountered at my workplace is a split between the repository and the data access (Dao) layer.
The repository implements an interface which other parts of your program uses to get data. The repository askes the data access layer to make database calls.
For testing other parts of the programs, we mock the repository interface, and implement simple returns of data instead of relying on the database at all. Then we have full control of what goes in and out of your legacy code, assuming you are able to use this.
For testing the dao, I don't actually have much experience since that's not a good option for us at the moment, but as others mentioned you can use in memory databases or manually mock the connection object provided to the dao to test that your save methods store the correct data. The latter being somewhat clunky in my experience but the best option when you are trying to save something with 20 values and making sure they end up in the right order or have the right values when converting enum values to strings for example.
I don't know much about cache, but if you want to keep it then it's possible to do it in the repository class.
If you ignore the caching, the approach you're describing loosely aligns with the concept of Domain-Driven Design (DDD). In DDD, the model is loaded before any business logic is executed, and then any changes made to the model are persisted back to the database.
In DDD, the model is loaded before any business logic is executed
That's really not a DDD requirement. Having a domain model does not require you to preload data to run business logic. For example, you can easily have business logic that only takes as input a value object and triggers a usecase, and you do not need to preload anything to instantiate a value.
Agree.
I’m just saying OP is loading stuff into a dictionary that perhaps function as a Domain Model. Then they pass this Domain Model to a Use Case, where it gets modified and saved to a database.
OP was asking for an architecture name or design pattern, and while it's not a perfect match, it's kinda like a Domain Model, although an anemic one.
None of this is a DDD requirement.