6 Dinge, die Sie in ASP.NET-Controllern nicht tun sollten

ASP.NET-Controller sollten dünn sein





Oh, diese immer wiederkehrende Plattheit, überwachsen mit tonnenweise Untertreibung.





Warum sollten sie dünn sein? Was ist das Plus? Wie kann man sie dünn machen, wenn sie es jetzt nicht sind? Wie Sie halten sie dünn?





( ) . , .





, , .





, 6 , , . , , .





1. (DTO)

, , - , , HTTP.





, - :





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)
{
    // ,  VIN   ...
    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. /

, ,



. CRUD , , .





, .





, , , , .





( ). , , .





public IActionResult CheckOutBook(BookRequest request)
{
    var book = _bookRepository.GetBookByTitleAndAuthor(request.Title, request.Author);

    //        ,   
    //         
  	// ...

		return Ok(book);
}
      
      



CRUD , , , .





( ) - CQRS .





!


, , ? - ? ? !





"C# ASP.NET Core ".





, , , 5 .









UPD:

4 , , . « ?» « ?».





, , , , . , — HTTP . , HTTP , HTTP .





« , ?» « ?» — , , .








All Articles