Code Like a Pro in C#....1
contents....5
preface....11
acknowledgments....13
about this book....15
Who should read this book....15
How this book is organized: A roadmap....16
About the code....17
liveBook discussion forum....18
about the author....19
about the cover illustration....20
Part 1—Using C# and .NET....21
1 Introducing C# and .NET....23
1.1 Why work in C#?....25
1.1.1 Reason 1: C# is economical....26
1.1.2 Reason 2: C# is maintainable....26
1.1.3 Reason 3: C# is developer friendly and easy to use....27
1.2 Why not work in C#?....27
1.2.1 Operating system development....28
1.2.2 Real-time operating system embedded development in C#....28
1.2.3 Numerical computing and C#....29
1.3 Switching to C#....29
1.4 What you will learn in this book....32
1.5 What you will not learn in this book....32
Summary....34
2 .NET and how it compiles....35
2.1 What is the .NET Framework?....36
2.2 What is .NET 5?....37
2.3 How CLI-compliant languages are compiled....39
2.3.1 Step 1: C# code (high-level)....40
2.3.2 Step 2: Common Intermediate Language (assembly level)....43
2.3.3 Step 3: Native code (processor level)....49
Exercises....51
Summary....52
Part 2—The existing codebase....53
3 How bad is this code?....55
3.1 Introducing Flying Dutchman Airlines....56
3.2 Pieces of the puzzle: Taking a look at our requirements....58
3.2.1 Object-relational mapping....58
3.2.2 The GET /flight endpoint: Retrieving information on all flights....59
3.2.3 The GET /flight/{flightNumber} endpoint: Getting specific flight information....60
3.2.4 The POST /booking/{flightNumber} endpoint: Booking a flight....61
3.3 Coming to terms with the existing codebase....63
3.3.1 Assessing the existing database schema and its tables....63
3.3.2 The existing codebase: Web service configuration files....64
3.3.3 Considering models and views in the existing codebase....70
Summary....77
4 Manage your unmanaged resources!....78
4.1 The FlightController: Assessing the GET /flight endpoint....80
4.1.1 The GET /flight endpoint and what it does....80
4.1.2 Method signature: The meaning of ResponseType and typeof....81
4.1.3 Collecting flight information with collections....83
4.1.4 Connection strings, or how to give a security engineer a heart attack....84
4.1.5 Using IDisposable to release unmanaged resources....85
4.1.6 Querying a database with SqlCommand....87
4.2 The FlightController: Assessing GET /flight/{flightNumber}....90
4.3 The FlightController: POST /flight....93
4.4 The FlightController: DELETE /flight/{flightNumber}....97
Exercises....98
Summary....99
Part 3—The database access layer....101
5 Setting up a project and database with Entity Framework Core....103
5.1 Creating a .NET 5 solution and project....104
5.2 Setting up and configuring a web service....108
5.2.1 Configuring a .NET 5 web service....109
5.2.2 Creating and using HostBuilder....111
5.2.3 Implementing the Startup class....113
5.2.4 Using the repository/service pattern for our web service architecture....116
5.3 Implementing the database access layer....118
5.3.1 Entity Framework Core and reverse-engineering....118
5.3.2 DbSet and the Entity Framework Core workflow....120
5.3.3 Configuration methods and environment variables....122
5.3.4 Setting an environment variable on Windows....123
5.3.5 Setting an environment variable on macOS....124
5.3.6 Retrieving environment variables at run time in your code....124
Exercises....127
Summary....127
Part 4—The repository layer....129
6 Test-driven development and dependency injection....131
6.1 Test-driven development....133
6.2 The CreateCustomer method....137
6.2.1 Why you should always validate input arguments....138
6.2.2 Using “arrange, act, assert” to write unit tests....139
6.2.3 Validating against invalid characters....140
6.2.4 In-lining test data with the [DataRow] attribute....143
6.2.5 Object initializers and autogenerated code....143
6.2.6 Constructors, reflection, and asynchronous programming....145
6.2.7 Locks, mutexes, and semaphores....147
6.2.8 Synchronous to asynchronous execution . . . continued....149
6.2.9 Testing Entity Framework Core....150
6.2.10 Controlling dependencies with dependency injection....152
Summary....159
7 Comparing objects....160
7.1 The GetCustomerByName method....161
7.1.1 Question marks: Nullable types and their applications....163
7.1.2 Custom exceptions, LINQ, and extension methods....164
7.2 Congruence: From the Middle Ages to C#....169
7.2.1 Creating a “comparer” class using EqualityComparer....170
7.2.2 Testing equality by overriding the Equals method....173
7.2.3 Overloading the equality operator....174
Exercises....177
Summary....178
8 Stubbing, generics, and coupling....180
8.1 Implementing the Booking repository....181
8.2 Input validation, separation of concerns, and coupling....184
8.3 Using object initializers....189
8.4 Unit testing with stubs....192
8.5 Programming with generics....196
8.6 Providing default arguments by using optional parameters....197
8.7 Conditionals, Func, switches, and switch expressions....199
8.7.1 The ternary conditional operator....200
8.7.2 Branching using an array of functions....201
8.7.3 Switch statements and expressions....201
8.7.4 Querying for pending changes in Entity Framework Core....203
Exercises....205
Summary....206
9 Extension methods, streams, and abstract classes....208
9.1 Implementing the Airport repository....209
9.2 Getting an Airport out of the database by its ID....210
9.3 Validating the AirportID input parameter....212
9.4 Output streams and being specifically abstract....214
9.5 Querying the database for an Airport object....219
9.6 Implementing the Flight repository....226
9.6.1 The IsPositive extension method and “magic numbers”....228
9.6.2 Getting a flight out of the database....234
Exercises....236
Summary....237
Part 5—The service layer....239
10 Reflection and mocks....241
10.1 The repository/service pattern revisited....242
10.1.1 What is the use of a service class?....243
10.2 Implementing the CustomerService....245
10.2.1 Setting up for success: Creating skeleton classes....245
10.2.2 How to delete your own code....247
10.3 Implementing the BookingService....249
10.3.1 Unit testing across architectural layers....254
10.3.2 The difference between a stub and a mock....255
10.3.3 Mocking a class with the Moq library....256
10.3.4 Calling a repository from a service....263
Exercises....265
Summary....266
11 Runtime type checking revisited and error handling....268
11.1 Validating input parameters of a service layer method....269
11.1.1 Runtime type checks with the is and as operators....273
11.1.2 Type checking with the is operator....273
11.1.3 Type checking with the as operator....275
11.1.4 What did we do in section 11.1?....275
11.2 Cleaning up the BookingServiceTests class....276
11.3 Foreign key constraints in service classes....278
11.3.1 Calling the Flight repository from a service class....279
Exercises....290
Summary....292
12 Using IAsyncEnumerable and yield return....293
12.1 Do we need an AirportService class?....294
12.2 Implementing the FlightService class....296
12.2.1 Getting information on a specific flight from the FlightRepository....296
12.2.2 Combining two data streams into a view....300
12.2.3 Using the yield return keywords with try-catch code blocks....308
12.2.4 Implementing GetFlightByFlightNumber....312
Exercises....318
Summary....319
Part 6—The controller layer....321
13 Middleware, HTTP routing, and HTTP responses....323
13.1 The controller class within the repository/service pattern....324
13.2 Determining what controllers to implement....326
13.3 Implementing the FlightController....328
13.3.1 Returning HTTP responses with the IActionResult interface (GetFlights)....329
13.3.2 Injecting dependencies into a controller using middleware....332
13.3.3 Implementing the GET /Flight/{FlightNumber} endpoint....340
13.4 Routing HTTP requests to controllers and methods....344
Exercises....350
Summary....350
14 JSON serialization/ deserialization and custom model binding....352
14.1 Implementing the BookingController class....353
14.1.1 Introduction to data deserialization....355
14.1.2 Using the [FromBody] attribute to deserialize incoming HTTP data....359
14.1.3 Using a custom model binder and method attribute for model binding....360
14.1.4 Implementing the CreateBooking endpoint method logic....363
14.2 Acceptance testing and Swagger middleware....369
14.2.1 Manual acceptance testing with an OpenAPI specification....369
14.2.2 Generating an OpenAPI specification at runtime....374
14.3 The end of the road....380
Summary....381
Appendix A—Exercise answers....383
Chapter 2: .NET and how it compiles....383
Chapter 4: Manage your unmanaged resources!....384
Chapter 5: Setting up a project and database with Entity Framework Core....384
Chapter 6: Test-driven development and dependency injection....385
Chapter 7: Comparing objects....386
Chapter 8: Stubbing, generics, and coupling....387
Chapter 9: Extension methods, streams, and abstract classes....387
Chapter 10: Reflection and mocks....388
Chapter 11: Runtime type checking revisited and error handling....389
Chapter 12: Using IAsyncEnumerable and yield return....389
Chapter 13: Middleware, HTTP routing, and HTTP responses....390
Appendix B—Clean code checklist....391
Appendix C—Installation guides....393
Appendix D—OpenAPI FlyTomorrow....397
Appendix E—Reading list....400
index....405
Symbols....405
A....405
B....406
C....406
D....407
E....408
F....408
G....409
H....409
I....410
J....410
K....411
L....411
M....411
N....412
O....412
P....412
Q....413
R....413
S....414
T....415
U....415
V....416
W....416
Y....416
Code Like a Pro in C# teaches you to how write clean C# code that’s suitable for enterprise applications. In this book, you’ll refactor a legacy codebase by applying modern C# techniques. You’ll explore tools like Entity Framework Core, design techniques like dependency injection, and key practices like testing and clean coding. It’s a perfect path to upgrade your existing C# skills or shift from another OO language into C# and the .NET ecosystem.
For developers experienced with object-oriented programming. No C# experience required.