Setting a default value - presentation logic or business logic?

I was wondering if the default setting for SelectList is considered presentation logic or business logic? For example, if the requirement is that the Employee cannot be saved without a location, but in 99% of cases the location to be selected is a specific element, for example, in Atlanta. Because of this, the SelectList location must be sent to Atlanta by default when the input screen for a new employee is displayed. Should I set the default location in the model or view model? One thing that I realized is that unit tests become inconvenient because in both cases I would have to test a place that would 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 places used in the test. I would be baud if you had thoughts about it.

+7
mvp asp.net-mvc
source share
4 answers

As with many such (subjective) questions, the answer is: "It depends."

If the “default” is a business default (for example, the default location for a business location or the number of default units in an order or something similar), then it’s probably the business level. This seems right for your specific situation here.

However, if the "default" of your list is simply because you need some kind of default, and you are just going to select index 0, or just going to choose based on the user's location or system settings, I would think that these will be presentation level problems.

+4
source share

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
+3
source share

I would think that the defaults are business logic.

For example, if a company relocates its default location, it is no longer Altanta or London, but New York or Nottingham.

0
source share

If you were really concerned about maintaining the boundaries of business rules and the presentation level, you could provide a default value through business logic, and your presentation level could use this default value to initialize the controls.

0
source share

All Articles