Preface xxi
Chapter 1: Understanding the Importance of Software Architecture 1
What is software architecture? .................................................................................................. 2
Creating an Azure account ........................................................................................................ 4
Software development process models ...................................................................................... 5
Reviewing traditional software development process models • 5
Understanding the waterfall model principles • 6
Analyzing the incremental model • 6
Understanding agile software development process models • 7
Scaling agile throughout a company • 12
Gathering the right information to design high-quality software .............................................. 13
Understanding the requirements gathering process • 14
Detecting exact user needs • 14
Analyzing requirements • 15
Writing the specifications • 16
Understanding the principles of scalability, robustness, security, and performance • 17
Reviewing the specification • 17
Using design techniques as a helpful tool ................................................................................ 18
Design Thinking • 18
Design Sprint • 19
Common cases where the requirements gathering process impacts system results ................... 19
Case 1 – my website is too slow to open that page! • 19
Understanding backend caching • 20
Applying asynchronous programming • 20
Dealing with object allocation • 21
Case 2 – the user’s needs are not properly implemented • 22
Case 3 – the usability of the system does not meet the user’s needs • 22
Summary ............................................................................................................................... 23
Questions ............................................................................................................................... 23
Further reading ...................................................................................................................... 23
Chapter 2: Non-Functional Requirements 25
Technical requirements .......................................................................................................... 25
Enabling scalability, availability, and resiliency with Azure and .NET 8 ..................................... 25
Creating a scalable web app in Azure • 26
Vertical scaling (scaling up) • 29
Horizontal scaling (scaling out) • 30
Creating a scalable web app with .NET 8 • 31
Performance issues that need to be considered when programming in C# ................................ 38
String concatenation • 38
Exceptions • 39
Multithreading environments for better results – dos and don’ts • 40
Software usability: how to design effective user interfaces ....................................................... 42
Designing fast selection logic • 44
Selecting from a huge number of items • 46
Interoperability with .NET 8 ................................................................................................... 47
Tip – creating a service in Linux • 48
Achieving security by design ................................................................................................... 49
List of practices for achieving a safe architecture • 50
Authentication • 50
Sensitive data • 51
Web security • 51
Summary ............................................................................................................................... 52
Questions ............................................................................................................................... 52
Further reading ...................................................................................................................... 53
Chapter 3: Managing Requirements 55
Technical requirements .......................................................................................................... 55
Introducing Azure DevOps ...................................................................................................... 56
Managing system requirements in Azure DevOps • 58
Epic work items • 59
Product Backlog items/User Story work items • 59
Azure DevOps repository • 60
Package feeds • 63
Test Plans • 66
Pipelines • 66
Usage • 67
Introducing GitHub projects ................................................................................................... 67
Summary ............................................................................................................................... 72
Questions ............................................................................................................................... 72
Chapter 4: Best Practices in Coding C# 12 75
Technical requirements .......................................................................................................... 75
The simpler your code, the better a programmer you are ......................................................... 75
Maintainability index • 76
Cyclomatic complexity • 77
Depth of inheritance • 82
Class coupling • 83
Number of lines of code • 86
Using a version control system ................................................................................................ 86
Dealing with version control systems in teams • 87
Writing safe code in C# ........................................................................................................... 87
try-catch • 87
try-finally and using • 89
The IDisposable interface • 91
.NET 8 tips and tricks for coding .............................................................................................. 92
Identifying well-written code .................................................................................................. 93
Understanding and applying tools that can evaluate C# code • 94
Applying extension tools to analyze code • 98
Applying SonarAnalyzer • 99
Checking the final code after analysis • 101
Summary ............................................................................................................................. 101
Questions ............................................................................................................................. 102
Further reading .................................................................................................................... 102
Chapter 5: Implementing Code Reusability in C# 12 105
Technical requirements ........................................................................................................ 105
................................................................... 105
What code reuse is not • 106
What code reuse is • 107
Reusability in the development life cycle • 109
Using .NET 8 for code reuse .................................................................................................. 109
Creating a reusable class library • 110
How does C# deal with code reuse? • 111
Object-oriented analysis • 111
Generics • 113
What if the code is not reusable? • 114
I have my libraries. How do I promote them? • 115
Documenting .NET libraries using DocFX • 116
Documenting a web API using Swagger • 117
Summary ............................................................................................................................. 117
Questions ............................................................................................................................. 117
Further reading .................................................................................................................... 118
Chapter 6: Design Patterns and .NET 8 Implementation 119
Technical requirements ........................................................................................................ 119
Understanding design patterns and their purpose ................................................................. 119
Builder pattern • 121
Factory pattern • 124
Singleton pattern • 126
Proxy pattern • 129
Command pattern • 131
Publisher/Subscriber pattern • 133
Dependency Injection pattern • 134
Understanding the design patterns available in .NET ............................................................. 135
Summary ............................................................................................................................. 136
Questions ............................................................................................................................. 137
Further reading .................................................................................................................... 137
Chapter 7: Understanding the Different Domains in Software Solutions 139
Technical requirements ........................................................................................................ 140
What are software domains? ................................................................................................. 140
Understanding DDD ............................................................................................................. 142
Relationships among Bounded Contexts • 144
Entity-level validation in .NET • 146
DDD entities in .NET • 148
Value objects • 149
Aggregates • 150
Domain events • 151
Common DDD patterns and architectures ............................................................................. 151
Classic layers architecture • 152
Onion architecture • 153
Repository pattern • 156
Unit of work pattern • 157
Classic repository pattern versus DDD aggregates • 159
Command Query Responsibility Segregation (CQRS) pattern • 160
Event sourcing • 162
Command handlers and aggregate events • 163
Summary ............................................................................................................................. 166
Questions ............................................................................................................................. 166
Further reading .................................................................................................................... 166
Chapter 8: Understanding DevOps Principles and CI/CD 169
Technical requirements ........................................................................................................ 170
Describing DevOps ............................................................................................................... 170
Understanding DevOps principles ......................................................................................... 170
CI • 171
CD • 171
Risks and challenges when using CI/CD • 172
Disabling continuous production deployment • 173
Incomplete features • 174
An unstable solution for testing • 177
Continuous feedback • 181
Tools to facilitate DevOps implementation ............................................................................ 181
Azure DevOps • 181
GitHub • 189
Application Insights • 191
Test and Feedback • 198
Summary ............................................................................................................................. 202
Questions ............................................................................................................................. 203
Chapter 9: Testing Your Enterprise Application 205
Technical requirements ........................................................................................................ 206
Understanding unit and integration tests .............................................................................. 206
Automating unit and integration tests • 207
Writing automated (unit and integration) tests • 208
Acceptance tests: writing functional and performance tests • 209
Understanding the basics of test-driven development ............................................................ 210
Functional tests .................................................................................................................... 214
Behavior-Driven Development (BDD) .................................................................................... 217
Defining C# test projects in Visual Studio ............................................................................... 218
Using the xUnit test framework • 219
Advanced test preparation and tear-down scenarios • 221
Mocking interfaces with Moq • 223
Automating functional tests in C# .......................................................................................... 225
Testing the staging application • 226
Testing the staging application with Selenium • 227
Testing a controlled application • 228
Recording tests with Selenium IDE • 230
Summary ............................................................................................................................. 231
Questions ............................................................................................................................. 231
Further reading .................................................................................................................... 232
Chapter 10: Deciding on the Best Cloud-Based Solution 233
Technical requirements ........................................................................................................ 233
Different software deployment models .................................................................................. 234
IaaS and Azure opportunities • 234
Security responsibility in IaaS • 236
PaaS – a world of opportunities for developers • 237
Web apps • 238
SQL databases • 239
Azure Cognitive Services • 241
SaaS — just sign in and get started! • 244
Understanding what serverless means • 245
Comparing IaaS, PaaS, SaaS, and FaaS • 246
Why are hybrid applications so useful in many cases? ............................................................ 247
Summary ............................................................................................................................. 247
Questions ............................................................................................................................. 248
Further reading .................................................................................................................... 248
Chapter 11: Applying a Microservice Architecture to
Your Enterprise Application 251
Technical requirements ........................................................................................................ 251
What are microservices? ....................................................................................................... 252
Microservices and the evolution of the concept of modules • 253
Microservice design principles • 254
The independence of design choices • 255
Independence from the deployment environment • 256
Loose coupling • 256
No chained requests/responses • 256
Containers and Docker • 260
When do microservices help? ............................................................................................... 261
Layered architectures and microservices • 261
The presentation layer • 264
When is it worth considering microservice architectures? • 266
How does .NET deal with microservices? ............................................................................... 268
.NET communication facilities • 268
Resilient task execution • 270
Using generic hosts • 271
Visual Studio support for Docker • 276
Analyzing the Docker file • 277
Publishing the project • 279
Azure and Visual Studio support for microservice orchestration • 281
Which tools are needed to manage microservices? ................................................................. 282
Defining your private Docker registry in Azure • 282
Summary ............................................................................................................................. 285
Questions ............................................................................................................................. 285
Further reading .................................................................................................................... 285
Chapter 12: Choosing Your Data Storage in the Cloud 287
Technical requirements ........................................................................................................ 288
Understanding the different repositories for different purposes ............................................. 288
Relational databases • 288
Document-oriented database • 292
Graph database • 293
Key-value database • 293
Wide-column store database • 294
Redis • 294
Azure storage accounts • 296
Choosing between SQL and NoSQL document-oriented databases .......................................... 297
Azure Cosmos DB – an opportunity to manage a multi-continental database ........................... 299
Creating an Azure Cosmos DB account • 300
Creating an Azure Cosmos DB container • 300
Accessing Azure Cosmos DB • 302
Defining database consistency • 302
The Cosmos DB client • 305
The Cosmos DB Entity Framework Core provider • 306
Summary ............................................................................................................................. 306
Questions ............................................................................................................................. 307
Further reading .................................................................................................................... 307
Chapter 13: Interacting with Data in C# – Entity Framework Core 309
Technical requirements ........................................................................................................ 310
Understanding ORM basics ................................................................................................... 310
Configuring Entity Framework Core ...................................................................................... 313
Defining DB entities • 314
Defining the mapped collections • 317
Completing the mapping configuration • 317
Entity Framework Core migrations ....................................................................................... 321
Understanding stored procedures and direct SQL commands • 325
Compiled models ................................................................................................................. 325
Querying and updating data with Entity Framework Core ...................................................... 326
Returning data to the presentation layer • 330
Issuing direct SQL commands • 332
Handling transactions • 333
Deploying your data layer ..................................................................................................... 334
How data and domain layers communicate with other layers ................................................. 335
Understanding Entity Framework Core advanced features ..................................................... 336
Summary ............................................................................................................................. 339
339
Further reading .................................................................................................................... 339
Chapter 14: Implementing Microservices with .NET 341
Technical requirements ........................................................................................................ 342
Communication and data serialization .................................................................................. 342
Efficient and flexible binary serialization • 345
The ProtoBuf language • 346
ProtoBuf serialization • 351
Efficient and flexible RPC • 352
Reliable data-driven asynchronous communication • 355
Distributed transactions • 357
Implementing worker microservices with ASP.NET Core ....................................................... 359
Implementing microservices with .NET worker services and message brokers ....................... 362
Installing RabbitMQ • 362
RabbitMQ basics • 362
Replacing internal queues with RabbitMQ • 364
Summary ............................................................................................................................. 368
Questions ............................................................................................................................. 368
Further reading .................................................................................................................... 368
Chapter 15: Applying Service-Oriented Architectures with .NET 371
Technical requirements ........................................................................................................ 372
Understanding the principles of the SOA approach ................................................................ 372
SOAP web services ............................................................................................................... 375
SOAP specifications • 376
Difficulties associated with the standard • 377
REST web services ................................................................................................................ 377
Service type compatibility rules • 378
REST and native HTTP features • 379
Example of methods in the REST language • 381
The OpenAPI standard • 382
REST service authorization and authentication • 383
API versioning • 385
How does .NET 8 deal with SOA? ........................................................................................... 386
SOAP client support • 386
gRPC support • 387
Implementing REST services with ASP.NET Core • 390
ASP.NET Core service authorization • 393
ASP.NET Core support for OpenAPI • 396
.NET HTTP clients • 400
Summary ............................................................................................................................. 402
Questions ............................................................................................................................. 402
Further reading .................................................................................................................... 403
Chapter 16: Working with Serverless – Azure Functions 405
Technical requirements ........................................................................................................ 406
Understanding the Azure Functions app ................................................................................ 406
Consumption plan • 407
Premium plan • 407
The App Service plan • 408
Programming Azure Functions using C# ................................................................................ 408
Listing Azure Functions templates • 413
Maintaining Azure Functions ................................................................................................ 414
Azure Durable Functions ...................................................................................................... 416
Azure Functions roadmap ..................................................................................................... 416
The decision to use serverless and Azure Functions ............................................................... 417
Summary ............................................................................................................................. 419
Questions ............................................................................................................................. 419
Further reading .................................................................................................................... 419
Chapter 17: Presenting ASP.NET Core 421
Technical requirements ........................................................................................................ 421
Understanding the presentation layers of web applications .................................................... 422
Understanding the basics of ASP.NET Core ............................................................................ 422
ASP.NET Core middleware • 423
Loading configuration data and using it with the options framework • 427
Defining the ASP.NET Core pipeline • 433
Defining controllers and ViewModels • 438
Understanding how ASP.NET Core MVC creates the response HTML ...................................... 443
Razor Views • 443
Learning the Razor flow of control statements • 445
Understanding Razor view properties • 447
Reusing view code • 452
Understanding the connection between ASP.NET Core MVC and design principles ................. 456
Advantages of the ASP.NET Core pipeline • 457
Server-side and client-side validation • 457
ASP.NET Core globalization • 458
The MVC pattern • 461
Summary ............................................................................................................................. 463
Questions ............................................................................................................................. 463
Further reading .................................................................................................................... 463
Chapter 18: Implementing Frontend Microservices with ASP.NET Core 465
Technical requirements ........................................................................................................ 466
Front-ends and micro-frontends ........................................................................................... 466
Public web APIs • 468
HTML micro-frontends • 469
Defining the application architecture .................................................................................... 471
Defining the domain layer interface ...................................................................................... 474
Defining the domain layer implementation ........................................................................... 476
Defining the application layer ............................................................................................... 479
Defining controllers ............................................................................................................. 483
Summary ............................................................................................................................. 484
Questions ............................................................................................................................. 484
Further reading .................................................................................................................... 484
Chapter 19: Client Frameworks: Blazor 487
Technical requirements ........................................................................................................ 488
Comparison of the various types of client technologies .......................................................... 488
Single-page applications • 488
Progressive applications • 489
Native applications • 490
Cross-platform applications • 491
Blazor WebAssembly architecture ......................................................................................... 491
What is a single-page application? • 492
Loading and starting the application • 494
Routing • 496
Blazor pages and components ............................................................................................... 498
Templates and cascading parameters • 502
Error handling • 505
Events • 506
Bindings • 508
How Blazor updates HTML • 510
Component lifecycle • 510
Blazor forms and validation .................................................................................................. 513
Modifying HTML <head> content from Blazor components • 515
Blazor advanced features ...................................................................................................... 516
References to components and HTML elements • 516
JavaScript interoperability • 517
Globalization and localization • 519
Authentication and authorization • 520
Communication with the server • 523
AOT compilation • 525
Third-party tools for Blazor WebAssembly ............................................................................. 526
.NET MAUI Blazor ................................................................................................................. 527
What is .NET MAUI? • 527
Developing native applications with Blazor • 528
Summary ............................................................................................................................. 532
Questions ............................................................................................................................. 532
Further reading .................................................................................................................... 532
Chapter 20: Kubernetes 535
Technical requirements ........................................................................................................ 535
Kubernetes basics ................................................................................................................ 536
.yaml files • 539
ReplicaSets and Deployments • 540
StatefulSets • 542
Services • 544
Ingresses • 549
Interacting with Kubernetes clusters .................................................................................... 551
Creating an Azure Kubernetes cluster • 551
Using minikube • 553
Using kubectl • 554
Deploying the demo Guestbook application • 556
Advanced Kubernetes concepts ............................................................................................. 561
Requiring permanent storage • 561
Kubernetes secrets • 563
Liveness and readiness checks • 564
Autoscaling • 566
Helm – installing an Ingress Controller • 567
Summary ............................................................................................................................. 570
Questions ............................................................................................................................. 571
Further reading .................................................................................................................... 571
Chapter 21: Case Study 573
Introducing World Wild Travel Club ...................................................................................... 573
User needs and system requirements .................................................................................... 574
Main types of .NET projects used at WWTravelClub ............................................................... 576
Managing WWTravelClub’s requirements using Azure DevOps ............................................... 577
Code standard for WWTravelClub – Dos and don’ts when writing code ................................... 581
Applying SonarCloud to WWTravelClub APIs ......................................................................... 582
Reusing code as a fast way to deliver good and safe software .................................................. 585
Understanding the domains of the WWTravelClub application ............................................... 586
The WWTravelClub DevOps approach ................................................................................... 589
How to choose your data storage in the cloud ........................................................................ 590
Implementing the destinations/packages database with Cosmos DB • 591
A worker microservice with ASP.NET Core ............................................................................ 595
The specifications and architecture • 595
The storage layer • 596
The application layer • 597
Processing the queued requests • 600
Testing the GrpcMicroservice project with a fake purchase requests generator • 602
A worker microservice based on RabbitMQ ........................................................................... 604
Exposing WWTravelClub packages using Web APIs ................................................................ 613
Implementing Azure Functions to send emails ...................................................................... 618
First step — creating an Azure queue storage • 619
Second step — creating the function to send emails • 621
Third step — creating the queue trigger function • 624
A frontend microservice ....................................................................................................... 626
Defining application specifications • 626
Defining the domain layer abstraction • 630
Defining the domain layer implementation • 632
Defining the application layer • 638
Defining controllers and views • 642
Using client technologies ...................................................................................................... 650
Preparing the solution • 650
Implementing the required ASP.NET Core REST APIs • 652
Implementing the business logic in a service • 655
Implementing the user interface • 656
Adding a Blazor MAUI version • 659
Testing the WWTravelClub application .................................................................................. 660
Connecting to an Azure DevOps repository • 663
Summary ............................................................................................................................. 669
Chapter 22: Case Study Extension: Developing .NET
Microservices for Kubernetes 671
Technical requirements ........................................................................................................ 672
The Tools needed for .NET Kubernetes development ............................................................. 672
Installing and configuring SQL Server Express • 674
Enabling Kubernetes application debugging with Bridge to Kubernetes • 678
Organizing the development process ..................................................................................... 681
gRPC worker microservices revisited • 681
Adding Docker support to GrpcMicroServiceDocker • 682
Moving GrpcMicroServiceDocker to SQL Server Express • 682
Enabling communication among microservices with a Docker virtual network • 683
When to test the application with Minikube • 685
Running your application in Minikube .................................................................................. 686
Remote debugging a Kubernetes application ......................................................................... 690
Summary ............................................................................................................................. 692
Questions ............................................................................................................................. 692
Further reading .................................................................................................................... 693
Answers 695
Other Books You May Enjoy 709
A book for the aspiring .NET software architect – design scalable and high-performance enterprise solutions using the latest features of C# 12 and .NET 8
Get introduced to software architecture fundamentals and begin applying them in .NET
Explore the main technologies used by software architects and choose the best ones for your needs
Master new developments in .NET with the help of a practical case study that looks at software architecture for a travel agency
Software Architecture with C# 12 and .NET 8 puts high-level design theory to work in a .NET context, teaching you the key skills, technologies, and best practices required to become an effective .NET software architect.
This fourth edition puts emphasis on a case study that will bring your skills to life. You’ll learn how to choose between different architectures and technologies at each level of the stack. You’ll take an even closer look at Blazor and explore OpenTelemetry for observability, as well as a more practical dive into preparing .NET microservices for Kubernetes integration.
Divided into three parts, this book starts with the fundamentals of software architecture, covering C# best practices, software domains, design patterns, DevOps principles for CI/CD, and more. The second part focuses on the technologies, from choosing data storage in the cloud to implementing frontend microservices and working with Serverless. You’ll learn about the main communication technologies used in microservices, such as REST API, gRPC, Azure Service Bus, and RabbitMQ. The final part takes you through a real-world case study where you’ll create software architecture for a travel agency.
By the end of this book, you will be able to transform user requirements into technical needs and deliver highly scalable enterprise software architectures.
Program and maintain Azure DevOps and explore GitHub Projects
Manage software requirements to design functional and non-functional needs
Apply architectural approaches such as layered architecture and domain-driven design
Make effective choices between cloud-based and data storage solutions
Implement resilient frontend microservices, worker microservices, and distributed transactions
Understand when to use test-driven development (TDD) and alternative approaches
Choose the best option for cloud development, from IaaS to Serverless
This book is for engineers and senior software developers aspiring to become architects or looking to build enterprise applications with the .NET stack.
Basic familiarity with C# and .NET is required to get the most out of this software architecture book.