Architecting-Modern-Web-Applications-with-ASP.NET-Core-and-Azure.pdf

(3428 KB) Pobierz
cw
EDITION v2.2
DOWNLOAD available at:
https://aka.ms/webappebook
PUBLISHED BY
DevDiv, .NET and Visual Studio product teams
A division of Microsoft Corporation
One Microsoft Way
Redmond, Washington 98052-6399
Copyright © 2019 by Microsoft Corporation
All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any means without the written permission
of the publisher.
This
book is provided “as-is” and expresses the author’s views and opinions. The views, opinions and
information expressed in this book, including URL
and other Internet website references, may change without notice.
Some examples depicted herein are provided for illustration only and are fictitious. No real association or connection is intended or should be inferred.
Microsoft and the trademarks listed at http://www.microsoft.com on the “Trademarks” webpage are
trademarks of the Microsoft group of companies. All
other marks are property of their respective owners.
Author:
Steve Smith (http://ardalis.com)
Editor:
Maira Wenzel
Participants and reviewers:
Scott Hanselman, .NET product team, Microsoft
Cesar de la Torre, .NET product team, Microsoft
Contents
Introduction................................................................................................................................................. 1
Version..................................................................................................................................................................................... 1
Purpose ........................................................................................................................................................................................ 2
Who should use this guide ............................................................................................................................................. 2
How you can use this guide............................................................................................................................................ 2
Characteristics of Modern Web Applications ......................................................................................... 3
Summary ..................................................................................................................................................................................... 3
Reference Application: eShopOnWeb ............................................................................................................................. 3
Cloud-Hosted and Scalable ................................................................................................................................................. 4
Cross Platform ........................................................................................................................................................................... 4
Modular and Loosely Coupled ........................................................................................................................................... 5
Easily Tested with Automated Tests ................................................................................................................................. 5
Traditional and SPA Behaviors Supported ..................................................................................................................... 5
Simple Development and Deployment ........................................................................................................................... 6
Traditional ASP.NET and Web Forms ............................................................................................................................... 6
Choosing Between Traditional Web Apps and Single Page Apps (SPAs) .......................................... 7
Summary ..................................................................................................................................................................................... 7
Razor Components ............................................................................................................................................................. 8
Project Templates
................................................................................................................................................................ 8
When to choose traditional web apps ......................................................................................................................... 11
When to choose SPAs ......................................................................................................................................................... 12
Decision table
Traditional Web or SPA .................................................................................................................... 13
Architectural Principles ............................................................................................................................ 14
Summary .................................................................................................................................................................................. 14
Common design principles ............................................................................................................................................... 14
Separation of Concerns ................................................................................................................................................. 14
Encapsulation..................................................................................................................................................................... 15
Dependency Inversion ................................................................................................................................................... 15
i
Explicit Dependencies .................................................................................................................................................... 17
Single Responsibility ....................................................................................................................................................... 17
Don’t Repeat Yourself (DRY)
........................................................................................................................................ 18
Persistence Ignorance .................................................................................................................................................... 19
Bounded Contexts ........................................................................................................................................................... 20
Common Web Application Architectures ............................................................................................. 21
Summary .................................................................................................................................................................................. 21
What is a monolithic application? .................................................................................................................................. 21
All-in-One applications....................................................................................................................................................... 21
What are layers? .................................................................................................................................................................... 22
Traditional “N-Layer” architecture
applications ........................................................................................................ 24
Clean architecture ................................................................................................................................................................. 28
Monolithic Applications and Containers ..................................................................................................................... 34
Monolithic application deployed as a container ................................................................................................. 36
Docker support ................................................................................................................................................................. 38
Common Client-Side Web Technologies .............................................................................................. 40
Summary .................................................................................................................................................................................. 40
HTML ......................................................................................................................................................................................... 40
CSS .............................................................................................................................................................................................. 41
JavaScript ................................................................................................................................................................................. 42
Legacy Web Apps with jQuery .................................................................................................................................... 42
jQuery vs a SPA Framework ......................................................................................................................................... 42
Angular SPAs...................................................................................................................................................................... 43
React...................................................................................................................................................................................... 44
Choosing a SPA Framework ......................................................................................................................................... 44
Developing ASP.NET Core Apps ............................................................................................................. 46
Summary .................................................................................................................................................................................. 46
MVC and Razor Pages......................................................................................................................................................... 46
Why Razor Pages? ........................................................................................................................................................... 47
When to use MVC ............................................................................................................................................................ 48
Mapping Requests to Responses ................................................................................................................................... 48
Working with Dependencies ............................................................................................................................................ 51
Declare Your Dependencies ......................................................................................................................................... 52
Structuring the Application............................................................................................................................................... 53
Feature Organization ...................................................................................................................................................... 54
Cross-Cutting Concerns ................................................................................................................................................. 56
Security ..................................................................................................................................................................................... 59
Authentication ................................................................................................................................................................... 59
ii
Authorization ..................................................................................................................................................................... 61
Client Communication ........................................................................................................................................................ 64
Domain-Driven Design
Should You Apply It?........................................................................................................ 65
When Should You Apply DDD .................................................................................................................................... 66
When Shouldn’t You Apply DDD
............................................................................................................................... 66
Deployment ............................................................................................................................................................................ 67
Working with Data in ASP.NET Core Apps ........................................................................................... 69
Summary .................................................................................................................................................................................. 69
Entity Framework Core (for relational databases) ................................................................................................... 69
The DbContext .................................................................................................................................................................. 70
Configuring EF Core ........................................................................................................................................................ 70
Fetching and Storing Data............................................................................................................................................ 72
Fetching Related Data .................................................................................................................................................... 73
Encapsulating Data .......................................................................................................................................................... 74
Resilient Connections ..................................................................................................................................................... 75
EF Core or micro-ORM? ..................................................................................................................................................... 77
SQL or NoSQL ........................................................................................................................................................................ 79
Azure Cosmos DB ................................................................................................................................................................. 80
Other Persistence Options ................................................................................................................................................ 81
Caching ..................................................................................................................................................................................... 81
ASP.NET Core Response Caching .............................................................................................................................. 81
Data Caching ...................................................................................................................................................................... 82
Testing ASP.NET Core MVC Apps........................................................................................................... 86
Summary .................................................................................................................................................................................. 86
Kinds of Automated Tests ................................................................................................................................................. 86
Unit Tests ............................................................................................................................................................................. 86
Integration Tests ............................................................................................................................................................... 87
Functional Tests ................................................................................................................................................................ 88
Testing Pyramid ................................................................................................................................................................ 89
What to Test ....................................................................................................................................................................... 90
Organizing Test Projects .................................................................................................................................................... 90
Test Naming ....................................................................................................................................................................... 92
Unit Testing ASP.NET Core Apps .................................................................................................................................... 93
Integration Testing ASP.NET Core Apps ...................................................................................................................... 95
Functional Testing ASP.NET Core Apps ....................................................................................................................... 95
Development process for Azure-hosted ASP.NET Core applications ............................................... 99
Vision ......................................................................................................................................................................................... 99
Development environment for ASP.NET Core apps ............................................................................................... 99
iii
Zgłoś jeśli naruszono regulamin