liveBook · Manning

Single responsibility principle

As explained in .NET MAUI in Action

The single-responsibility principle is a software design principle stating that a component should have one primary reason to change.

The single-responsibility principle (SRP) says that each class or method should have one purpose, with every part aligned to that purpose. In application architecture, separating UI, presentation, and business responsibilities reduces coupling and makes changes easier to isolate. section 9.4 section 9.3

Apply SRP in an MVVM architecture by assigning each concern to its proper component:

  • Keep layout and UI behavior, such as markup, animation, and formatting, in the View. section 9.3 section 9.3.5
  • Keep UI state and presentational logic, such as bound properties and responses to user actions, in the ViewModel. section 9.3 section 9.3.5
  • Keep domain objects, processing rules, and database or API operations in the Model and its services. section 9.3 section 9.3.5

MauiStockTake without MVVM

A UI that gathers control values, constructs service requests, manages application state, and executes business logic violates SRP. The result is tight coupling, difficult maintenance, and business logic that is almost impossible to test except manually. Moving state and commands into a ViewModel and business operations into the Model separates those responsibilities. section 9.4

Figure 9.7 In the MVVM pattern, the View only contains UI logic. Controls are bound to properties on a ViewModel, and rather than event handlers, commands are used to trigger actions. The ViewModel maintains the state of the View by exposing properties for the UI to bind to and commands for the UI to call. The Model contains all the business logic, and the ViewModel interacts with the Model. In this example, a CarService from the Model is injected into the ViewModel. A Button in the View is used to trigger a command in the ViewModel. The ViewModel contains a Car object, which has properties bound in the UI. The Add Car Command calls the AddCar method in the injected service and passes in the ViewModel’s Car property as a parameter.

Trace how a car application divides responsibilities: the View binds controls, the ViewModel exposes state and commands, and an injected CarService performs business operations. section 9.3.5

How .NET MAUI in Action covers it

How deep it goes
Defines one-purpose classes and methods, then works through responsibility boundaries and their maintenance and testing effects.
Examples
Uses MauiStockTake, a car database application, injected services, bound properties, commands, and UI-versus-business-logic comparisons.
What you can do after
Apply it via MVVM to separate UI state and behavior from business operations, improving maintainability and testability.
How it connects
Connects SRP with SOLID, MVVM, View, ViewModel, Model, UI logic, presentation logic, and business logic.

Also explained in

Compare how 7 books explain single responsibility principle →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET MAUI in Action. The section links point to the parts of the book it draws on.