.NET controllers should be lightweight
An ever-repeated commonplace phrase with three tons of luggage to be unpacked.
Why should they be light? What is the use of this? What steps can I take to make them easier if they are not already? How do I keep them light?
, , , .
6 , , . , , .
1. (DTO)
, , , URL HTTP .
, DTO, , , - , , .
, :
public IActionResult CheckOutBook([FromBody]BookRequest bookRequest)
{
var book = new Book();
book.Title = bookRequest.Title;
book.Rating = bookRequest.Rating.ToString();
book.AuthorID = bookRequest.AuthorID;
//...
}
, . - HTTP-.
2.
, . , , .
-. , , . ASP.NET MVC, , , .
!
public IActionResult Register([FromBody]AutomobileRegistrationRequest request)
{
// //validating that a VIN number was provided...
if (string.IsNullOrEmpty(request.VIN))
{
return BadRequest();
}
//...
}
! ( )
3. -
-, , , , .
. , - ( , , ), .
4.
, . , , .
, , ASP.NET (, ).
(User) , / -, , , .
5.
, !
public IActionResult GetBookById(int id)
{
try
{
// , -...
}
catch (DoesNotExistException)
{
// -, ...
}
catch (Exception e)
{
// , ...
}
}
, , , , . , , - .
-, , - . , - .
6. /
, (Repository), , . - CRUD, .
, .
, , , , .
-, ( ), , , , .
public IActionResult CheckOutBook(BookRequest request)
{
var book = _bookRepository.GetBookByTitleAndAuthor(request.Title, request.Author);
// ,
// - ...
//...
return Ok(book);
}
CRUD, — , .
- ( ) , - CQRS- /.
!
- ?
, , . , , , .
( ).
- «Serverless azure». 1: , serverless computing, serverless computing azure , azure function. 2: , azure storage, , ARM . !