Pro C#10 Introduction

Pro C# 10 with .NET 6

Introduction

Choose Your Own Adventures

选择你自己的冒险

From the very first editions that Andrew wrote, and the ensuing editions since I have taken over the book, the goal has been (and always will be) to help you be productive and informed software engineers. This book, like C# and the .NET platform, continues to grow with every release. This provides an opportunity for you, the reader, to choose your own adventures. Whether you are new to software development or a seasoned veteran, this book has content to take you to the next level.
从Andrew写的第一版,到我接手这本书以来的后续版本,我们的目标一直是(并且永远是)帮助你成为高效和知情的软件工程师。本书与 C# 和 .NET 平台一样,随着每个版本的发布而不断增长。这为你(读者)提供了一个选择自己的冒险的机会。无论你是软件开发新手还是经验丰富的老手,本书都有内容将带你更上一层楼。

For the veteran C# developer, this book keeps you up to date with the latest that C# and .NET has to offer. Starting with C# 7, the section headers indicate when a feature was introduced or updated. Quickly scanning the table of contents highlights the new features so you can quickly read up on them and take them for a spin. This book can also serve as a reference manual, with code samples ready to help you complete that task that isn’t quite muscle memory.
对于资深的 C# 开发人员,本书让您了解 C# 和 .NET 提供的最新信息。从 C# 7 开始,节标题指示何时引入或更新功能。快速扫描目录会突出显示新功能,以便您可以快速阅读它们并试一试。本书还可以作为参考手册,其中包含代码示例,可帮助您完成不完全是肌肉记忆的任务。

For the beginner, this book uses an organized approach to teaching C# and object oriented programming. Once you are comfortable with those topics, you can exercise them by skipping ahead and building application ASP.NET Core or Windows Presentation Foundation. Not ready to dive into data access? All of the code for each chapter is provide in the GitHub repo, so you can skip ahead in the book and use the provided code samples to explore in the order you choose.
对于初学者,本书使用有组织的方法来教授C#和面向对象编程。熟悉这些主题后,可以通过跳过并 ASP.NET Core 或 Windows Presentation Foundation 构建应用程序来练习它们。还没准备好深入了解数据访问?每章的所有代码都在 GitHub 存储库中提供,因此您可以跳过本书并使用提供的代码示例按您选择的顺序进行探索。

As an author, I don’t know what your individual needs are at any single point in time. As a CTO and Chief Architect, I know what our organization and software engineers need for the long haul. Not only for the software they build, but for their careers as the move towards tech lead and architect positions. My goal is to provide you with the same information that I provide my team and my organization. Chances are you don’t need everything in this book – right now. Chances are also good that you will at some point in your development cycles. So, choose your own adventure, grab what you need to finish your task at hand, and know that we’ll be ready for you when you come back to tackle another section.
作为一名作者,我不知道你的个人在任何单一时间点的需求是什么。作为首席技术官和首席架构师,我知道我们的组织和软件工程师需要什么。不仅是为了他们构建的软件,也是为了他们的职业生涯,因为向技术主管和架构师职位迈进。我的目标是为您提供与我提供给我的团队和组织相同的信息。很可能你不需要这本书中的所有内容——现在。在开发周期的某个时刻,您也很有可能会这样做。因此,选择您自己的冒险,抓住完成手头任务所需的东西,并知道当您回来处理另一个部分时,我们会为您做好准备。

The Source Code

源代码

The source code for this book is hosted on GitHub in the https://github.com/apress/pro-c-sharp-10 repository.
本书的源代码托管在 GitHub 的存储库中。
https://www.Github.com/apress/pro-c- sharp-10 repository

An Overview of This Book

本书概述

Pro C# 10 with .NET 6 is logically divided into nine distinct parts, each of which contains a number of related chapters. Here is a part-by-part and chapter-by-chapter breakdown of the text.
带有 .NET 10 的 Pro C# 6 在逻辑上分为九个不同的部分,每个部分都包含许多相关章节。以下是文本的逐部分和逐章分解。

Part I: Introducing C# and .NET 6

第 I 部分:介绍 C# 和 .NET 6

The purpose of Part I is to acclimate you to the nature of the .NET platform and various development tools used during the construction of .NET applications.
第一部分的目的是让您适应 .NET 平台的性质以及在构建 .NET 应用程序期间使用的各种开发工具。

Chapter 1: Introducing C# and .NET 6

第 1 章:介绍 C# 和 .NET 6

This first chapter functions as the backbone for the remainder of the text. The primary goal of this chapter is to acquaint you with a number of .NET–centric building blocks, such as the Common Language Runtime (CLR), Common Type System (CTS), Common Language Specification (CLS), and Base Class Libraries (BCL). Here, you will take an initial look at the C# programming language, namespaces, and the .NET assembly format.
第一章是文本其余部分的主干。本章的主要目标是让您熟悉许多以 .NET 为中心的构建基块,例如公共语言运行时 (CLR)、通用类型系统 (CTS)、公共语言规范 (CLS) 和基类库 (BCL)。在这里,您将初步了解 C# 编程语言、命名空间和 .NET 程序集格式。

Chapter 2: Building C# Applications

第 2 章:构建 C# 应用程序

The goal of this chapter is to introduce you to the process of compiling C# source code files. After installing the .NET SDK and runtimes, you will learn about the completely free (and fully functional) Visual Studio Community edition as well as the extremely popular (and also free) Visual Studio Code. You learn how to create, run, and debug .NET C# applications using both Visual Studio and Visual Studio Code.
本章的目标是向您介绍编译 C# 源代码文件的过程。安装 .NET SDK 和运行时后,您将了解完全免费(且功能齐全)的 Visual Studio 社区版以及非常流行(也是免费的)Visual Studio Code。您将学习如何使用 Visual Studio 和 Visual Studio Code 创建、运行和调试 .NET C# 应用程序。

Part II: Core C# Programming

第二部分:核心 C# 编程

The topics presented in this part of the book are quite important because you will use them regardless of which type of .NET software you intend to develop (e.g., web applications, desktop GUI applications, code libraries, services, etc.). Here, you will learn about the fundamental data types of .NET, work with text manipulation, and learn the role of various C# parameter modifiers (including optional and named arguments).
本书这一部分中介绍的主题非常重要,因为无论您打算开发哪种类型的 .NET 软件(例如,Web 应用程序、桌面 GUI 应用程序、代码库、服务等),您都将使用它们。在这里,你将了解 .NET 的基本数据类型,使用文本操作,并了解各种 C# 参数修饰符(包括可选参数和命名参数)的作用。

Chapter 3: Core C# Programming Constructs, Part 1

第 3 章:核心 C# 编程构造,第 1 部分

This chapter begins your formal investigation of the C# programming language. Here, you will learn about the role of the Main() method, top-level statements, and numerous details regarding the intrinsic data types of the .NET platform and variable declaration. You will work with and manipulate textual data using System. String and System.Text.StringBuilder. You will also examine iteration and decision constructs, pattern matching, narrowing and widening operations, and the unchecked keyword.
本章开始对 C# 编程语言进行正式研究。在这里,您将了解 Main() 方法的作用、顶级语句以及有关 .NET 平台和变量声明的内部数据类型的大量详细信息。您将使用系统处理和操作文本数据。String and System.Text.StringBuilder.您还将检查迭代和决策构造、模式匹配、缩小和加宽操作以及未选中的关键字。

Chapter 4: Core C# Programming Constructs, Part 2

第 4 章:核心 C# 编程构造,第 2 部分

This chapter completes your examination of the core aspects of C#, beginning with creating and manipulating arrays of data. Next, you examine how to construct overloaded type methods and define parameters using the out, ref, and params keywords. You will also learn about the enum type, structures, and nullable data types, and you will understand the distinction between value types and reference types. Finally, you will learn about tuples.
本章完成了对 C# 核心方面的检查,从创建和操作数据数组开始。接下来,您将研究如何使用 out、ref 和 params 关键字构造重载类型方法和定义参数。您还将了解枚举类型、结构和可为 null 的数据类型,并了解值类型和引用类型之间的区别。最后,您将了解元组。

Part III: Object-Oriented Programming with C

第三部分:用C#语言进行面向对象编程

In this part, you will come to understand the core constructs of the C# language, including the details of object-oriented programming. This part will also examine how to process runtime exceptions and will dive into the details of working with strongly typed interfaces. Finally, you will learn about object lifetime and garbage collection.
在这一部分中,您将了解 C# 语言的核心结构,包括面向对象编程的细节。本部分还将研究如何处理运行时异常,并将深入探讨使用强类型接口的详细信息。最后,您将了解对象生存期和垃圾回收。

Chapter 5: Understanding Encapsulation

第 5 章:了解封装

This chapter begins your examination of object-oriented programming (OOP) using the C# programming language. After you are introduced to the pillars of OOP (encapsulation, inheritance, and polymorphism), the remainder of this chapter will show you how to build robust class types using constructors, properties, static members, constants, and read-only fields. You will also learn about partial type definitions, object initialization syntax, and automatic properties, and the chapter will wrap up with an examination of record types and record structs.
本章开始使用 C# 编程语言检查面向对象编程 (OOP)。在介绍了 OOP 的支柱(封装、继承和多态性)之后,本章的其余部分将向您展示如何使用构造函数、属性、静态成员、常量和只读字段构建健壮的类类型。您还将了解分部类型定义、对象初始化语法和自动属性,本章将最后检查记录类型和记录结构。

Chapter 6: Understanding Inheritance and Polymorphism

第6章 理解继承和多态性

Here, you will examine the remaining pillars of OOP (inheritance and polymorphism), which allow you to build families of related class types. As you do this, you will examine the role of virtual methods, abstract methods (and abstract base classes), and the nature of the polymorphic interface. Then you will explore pattern matching with the is keyword, and finally, this chapter will explain the role of the ultimate base class of the .NET platform, System.Object.
在这里,您将检查 OOP(继承和多态性)的其余支柱,它们允许您构建相关类类型的族。执行此操作时,您将检查虚拟方法、抽象方法(和抽象基类)的作用以及多态接口的性质。然后,您将探索使用 is 关键字的模式匹配,最后,本章将解释 .NET 平台的最终基类 System.Object 的作用。

Chapter 7: Understanding Structured Exception Handling

第 7 章:了解结构化异常处理

The point of this chapter is to discuss how to handle runtime anomalies in your code base through the use of structured exception handling. Not only will you learn about the C# keywords that allow you to handle such problems (try, catch, throw, when, and finally), but you will also come to understand the distinction between application-level and system-level exceptions. In addition, this chapter will show you how to set Visual Studio on break on all exceptions to debug the exceptions that escape your notice.
本章的重点是讨论如何使用结构化异常处理来处理代码库中的运行时异常。您不仅将了解允许您处理此类问题的 C# 关键字(尝试、捕获、抛出、何时和最后),而且还将了解应用程序级和系统级异常之间的区别。此外,本章将介绍如何在所有异常上设置 Visual Studio 中断,以调试逃脱您注意的异常。

Chapter 8: Working with Interfaces

第 8 章:使用接口

The material in this chapter builds upon your understanding of object-based development by covering the topic of interface-based programming. Here, you will learn how to define classes and structures that support multiple behaviors, how to discover these behaviors at runtime, and how to selectively hide particular behaviors using explicit interface implementation. In addition to creating a number of custom interfaces, you will also learn how to implement standard interfaces found within the .NET platform. You will use these to build objects that can be sorted, copied, enumerated, and compared.
本章中的材料基于您对基于对象的开发的理解,涵盖了基于接口的编程主题。在这里,您将学习如何定义支持多种行为的类和结构,如何在运行时发现这些行为,以及如何使用显式接口实现有选择地隐藏特定行为。除了创建许多自定义接口外,您还将学习如何实现 .NET 平台中的标准接口。您将使用这些来构建可以排序、复制、枚举和比较的对象。

Chapter 9: Understanding Object Lifetime

第 9 章:了解对象生存期

The final chapter of this part examines how the CLR manages memory using the .NET garbage collector. Here, you will come to understand the role of application roots, object generations, and the System.GC type. Once you understand the basics, you will examine the topics of disposable objects (using the IDisposable interface) and the finalization process (using the System.Object.Finalize() method). This chapter will also investigate the Lazy class, which allows you to define data that will not be allocated until requested by a caller. As you will see, this feature can be helpful when you want to ensure you do not clutter the heap with objects that are not actually required by your programs.
本部分的最后一章介绍 CLR 如何使用 .NET 垃圾回收器管理内存。在这里,您将了解应用程序根、对象生成和 System.GC 类型的作用。了解基础知识后,您将检查一次性对象(使用 IDisposable 接口)和定稿过程(使用 System.Object.Finalize() 方法)的主题。本章将还要调查 Lazy 类,该类允许您定义在调用方请求之前不会分配的数据。如您所见,当您想要确保不会用程序实际上不需要的对象弄乱堆时,此功能会很有帮助。

Part IV: Advanced C# Programming

第四部分:高级 C# 编程

This part of the book will deepen your understanding of the C# language by walking you through a number of more advanced (but important) concepts. Here, you will complete your examination of the .NET type system by investigating collections and generics. You will also examine a number of more advanced features of C# (e.g., extension methods, operator overloading, anonymous types, and pointer manipulation). You will then examine delegates and lambda expressions, take a first look at Language Integrated Query, and finish the section with two chapters that focus on processes and multithreaded/async programming.
本书的这一部分将通过引导您了解一些更高级(但重要)的概念来加深您对 C# 语言的理解。在这里,您将通过调查集合和泛型来完成对 .NET 类型系统的检查。您还将检查 C# 的许多更高级的功能(例如,扩展方法、运算符重载、匿名类型和指针操作)。然后,您将检查委托和 lambda 表达式,首先了解语言集成查询,并以两章结束本节,重点介绍进程和多线程/异步编程。

Chapter 10: Collections and Generics

第10章 集合和泛型

This chapter explores the topic of generics. As you will see, generic programming gives you a way to create types and type members, which contain various placeholders that can be specified by the caller. In a nutshell, generics greatly enhance application performance and type safety. Not only will you explore various generic types within the System.Collections.Generic namespace, but you will also learn how to build your own generic methods and types (with and without constraints).
本章探讨泛型的主题。如您所见,泛型编程为您提供了一种创建类型和类型成员的方法,其中包含可由调用方指定的各种占位符。简而言之,泛型极大地增强了应用程序性能和类型安全性。您不仅将探索 System.Collections.Generic 命名空间中的各种泛型类型,还将学习如何构建自己的泛型方法和类型(有和没有约束)。

Chapter 11: Advanced C# Language Features

第 11 章:高级 C# 语言功能

This chapter deepens your understanding of the C# programming language by introducing you to a number of advanced programming techniques. Here, you will learn how to overload operators and create custom conversion routines (both implicit and explicit) for your types. You will also learn how to build and interact with type indexers, as well as work with extension methods, anonymous types, partial methods, and C# pointers using an unsafe code context.
本章通过向您介绍许多高级编程技术,加深您对 C# 编程语言的理解。在这里,您将学习如何重载运算符并为类型创建自定义转换例程(隐式和显式)。您还将学习如何生成类型索引器并与之交互,以及如何使用不安全的代码上下文使用扩展方法、匿名类型、分部方法和 C# 指针。

Chapter 12: Delegates, Events, and Lambda Expressions

第 12 章:委托、事件和 Lambda 表达式

The purpose of this chapter is to demystify the delegate type. Simply put, a .NET delegate is an object that points to other methods in your application. Using this type, you can build systems that allow multiple objects to engage in a two-way conversation. After you have examined the use of .NET delegates, you will then be introduced to the C# event keyword, which you can use to simplify the manipulation of raw delegate programming. You will wrap up this chapter by investigating the role of the C# lambda operator (=>) and exploring the connection between delegates, anonymous methods, and lambda expressions.
本章的目的是揭开委托类型的神秘面纱。简而言之,.NET 委托是指向应用程序中其他方法的对象。使用此类型,您可以构建允许多个对象进行双向对话的系统。检查 .NET 委托的用法后,将向您介绍 C# 事件关键字,该关键字可用于简化原始委托编程的操作。您将通过研究 C# lambda 运算符 (=>) 的角色并探索委托、匿名方法和 lambda 表达式之间的联系来结束本章。

Chapter 13: LINQ to Objects

第 13 章:LINQ to 对象

This chapter begins your examination of Language Integrated Query (LINQ). LINQ allows you to build strongly typed query expressions that can be applied to a number of LINQ targets to manipulate data in the broadest sense of the word. Here, you will learn about LINQ to Objects, which allows you to apply LINQ expressions to containers of data (e.g., arrays, collections, and custom types). This information will serve you well as you encounter a number of additional LINQ APIs throughout the remainder of this book.
本章开始研究语言集成查询 (LINQ)。LINQ 允许您生成强类型查询表达式,这些表达式可应用于多个 LINQ 目标,以最广泛的意义上操作数据。在这里,您将了解 LINQ to Objects,它允许您将 LINQ 表达式应用于数据容器(例如,数组、集合和自定义类型)。此信息将很好地帮助您在本书的其余部分中遇到许多其他 LINQ API。

Chapter 14: Processes, AppDomains, and Load Contexts

第 14 章:进程、应用程序域和加载上下文

Now that you have a solid understanding of assemblies, this chapter dives deeper into the composition of a loaded .NET Core executable. The goal of this chapter is to illustrate the relationship between processes,application domains, and contextual boundaries. These topics provide the proper foundation for Chapter 15, where you will examine the construction of multithreaded applications.
现在,你已对程序集有了深入的了解,本章将更深入地介绍加载的 .NET Core 可执行文件的组合。本章的目标是说明过程之间的关系,应用程序域和上下文边界。这些主题为第 15 章提供了适当的基础,您将在其中研究多线程应用程序的构造。

Chapter 15: Multithreaded, Parallel, and Async Programming

第15章 多线程、并行和异步编程

This chapter examines how to build multithreaded applications and illustrates a number of techniques you can use to author thread-safe code. The chapter opens by revisiting the .NET delegate type to ensure explaining a delegate’s intrinsic support for asynchronous method invocations. Next, you will investigate the types within the System.Threading namespace. The next section covers the Task Parallel Library (TPL). Using the TPL, .NET developers can build applications that distribute their workload across all available CPUs in a wickedly simple manner. At this point, you will also learn about the role of Parallel LINQ, which provides a way to create LINQ queries that scale across multiple machine cores. The remainder of the chapter covers creating nonblocking calls using the async/await keywords, local functions and generalized async return types, and asynchronous streams, and the ForEachAsync() method.
本章介绍如何构建多线程应用程序,并说明了可用于编写线程安全代码的许多技术。本章首先重新访问 .NET 委托类型,以确保解释委托对异步方法调用的固有支持。接下来,您将调查
命名空间中的类型。下一节将介绍任务并行库 (TPL)。使用 TPL,.NET 开发人员可以构建应用程序,以非常简单的方式在所有可用的 CPU 之间分配其工作负载。此时,您还将了解并行 LINQ 的作用,它提供了一种创建跨多个计算机核心缩放的 LINQ 查询的方法。本章的其余部分介绍如何使用 async/await 关键字、本地函数和通用异步返回类型、异步流以及 ForEachAsync() 方法创建非阻塞调用。

Part V: Programming with .NET Core Assemblies

第 V 部分:使用 .NET Core 程序集进行编程
Part V dives into the details of the .NET assembly format. Not only will you learn how to deploy and configure .NET code libraries, but you will also come to understand the internal composition of a .NET binary image. This section explains the role of .NET attributes and the role of resolving type information at runtime and the role of the Dynamic Language Runtime (DLR) and the C# dynamic keyword. The final chapter covers the syntax of Common Intermediate Language (CIL) and the role of dynamic assemblies.
第 V 部分深入介绍了 .NET 程序集格式的详细信息。您不仅将学习如何部署和配置 .NET 代码库,而且还将了解 .NET 二进制映像的内部组合。本节介绍 .NET 属性的作用和在运行时解析类型信息的角色,以及动态语言运行时 (DLR) 和 C# 动态关键字的角色。最后一章介绍公共中间语言 (CIL) 的语法和动态程序集的作用。

Chapter 16: Building and Configuring Class Libraries

第 16 章:构建和配置类库

At a high level, assembly is the term used to describe a binary file created with a .NET compiler. However, the true story of .NET assemblies is far richer than that. Here, you will learn how to build and deploy assemblies and learn the difference between class libraries and console applications. The final section covers the new options available in .NET, such as single file executables and ready-to-run publishing.
在高级别上,程序集是用于描述使用 .NET 编译器创建的二进制文件的术语。但是,.NET 程序集的真实故事远不止于此。在这里,您将学习如何生成和部署程序集,并了解类库和控制台应用程序之间的区别。最后一部分介绍 .NET 中可用的新选项,例如单个文件可执行文件和随时可以运行的发布。

Chapter 17: Type Reflection, Late Binding, Attribute, and Dynamic Types

第 17 章:类型反射、后期绑定、属性和动态类型

This chapter continues your examination of .NET assemblies by checking out the process of runtime type discovery using the System.Reflection namespace. Using the types of this namespace, you can build applications that can read an assembly’s metadata on the fly. You will also learn how to load and create types at runtime dynamically using late binding. The next topic of this chapter will explore the role of .NET attributes (both standard and custom). To illustrate the usefulness of each of these topics, the chapter shows you how to construct an extendable application complete with snap-ins. .NET 4.0 introduced a new aspect of the .NET runtime environment called the Dynamic Language Runtime. Using the DLR and the C# dynamic keyword, you can define data that is not truly resolved until runtime. Using these features simplifies some complex .NET programming tasks dramatically. In this final topic of the chapter, you will learn some practical uses of dynamic data, including how to leverage the .NET reflection APIs in a streamlined manner.
本章通过检查使用 System.Reflection 命名空间的运行时类型发现过程来继续检查 .NET 程序集。使用此命名空间的类型,可以生成可以动态读取程序集元数据的应用程序。您还将学习如何使用后期绑定在运行时动态加载和创建类型。本章的下一主题将探讨 .NET 属性(标准属性和自定义属性)的作用。为了说明其中每个主题的有用性,本章介绍如何构造一个带有管理单元的可扩展应用程序。.NET 4.0 引入了 .NET 运行时环境的新方面,称为动态语言运行时。使用 DLR 和 C#
动态关键字,您可以定义直到运行时才真正解析的数据。使用这些功能可以大大简化一些复杂的 .NET 编程任务。在本章的最后一个主题中,您将学习动态数据的一些实际用法,包括如何以简化的方式利用 .NET 反射 API。

Chapter 18: Understanding CIL and the Role of Dynamic Assemblies

第18章 了解CIL和动态装配体的作用
The goal of the final chapter in this section is twofold. The first part examines the syntax and semantics of CIL in much greater detail than in previous chapters. The remainder of this chapter will cover the role of the System.Reflection.Emit namespace. You can use these types to build software that can generate .NET Core assemblies in memory at runtime. Formally speaking, assemblies defined and executed in memory are termed dynamic assemblies.
本节最后一章的目标是双重的。第一部分比前几章更详细地检查了 CIL 的语法和语义。本章的其余部分将介绍 System.Reflection.Emit 命名空间的角色。可以使用这些类型来生成可生成 .NET 的软件运行时内存中的核心程序集。从形式上讲,在内存中定义和执行的程序集称为动态程序集。

Part VI: File Handling, Object Serialization, and Data Access

第 VI 部分:文件处理、对象序列化和数据访问

By this point in the text, you have a solid handle on the C# language and the details of the .NET assembly format. Part VI leverages your newfound knowledge by exploring a number of commonly used services found within the base class libraries, including file I/O, object serialization, and database access using ADO.NET.
到文本中的这一点,你已经掌握了 C# 语言和 .NET 程序集格式的详细信息。第 VI 部分通过探索基类库中的许多常用服务(包括文件 I/O、对象序列化和使用 ADO.NET 访问数据库)来利用您新发现的知识。

Chapter 19: File I/O and Object Serialization

第 19 章:文件 I/O 和对象序列化

The System.IO namespace allows you to interact with a machine’s file and directory structure. Over the course of this chapter, you will learn how to create (and destroy) a directory system programmatically. You will also learn how to move data into and out of various streams (e.g., file based, string based, and memory based). The latter part of this chapter will examine the XML and JSON object serialization services of the .NET platform. Simply put, serialization allows you to persist the public state of an object (or a set of related objects) into a stream for later use. Deserialization (as you might expect) is the process of plucking an object from the stream into memory for consumption by your application.
System.IO 命名空间允许您与计算机的文件和目录结构进行交互。在本章中,您将学习如何以编程方式创建(和销毁)目录系统。您还将学习如何将数据移入和移出各种流(例如,基于文件、基于字符串和基于内存)。本章的后半部分将研究.NET 平台。简而言之,序列化允许您将一个对象(或一组相关对象)的公共状态保存到流中以供以后使用。反序列化(如您所料)是将对象从流中提取到内存以供应用程序使用的过程。

Chapter 20: Data Access with ADO.NET

第 20 章:ADO.NET 数据访问

This chapter covers database access using ADO.NET, the database API for .NET applications. Specifically, this chapter will introduce you to the role of .NET data providers and how to communicate with a relational database using ADO.NET, which is represented by connection objects, command objects, transaction objects, and data reader objects. This chapter also begins the creation of the AutoLot database, which will be enhanced in Part VII.
本章介绍如何使用 ADO.NET(适用于 .NET 应用程序的数据库 API)进行数据库访问。具体而言,本章将介绍 .NET 数据提供程序的角色以及如何使用 ADO.NET(由连接对象、命令对象、事务对象和数据读取器对象表示)与关系数据库进行通信。本章还开始创建 AutoLot 数据库,该数据库将在第七部分中得到增强。

Part VII: Entity Framework Core

第七部分:实体框架核心

By this point in the text, you have a solid handle on the C# language and the details of the .NET assembly format. Part VI leverages your newfound knowledge by exploring a number of commonly used services found within the base class libraries, including file I/O, database access using ADO.NET, and database access using Entity Framework Core.
到文本中的这一点,你已经掌握了 C# 语言和 .NET 程序集格式的详细信息。第 VI 部分通过探索基类库中找到的许多常用服务(包括文件 I/O、使用 ADO.NET 的数据库访问和使用实体框架核心的数据库访问)来利用您新发现的知识。

Chapter 21: Introducing Entity Framework Core

第 21 章:实体框架核心简介

This chapter introduces Entity Framework (EF) Core. EF Core is an object-relational mapping (ORM) framework built on top of ADO.NET. EF Core provides a way to author data access code using strongly typed classes that directly map to your business model. Here, you will come to understand the building blocks of EF Core, including DbContext, entities, the specialized collection class DbSet, and the DbChangeTracker. Next, you will learn about building your data model understanding EF Core conventions, data annotations, and the Fluent API. The next sections cover query execution and tracking versus non-tracking queries. The final piece of this chapter is the EF Core global tool for the .NET Core command-line interface (CLI).
本章介绍实体框架 (EF) 核心。EF Core 是一个基于 ADO.NET 构建的对象关系映射 (ORM) 框架。EF Core 提供了一种使用直接映射到业务模型的强类型类创作数据访问代码的方法。在这里,您将了解 EF Core 的构建基块,包括 DbContext、实体、专用集合类 DbSet 和 DbChangeTracker。接下来,你将了解如何构建数据模型,了解 EF Core 约定、数据批注和 Fluent API。接下来的部分介绍查询执行和跟踪与非跟踪查询。本章的最后一部分是用于 .NET Core 命令行界面 (CLI) 的 EF Core 全局工具。

Chapter 22: Exploring Entity Framework Core

第 22 章:探索实体框架核心

This chapter continues exploring EF Core. The chapter begins by diving deep into create, read, update, and delete (CRUD) operations. The rest of the chapter covers some of the more notable features of EF Core, including global query filters, raw SQL queries with LINQ, projections, database generated values, concurrency checking, connection resiliency, database function mapping, batching of statements, value converters, and shadow properties. The final section covers SQL Server temporal table support, the newest feature in EF Core 6.
本章继续探讨 EF Core。本章首先深入探讨创建、读取、更新和删除 (CRUD) 操作。本章的其余部分介绍 EF Core 的一些更值得注意的功能,包括全局查询筛选器、使用 LINQ 的原始 SQL 查询、投影、数据库生成的值、并发检查、连接复原、数据库函数映射、语句批处理、值转换器和影子属性。最后一部分介绍 SQL Server 临时表支持,这是 EF Core 6 中的最新功能。

Chapter 23: Build a Data Access Layer with Entity Framework Core

第 23 章:使用实体框架核心构建数据访问层

This chapter builds the AutoLot data access layer. It begins with scaffolding the AutoLot database from Chapter 20 into a derived DbContext and entity classes. Then the project and database are updated to change to a code first approach. The entities are updated to their final version, and migration are used to update the database tables and add SQL Server objects. The final database change is to create a migration for the stored procedure from Chapter 21 and a new database view. The next section builds a rich set of repositories for code encapsulation, and the final update is to add data initialization code.
本章构建 AutoLot 数据访问层。它首先将第 20 章中的 AutoLot 数据库基架构建 为派生的 DbContext 和实体类。然后更新项目和数据库以更改为代码优先方法。实体将更新到其最终版本,迁移用于更新数据库表和添加 SQL Server 对象。最后一个数据库更改是从第 21 章为存储过程创建一个迁移 和一个新的数据库视图。下一节为代码封装构建一组丰富的存储库,最终更新是添加数据初始化代码。

Chapter 24: Test Driving the AutoLot

第24章 Test Driving the AutoLot

Chapter 24 uses the xUnit testing framework to build automated integration tests for the AutoLot data access layer. In this chapter over 60 tests are used to explore querying, creating, updating, and deleting records.
第24章使用xUnit测试框架为AutoLot数据访问层构建自动化集成测试。在本章中,使用了 60 多个测试来探索查询、创建、更新和删除记录。

Part VIII: Windows Presentation Foundation

第八部分:WPF基础

The initial desktop GUI API supported by the .NET platform was termed Windows Forms. While this API is still fully supported, .NET 3.0 introduced programmers to an API called Windows Presentation Foundation (WPF). Unlike Windows Forms, this framework integrates a number of key services, including data binding, 2D and 3D graphics, animations, and rich documents, into a single, unified object model. This is all accomplished using a declarative markup grammar called Extensible Application Markup Language (XAML). Furthermore,the WPF control architecture provides a trivial way to restyle the look and feel of a typical control radically using little more than some well-formed XAML.
.NET 平台支持的初始桌面 GUI API 称为 Windows Forms。虽然此 API 仍完全受支持,但 .NET 3.0 向程序员介绍了一个名为 Windows Presentation Foundation (WPF) 的 API。与 Windows 窗体不同,此框架将许多关键服务(包括数据绑定、二维和三维图形、动画和丰富文档)集成到单个统一的对象模型中。这一切都是使用称为可扩展应用程序标记语言 (XAML) 的声明性标记语法完成的。此外WPF 控件体系结构提供了一种简单的方法来重新设计典型控件的外观,从根本上只使用一些格式良好的 XAML。

Chapter 25: Introducing Windows Presentation Foundation and XAML

第 25 章:介绍 Windows Presentation Foundation 和 XAML

In this chapter, you will begin by examining the motivation behind the creation of WPF (when there was already a desktop development framework in .NET). Then, you will learn about the syntax of XAML and, finally, take a look at the Visual Studio support for building WPF applications.
在本章中,您将首先检查创建 WPF 背后的动机(当时 .NET 中已经有一个桌面开发框架)。然后,您将了解 XAML 的语法,最后,了解 Visual Studio 对生成 WPF 应用程序的支持。

Chapter 26: WPF Controls, Layouts, Events, and Data Binding

第 26 章:WPF 控件、布局、事件和数据绑定
This chapter will expose you to the process of using intrinsic WPF controls and layout managers. For example, you will learn to build menu systems, splitter windows, toolbars, and status bars. This chapter will also introduce you to a number of WPF APIs (and their related controls), including the Ink API, commands, routed events, the data-binding model, and dependency properties.
本章将向您介绍使用内部 WPF 控件和布局管理器的过程。例如,您将学习构建菜单系统、拆分器窗口、工具栏和状态栏。本章还将介绍许多 WPF API(及其相关控件),包括 Ink API、命令、路由事件、数据绑定模型和依赖项属性。

Chapter 27: WPF Graphics Rendering Services

第 27 章:WPF 图形呈现服务

WPF is a graphically intensive API; given this fact, WPF provides three ways to render graphics: shapes, drawings and geometrics, and visuals. In this chapter, you will evaluate each option and learn about a number of important graphics primitives (e.g., brushes, pens, and transformations) along the way. This chapter will also examine ways to incorporate vector images into your WPF graphics, as well as how to perform hit-testing operations against graphical data.
WPF 是一个图形密集型 API;鉴于这一事实,WPF 提供了三种呈现图形的方法:形状、绘图和几何以及视觉对象。在本章中,您将评估每个选项,并在此过程中了解许多重要的图形基元(例如,画笔、笔和变换)。本章还将研究将矢量图像合并到 WPF 图形中的方法,以及如何对图形数据执行命中测试操作。

Chapter 28: WPF Resources, Animations, Styles, and Templates

第 28 章:WPF 资源、动画、样式和模板

This chapter will introduce you to three important (and interrelated) topics that will deepen your understanding of the Windows Presentation Foundation API. The first order of business is to learn the role of logical resources. As you will see, the logical resource (also termed an object resource) system provides a way for you to name and refer to commonly used objects within a WPF application. Next, you will learn how to define, execute, and control an animation sequence. Despite what you might be thinking, however, WPF animations are not limited to the confines of video games or multimedia applications. You will wrap up the chapter by learning about the role of WPF styles. Similar to a web page that uses CSS or the ASP.NET theme engine, a WPF application can define a common look and feel for a set of controls.
本章将向您介绍三个重要(且相互关联)的主题,这些主题将加深您对 Windows Presentation Foundation API 的理解。业务的首要任务是学习逻辑资源的作用。如您所见,逻辑资源(也称为对象资源)系统提供了一种命名和引用 WPF 应用程序中常用对象的方法。接下来,您将学习如何定义、执行和控制动画序列。但是,不管您可能在想什么,WPF 动画并不局限于视频游戏或多媒体应用程序。您将通过了解 WPF 样式的作用来结束本章。与使用 CSS 或 ASP.NET 主题引擎的网页类似,WPF 应用程序可以为一组控件定义通用外观。

Chapter 29: WPF Notifications, Validations, Commands, and MVVM

第 29 章:WPF 通知、验证、命令和 MVVM

This chapter begins by examining three core WPF framework capabilities: notifications, validations, and commands. In the notifications section, you will learn about observable models and collections and how they keep your application data and UI in sync. Next, you will dig deeper into commands, building custom commands to encapsulate your code. In the validations section, you will learn how to use the several validation mechanisms available in WPF applications. The chapter closes with an examination of the Model- View-ViewModel (MVVM) pattern and ends by creating an application that demonstrates the MVVM pattern in action.
本章首先介绍三个核心 WPF 框架功能:通知、验证和命令。在通知部分中,你将了解可观察的模型和集合,以及它们如何使应用程序数据和 UI 保持同步。接下来,您将深入挖掘命令,构建自定义命令来封装代码。在验证部分中,您将学习如何使用 WPF 应用程序中提供的几种验证机制。本章以对模型-视图-视图模型 (MVVM) 模式的检查结束,最后创建一个演示 MVVM 模式运行的应用程序。

Part IX: ASP.NET Core

第九部分: ASP.NET Core

Part VIII is devoted to an examination of constructing web applications using ASP.NET Core. The chapters in this section cover ASP.NET Core fundamentals as well as build a RESTful service, a web application using the MVC pattern, and a Razor page based web application.
第八部分专门研究使用 ASP.NET 核心构建Web应用程序。本节中的章节涵盖 ASP.NET 核心基础知识,以及构建 RESTful 服务、使用 MVC 模式的 Web 应用程序和基于 Razor 页面的 Web 应用程序。

Chapter 30: Introducing ASP.NET Core

第 30 章:ASP.NET Core 简介

This chapter introduces ASP.NET Core. After describing the Model-View-Controller (MVC) pattern, the solution and the three ASP.NET Core projects are created and the multiple ways to run and debug application is explored. Next, the chapter covers many of the features from ASP.NET MVC/ASP.NET WebAPI that were brought forward into ASP.NET Core. These include convention over configuration, controllers and actions, routing, model binding and validation, and filters.
本章介绍 ASP.NET Core。在描述模型-视图-控制器 (MVC) 模式之后,将创建解决方案和三个 ASP.NET 核心项目,以及运行和调试的多种方式探索应用。接下来,本章将介绍 MVC/ASP.NET WebAPI 中的许多功能 ASP.NET 这些功能被引入 ASP.NET Core。其中包括配置约定、控制器和操作、路由、模型绑定和验证以及筛选器。

Chapter 31: Diving into ASP.NET Core

第31章 潜入ASP.NET Core

The chapter covers the many new features introduced in ASP.NET Core, including Razor pages, the environmentally aware configuration system, built-in dependency injection and the options pattern, the HTTP client factory, deployment patterns, the HTTP request pipeline, and logging.
本章介绍了 ASP.NET Core 中引入的许多新功能,包括 Razor 页面、环境感知配置系统、内置依赖项注入和选项模式、HTTP 客户端工厂、部署模式、HTTP 请求管道和日志记录。

Chapter 32: RESTful Services with ASP.NET Core

第 32 章: ASP.NET Core REST 服务

This chapter finishes the ASP.NET Core RESTful service application. The chapter begins with a look at returning JSON from action methods and JSON configuration options. The functionality for API controllers that the ApiAttribute adds is explored. API Versioning is covered next, and the Swagger/OpenAPI configuration is updated to support versioned APIs. A base controller to provide standard CRUD operations is created and the entity specific controllers are added. An exception filter is added and basic authentication is added to the service.
本章完成了 ASP.NET Core RESTful 服务应用程序。本章首先介绍如何从操作方法和 JSON 配置选项返回 JSON。本文探讨了 ApiAttribute 添加的 API 控制器的功能。接下来将介绍 API 版本控制,并更新 Swagger/OpenAPI 配置以支持版本控制的 API。将创建用于提供标准 CRUD 操作的基本控制器,并添加特定于实体的控制器。将添加异常筛选器,并将基本身份验证添加到服务中。

Chapter 33: Web Applications with MVC

第 33 章:使用 MVC 的 Web 应用程序

This chapter finishes the MVC-based web application. The chapter starts with a deep look into views and the Razor View Engine, including layouts and partials. Next, managing client-side libraries and the bundling/ minification of those libraries is covered. The base controller is built along with the derived entity specific controller. An Area is added to the application for managing Make records. Next tag helpers (another new feature in ASP.NET Core) are explored, followed by creating custom tag helpers. The application’s view component is added for the dynamic menu. Two custom validation attributes and their related server and client-side code are used to provide validation to a view model. The final section covers the General Data Protection Regulation (GDPR) support in ASP.NET Core.
本章将完成基于 MVC 的 Web 应用程序。本章首先深入探讨视图和 Razor 视图引擎,包括布局和部分。接下来,介绍管理客户端库以及这些库的捆绑/缩小。基本控制器与派生的实体特定控制器一起构建。区域将添加到用于管理创建记录的应用程序中。接下来将探索标记帮助程序(ASP.NET Core 中的另一个新功能),然后创建自定义标记帮助程序。为动态菜单添加了应用程序的视图组件。两个自定义验证属性及其相关的服务器和客户端代码用于为视图模型提供验证。最后一部分介绍了 ASP.NET Core 中的通用数据保护条例 (GDPR) 支持。

Chapter 34: Web Applications Using Razor Pages

第 34 章:使用 Razor 页面的 Web 应用程序

The chapter starts with a deep look into Razor pages and Razor page views and then completes the AutoLot. Web application. Many of the MVC application capabilities, like layouts, partial views, tag helpers, view components, GDPR support, and areas are supported in Razor page applications. All of the features of the AutoLot.Mvc application is replicated in the AutoLot.Web application, leveraging the similarities and highlighting the differences between MVC and Razor pages.
本章首先深入了解 Razor 页面和 Razor 页面视图,然后完成 AutoLot。网络应用程序。Razor 页面应用程序支持许多 MVC 应用程序功能,如布局、分部视图、标记帮助程序、视图组件、GDPR 支持和区域。的所有功能AutoLot.Mvc 应用程序在 AutoLot.Web 应用程序中复制,利用 MVC 和 Razor 页面之间的相似之处并突出显示差异。

发表评论