ISBN-10:
1617298026
ISBN-13:
9781617298028
Pub. Date:
Publisher:
Code like a Pro in C#

Code like a Pro in C#

by Jort Rodenburg

Paperback

$59.99
View All Available Formats & Editions
Choose Expedited Shipping at checkout for delivery by Wednesday, October 27

Overview

Build on your existing programming skills and upskill to professional-level C# programming.

Summary
In Code Like A Pro in C# you will learn:

Unit testing and test-driven development
Refactor a legacy .NET codebase
Principles of clean code
Essential backend architecture skills
Query and manipulate databases with LINQ and Entity Framework Core

Critical business applications worldwide are written in the versatile C# language and the powerful .NET platform, running on desktops, cloud systems, and Windows or Linux servers. Code Like a Pro in C# makes it easy to turn your existing abilities in C# or another OO language (such as Java) into practical C# mastery. There’s no “Hello World” or Computer Science 101 basics—you’ll learn by refactoring an out-of-date legacy codebase, using new techniques, tools, and best practices to bring it up to modern C# standards.

Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications.

About the technology
You know the basics, now get ready for the next step! Pro-quality C# code is efficient, clean, and fast. Whether you’re building user-facing business applications or writing data-intensive backend services, the experience-based, practical techniques in this book will take your C# skills to a new level.

About the book
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.

What's inside

Unit testing and test-driven development
Refactor a legacy .NET codebase
Principles of clean code
Query and manipulate databases with LINQ and Entity Framework Core

About the reader
For developers experienced with object-oriented programming. No C# experience required.

About the author
Jort Rodenburg is a software engineer who has taught numerous courses on getting up to speed with C# and .NET.

Table of Contents

PART 1 USING C# AND .NET
1 Introducing C# and .NET
2 .NET and how it compiles
PART 2 THE EXISTING CODEBASE
3 How bad is this code?
4 Manage your unmanaged resources!
PART 3 THE DATABASE ACCESS LAYER
5 Setting up a project and database with Entity Framework Core
PART 4 THE REPOSITORY LAYER
6 Test-driven development and dependency injection
7 Comparing objects
8 Stubbing, generics, and coupling
9 Extension methods, streams, and abstract classes
PART 5 THE SERVICE LAYER
10 Reflection and mocks
11 Runtime type checking revisited and error handling
12 Using IAsyncEnumerable and yield return
PART 6 THE CONTROLLER LAYER
13 Middleware, HTTP routing, and HTTP responses
14 JSON serialization/deserialization and custom model binding


Related collections and offers

Product Details

ISBN-13: 9781617298028
Publisher: Manning
Publication date: 07/27/2021
Pages: 416
Product dimensions: 7.38(w) x 9.25(h) x 0.90(d)

About the Author

Jort Rodenburg is a software engineer specializing in C#. He has built software for numerous fields, from financial compliance to inkjet printing. Jort has mentored and taught courses on object-oriented programming to help developers get up to speed with C# and .NET.

Table of Contents

Preface ix

Acknowledgments xi

About this book xiii

About the author xvii

About the cover illustration xviii

Part 1 Using C# and .NET 1

1 Introducing C# and .NET 3

1.1 Why work in C#? 5

Reason 1 C# is economical 6

Reason 2 C# is maintainable 6

Reason 3 C# is developer friendly and easy to use 7

1.2 Why not work in C#? 7

Operating system development 8

Real-time operating system embedded development in C# 8

Numerical computing and C# 9

1.3 Switching to C# 9

1.4 What you will learn in this book 12

1.5 What you will not learn in this book 12

2 .NET and how it compiles 15

2.1 What is the .NET Framework? 16

2.2 What is .NET 5? 17

2.3 How CLI-compliant languages are compiled 19

Step 1 C# code (high-level) 20

Step 2 Common Intermediate Language (assembly level) 23

Step 3 Native code (processor level) 29

Part 2 The existing codebase 33

3 How bad is this code? 35

3.1 Introducing Flying Dutchman Airlines 36

3.2 Pieces of the puzzle: Taking a look at our requirements 38

Object-relational mapping 38

The GET/flight endpoint: Retrieving information on all flights 39

The GET /flight/ {flightNumber} endpoint: Getting specific flight information 40

The POST /booking/ {flightNumber} endpoint: Booking a flight 41

3.3 Coming to terms with the existing codebase 43

Assessing the existing database schema and its tables 43

The existing codebase: Web service configuration files 44

Considering models and views in the existing codebase 50

4 Manage your unmanaged resources! 58

4.1 The FlightController: Assessing the GET/flight endpoint 60

The GET /flight endpoint and what it does 60

Method signature: The meaning of ResponseType and typeof 61

Collecting flight information with collections 63

Connection strings, or how to give a security engineer a heart attack 64

Using IDisposable to release unmanaged resources 65

Querying a database with SqlCommand 67

4.2 The FlightController: Assessing GET /flight/{flightNumber} 70

4.3 The FlightController: POST /flight 73

4.4 The FlightController: DELETE /flight/{flightNumber} 77

Part 3 The database access layer 81

5 Setting up a project and database with Entity Framework Core 83

5.1 Creating a .NET 5 solution and project 84

5.2 Setting up and configuring a web service 88

Configuring a .NET 5 web service 89

Creating and using HostBuilder 91

Implementing the Startup class 93

Using the repository/service pattern for our web service architecture 96

5.3 Implementing the database access layer 98

Entity Framework Core and reverse-engineering 98

DbSet and the Entity Framework Core workflow 100

Configuration methods and environment variables 102

Setting an environment variable on Windows 103

Setting an environment variable on macOS 104

Retrieving environment variables at run time in your code 104

Part 4 The Repository Layer 109

6 Test-driven development and dependency injection 111

6.1 Test-driven development 113

6.2 The CreateCustomer method 117

Why you should always validate input arguments 118

Using "arrange, act, assert" to write unit tests 119

Validating against invalid characters 120

In-lining test data with the [DataRow] attribute 123

Object initializers and autogenerated code 123

Constructors, reflection, and asynchronous programming 125

Locks, mutexes, and semaphores 127

Synchronous to asynchronous execution … continued 129

Testing Entity Framework Core 130

Controlling dependencies with dependency injection 132

7 Comparing objects 140

7.1 The GetCustomerByName method 141

Question marks: Nullable types and their applications 143

Custom exceptions, LINQ, and extension methods 144

7.2 Congruence: From the Middle Ages to C# 149

Creating a "comparer" class using EqualityComparer 150

Testing equality by overriding the Equals method 153

Overloading the equality operator 154

8 Stubbing, generics, and coupling 160

8.1 Implementing the Booking repository 161

8.2 Input validation, separation of concerns, and coupling 164

8.3 Using object initializers 169

8.4 Unit testing with stubs 172

8.5 Programming with generics 176

8.6 Providing default arguments by using optional parameters 177

8.7 Conditionals, Func, switches, and switch expressions 179

The ternary conditional operator 180

Branching using an array of functions 181

Switch statements and expressions 181

Querying for pending changes in Entity Framework Core 183

9 Extension methods, streams, and abstract classes 188

9.1 Implementing the Airport repository 189

9.2 Getting an Airport out of the database by its ID 190

9.3 Validating the AirportID input parameter 192

9.4 Output streams and being specifically abstract 194

9.5 Querying the database for an Airport object 199

9.6 Implementing the Flight repository 206

The IsPositive extension method and "magic numbers" 208

Getting a flight out of the database 214

Part 5 The service layer 219

10 Reflection and mocks 221

10.1 The repository/service pattern revisited 222

What is the use of a service class? 223

10.2 Implementing the CustomerService 225

Setting up for success: Creating skeleton classes 225

How to delete your own code 227

10.3 Implementing the BookingService 229

Unit testing across architectural layers 234

The difference between a stub and a mock 235

Mocking a class with the Moq library 236

Calling a repositoiy from a service 243

11 Runtime type checking revisited and error handling 248

11.1 Validating input parameters of a service layer method 249

Runtime type checks with the is and as operators 253

Type checking with the is operator 253

Type checking with the as operator 255

What did we do in section 11.1? 255

11.2 Cleaning up the BookingServiceTests class 256

11.3 Foreign key constraints in service classes 258

Calling the Flight repository from a service class 259

12 Using IAsyncEnumerable and yield return 273

12.1 Do we need an AirportService class? 274

12.2 Implementing the FlightService class 276

Getting information on a specific flight from the FlightRepository 276

Combining two data streams into a view 280

Using the yield return keywords with try-catch code blocks 288

Implementing GetFlightByFlightNumber 292

Part 6 The controller layer 301

13 Middleware, HTTP routing, and HTTP responses 303

13.1 The controller class within the repository/service pattern 304

13.2 Determining what controllers to implement 306

13.3 Implementing the FlightController 308

Returning HTTP responses with the IActionResult interface (GetFlights) 309

Injecting dependencies into a controller using middleware 312

Implementing the GET/Flight/(FlightNumber) endpoint 320

13.4 Routing HTTP requests to controllers and methods 324

14 JSON serialization/deserialization and custom model binding 332

14.1 Implementing the BookingControiler class 333

Introduction to data deserialization 335

Using the [FromBody] attribute to deserialize incoming HTTP data 339

Using a custom model binder and method attribute for model binding 340

Implementing the CreateBooking endpoint method logic 343

14.2 Acceptance testing and Swagger middleware 349

Manual acceptance testing with an OpenAPI specification 349

Generating an OpenAPI specification at runtime 354

14.3 The end of the road 360

Appendix A Exercise answers 363

Appendix B Clean code checklist 371

Appendix C Installation guides 373

Appendix D OpenAPI FlyTomorrow 377

Appendix E Reading list 380

Index 385

Customer Reviews