Because of this, the SelectList location must be set to Location5 by default when the input screen for a new employee is displayed. Should I set the default location in the model or in the view model?
This is the business logic in your example, but it will not stop you from having your cake in this case. The model may indicate a default value; the view then initializes itself with this default value.
In general, whether it is “business logic” or “presentation logic”, it depends on whether it belongs to the domain or not. For example, setting the earliest year in a date drop-down list, say in 1900, is probably related to the view. But it can also be a problem for the business if the system is not designed to receive dates earlier than in 1900.
One thing that I realized is that unit tests become inconvenient because in both cases I would have to test against a location that will always be present in production, but I cannot create a unit test with my own test data if only Atlanta was in the set of locations used in the test. I would be baud if you had thoughts about it.
With the strategy I mentioned above, unit testing is easy. Just make sure:
- the model provides a default value
- view accepts this default
- view initialized to default
- the view has appropriate behavior, regardless of whether the model matches this value
John feminella
source share