Advantages of Design Patterns in Web API: Commands

In these posts I would like to point out the benefit of using a number of design patterns specifically for Web API projects. Specifically I will describe Commands, Strategies, Factories and Builders. Afterwards I will look at dependency injection, mocking and AutoMapper, and lastly conclude how it all works together.
Today we start with Commands.

Commands are used to encapsulate a set of actions for a specific goal, while shielding the implementation details. Coincidentally, this makes them an ideal fit to Web API endpoints; The Command contains the business logic for an endpoint. A comprehensive description can be found here on codeproject by Rahul Rajat Singh.

The major benefits are:

  • Keep your Controllers small. Only a single call to the appropriate Command is sufficient.
  • Make your Command as unit of logic testable through unit testing. We will get to that under dependency injection and AutoMapper.
  • Provide a single readable script for each endpoint, making debugging and maintenance much easier.
  • Easily portable and usable in different scenarios. Think of sharing and migration between WCF, Web API, MVC application or maintenance tooling (for application management for example). Each can reuse the same scripts, resulting in consistent results and an easier to maintain codebase.
Facebooktwitterredditpinterestlinkedinmail

Leave a Reply