I’m gathering large quantity of data in a corporative system (through a bot) in the form of objects and I’m not sure what is the OOP recomendation for dealing with this data.
Each object will contain quite small quantity of data, but they’re going to be thousands. Should I record in database the object as soon as it is created or should I keep it on memory until the procedure ends, then I update the database?
The first case seems to be safer for me, but is slower too, due the constant recording. In second case, I’ll gain on performance but the data will be kept only in memory until the procedure (wich would spend a lot of time) ends.
I frequently ask to myself what OOP recommends on storing data from objects. Should the object implements itself record/retrieve data methods from database? Or ought I to create a class to manipulate the objects recordings in DB? If you have experience in this area, I would like to know what you’re practicing. Thanks!
Use the repository pattern, meaning, create a class to encapsulate access to some storage.
In your case I recommend that the implementation of the repository behavior will be as following:
when saving an object, store it in memory and do a bulk db insert periodically.