liveBook · Manning

Single responsibility principle

As explained in ASP.NET Core in Action, 3e

The single-responsibility principle is a design principle stating that a class should have responsibility for one piece of functionality and change only when that functionality changes.

The single-responsibility principle (SRP) keeps each class responsible for one piece of functionality and limits its reasons to change. Small, independent services can then combine into a complete application. section 3.7.1

An order total built from focused services

An e-commerce application can use TaxCalculator for tax, ShippingCostService for shipping, and OrderTotalCalculator to combine both results. OrderTotalCalculator should use those services rather than calculate every concern itself. section 3.7.1

SRP encourages a practical separation of responsibilities:

  • Give each class one independent piece of functionality. section 3.7.1
  • Avoid combining service use with dependency construction; dependency injection lets another class provide required services. section 3.7.1

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains the definition, applies it to e-commerce services, and connects it to dependency construction without detailing implementation registration.
Examples
Uses TaxCalculator, ShippingCostService, and OrderTotalCalculator in an e-commerce order calculation.
What you can do after
Apply SRP by separating tax, shipping, and order-total responsibilities into focused services.
How it connects
Connects services with dependency injection, inversion of control, modularization, and application design.

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 ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.