6 jenis kode yang tidak boleh berada di dalam pengontrol .NET


Pengontrol .NET harus ringan





Ungkapan umum yang selalu berulang dengan tiga ton koper untuk dibongkar.





Mengapa harus ringan? Apa gunanya ini? Langkah-langkah apa yang dapat saya ambil untuk membuatnya lebih mudah jika mereka belum melakukannya? Bagaimana cara membuatnya tetap ringan?





( ) , . «» , .





, , , .





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- /.





!

- ?





, , . , , , .





( ).






"C# ASP.NET Core ".





- «Serverless azure». 1: , serverless computing, serverless computing azure , azure function. 2: , azure storage, , ARM . !








All Articles