So I have a design decision to make. I’m building a website, so the speed would be the most important thing. I have values that depend on other values. I have two options:
1- Retrieve my objects from the database, and then generate the dependent values/objects.
2- Retrieve the objects with the dependent values already stored in the database.
I’m using ASP.NET MVC with Entity Framework.
What considerations should I have in making that choice?
Ask yourself this question:
If so, then don’t store them in the database – not because you can’t or shouldn’t, but because it is good practice – you should only have business rules in the database if that is the best or only place to have it, not just because you can.
Serializing your objects to the database will usually be slower than creating the objects in normal compiled code. Database access is normally pretty quick, it is the act of serialization that is slow. However if you have a complicated object creation process that is time consuming then serialization could end up quicker, especially if you use a custom serialization method.
Sooooo…. if your ‘objects’ are relatively normal data objects with some calculated/derived values then I would suggest that you store the values of the ‘objects’ in the database, read those values from the database and map them to data objects created in the compiled code*, then calculate your dependent values.
*Note that this is standard data retrieval – some people use an ORM, some manually map the values to objects.