.NET-Controller sollten leichtgewichtig sein
Eine immer wiederkehrende Redewendung mit drei Tonnen Gepäck zum Auspacken.
Warum sollten sie leicht sein? Was nützt das? Welche Schritte kann ich unternehmen, um sie zu vereinfachen, wenn dies noch nicht geschehen ist? Wie halte ich sie leicht?
, , , .
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 . !