Calculated Values

BitLost

Active member
Joined
Dec 10, 2016
Messages
35
Programming Experience
Beginner
This may seem like a simple question. Its probably cause I am a bit fussy about trying to do the best job possible.

I have a length, width and depth. Pretty simple maths length by width by depth = volume.

In Access I wouldn't bother to calculate this value until presented. Therefore in MVC terms I would think this is calculated in the view.

Is this the correct approach in MVC? Or is the principle calculations like this completed in the controller? Or is it not important at all?
 
You should be using a view model (i.e. the model passed to the view) appropriate to the data that you need to display in the view. If the view needs to display volume then your view model should have a property for volume. If your lower-level model, which may be a DataTable/DataRow, an EF entity, a DTO or something else, does not have such a property then that should not be used as your view model, but rather it should be mapped to a different type defined specifically for the purpose of being a model for that view.

As for how the value gets calculated, that Volume property could be read-only and the calculation could be performed in the getter. Alternatively, the controller would do that calculation and then assign the result to the property. In most of the applications I work on, we use AutoMapper to map between EF entities and DTOs as well as between DTOs and view models so we would put the calculation in the class that is dedicated to performing the data mappings between DTOs and view models. In that case, the controller calls a service method and receives one or more DTOs and then invokes the mapper to get one or more view models which are then passed to the view.
 
Back
Top Bottom