I have in mind two options:
- Code in the class being saved/loaded. The flows for each entity/model are in the same place, so it’s easy to just have one file open to see all the functionalities of that class, but this means having more code in a single file.
- Code in a dedicated class (like a factory)
This makes each file smaller, but spreads flows of a single model into different parts of the repo, also because I’m thinking of having a directory/src/models
and another like/src/export
(orserialize
)
What do you guys think?
What’s your preferred way to organize the save and load flows?
Usually the serialization/deserialization code, I keep with the model. The part where a file or whatever comes in, I leave that to the caller to decide on. In other words, the model knows how to serialize and deserialize itself, but not where it’s being serialized to or deserialized from.
Then again, in C#, it’s usually just a couple attributes on my model, and in Rust, it’s usually just a couple derives. It’s rare I actually write much, if any, serialization logic by hand.