I'm currently building an API and a web app for an internal warehouse system using NET Core. I have the core entity structure, that goes like this:

"Material" has many "MaterialSubtypes" has many "MaterialClasses" has many "Packs".

Now, I need to create a list, representing a single sale. It can include lots of packs of different materials. The user should be able to add or remove packs to a sale as it is being prepared. The problem is that I also need to show the user the list of all materials hierarchy that the sale contains, as well as the sum of "Quantity" field of all packs on each sublevel. This quantity is supposed to be updated dynamically in a client app.

What is the most efficient way to do this? Should I just add all packs to a sale, and then, on every GET request, Include everything and recalculate all sums via foreach loops? Or should I create separate entities for Material->MaterialSubtype->MaterialClass within the Sale and update them each time a Pack is added? None of that seems optimal, but I can't think of anything else.


The answer depends on your use case.

  • If you add many packs but read the sum far less frequently, it will probably be more efficient just to calculate the sum on every read.
  • If you add few packs but read the sum very frequently, it will be more efficient to calculate the sum every time you add a pack.

If you're somewhere in the middle, either 1) profile it and then decide or 2) just do either and don't worry about it.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.