Code like a Pro in C#
Jort Rodenburg
  • MEAP began April 2020
  • Publication in Spring 2021 (estimated)
  • ISBN 9781617298028
  • 425 pages (estimated)
  • printed in black & white

I love the ideas here. I kept thinking "yes, yes, yes" as I've experienced similar pains after going through code reviews.

Jason Hales
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. Each technique is reinforced through coding katas and clean code best practices, locking in the skills you’ll need to tackle any job and ace your next interview.

About the Technology

C# and the .NET framework are prized for their power, flexibility, and ease of scaling. Line-of-business applications, web systems running ASP.NET, and backend servers powering enterprise data centers all rely on C#. Growing into a C# pro requires practice in real-world systems, where you learn to refactor messy code and integrate new features into existing designs. Code Like a Pro in C# lays out a path to C# mastery, tackling common challenges such as clean coding, idiomatic C#, and .NET Core that can stump aspiring C# software engineers.

About the book

Code like a Pro in C# builds on your existing programming skills to help you seamlessly upskill your coding practice or transition to C# from Java or another OO language. You’ll learn to write the kind of idiomatic C# code that’s essential for enterprise development, honing your mastery with guided coding katas. This book discusses essential backend skills and puts them into practice with a common career challenge: refactoring a legacy codebase to be secure, clean, and readable. By the time you’re done, you’ll have a professional-level understanding of C# and be ready to start specializing with advanced-level resources.
Table of Contents detailed table of contents

Part 1: Using C# and .NET

1 Introducing C# and .NET

1.1 Why work in C#?

1.1.1 Reason 1: C# is scalable and economical

1.1.2 Reason 2: C# can improve code stability

1.1.3 Reason 3: C# is developer-friendly and easy to use

1.2 Why not work in C#?

1.2.1 Operating system development in C#

1.2.2 Real-time operating system embedded development in C#

1.2.3 Numerical computing and C#

1.3 Switching to C#

1.4 The importance of “clean code”

1.5 What you will learn in this book

1.6 What you will not learn in this book

1.7 How this book is structured

1.8 Summary

2 Tour de C# and .NET

2.1 The .NET Framework compared to other platforms

2.1.1 The .NET Framework compared to other platforms: tuples

2.1.2 The .NET Framework compared to other platforms: events

2.1.3 The .NET Framework compared to other platforms: SQL

2.1.4 The .NET Framework compared to other platforms: CSV parser

2.1.5 Exercises

2.2 What is .NET Core?

2.2.1 Exercises

2.3 What is .NET 5?

2.4 How CLI-compliant languages are compiled

2.4.1 Step 1: C# code (high-level)

2.4.2 Step 2: Common Intermediate Language (assembly level)

2.4.3 Step 3: Native code (Processor Level)

2.4.4 Exercises

2.5 Summary

Part 2: Inheriting Code

3 Requirements, models, and views

3.1 Introducing “Flying Dutchman Airlines”

3.2 Pieces of the puzzle

3.2.1 The GET /flight endpoint: retrieving information on all flights

3.2.2 GET /flight/{flightNumber}

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

3.2.4 Exercises

3.3 The existing code

3.3.1 The database schema and its tables

3.3.2 The existing codebase: web service configurations files

3.3.3 Models and views

3.4 Summary

4 Examining the controller class

4.1 The FlightController: GET /flight endpoint

4.1.1 The GET /flight endpoint and what it does

4.1.2 Method Signature: the ResponseType and Typeof story

4.1.3 Collecting flight information with collections

4.1.4 Connection strings; or how to give a security engineer a heart-attack

4.1.5 Garbage collection through using statements and IDisposable

4.2 The FlightController: GET /flight/{flightNumber}

4.3 The FlightController: POST /flight

4.4 The FlightController: DELETE /flight/{flightNumber}

4.5 Exercises

4.6 Summary

Part 3: The Database Access Layer

5 Setting up a project and database with Entity Framework Core

5.1 Creating a .NET Core solution and project

5.2 Setting up and configuring a web service

5.2.1 Configuring a .NET Core web service

5.2.2 Using the Builder pattern in C#

5.2.3 Creating and using WebHostBuilder

5.2.4 Implementing the Startup class

5.2.5 Using the Repository/Service Pattern for our web service architecture

5.3 Implementing the database access layer

5.3.1 Entity Framework Core and “Reverse-Engineering”

5.3.2 DbSet and the Entity Framework Core workflow

5.3.3 Configuration Methods and environment variables

5.3.4 Setting an environment variable on Windows

5.3.5 Setting an environment variable on macOS

5.3.6 Retrieving environment variables at runtime in your code

5.4 Exercises

5.5 Summary

Part 4: The Repository Layer

6 Async, Test-Driven Development, and dependency injection

6.1 Test-Driven Development

6.1.1 Exercises

6.2 The CreateCustomer method

6.2.1 Why you should always validate input arguments

6.2.2 Using “Arrange, Act, Assert” to write unit tests

6.2.3 Validating against invalid characters and LINQ

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

6.2.5 Object initializers and auto-generated code

6.2.6 Constructors, reflection, and asynchronous programming

6.2.7 Locks, mutexes, and semaphores

6.2.8 Synchronous to asynchronous execution… continued

6.2.9 Dependency Injection and testing Entity Framework Core

6.2.10 Exercises

6.3 Summary

7 Equality, nullable types, and operator overloading

7.1 The GetCustomerByName method

7.1.1 Question marks: nullable types and their applications

7.1.2 Custom exceptions, LINQ, and Extension methods

7.2 Congruence: from the middle ages to C#

7.2.1 Creating a “Comparer” class

7.2.2 Overloading the equality operator

7.3 Exercises

7.4 Summary

8 Stubbing, generics, and coupling

8.1 Implementing the Booking Repository

8.2 Input validation, separation of concerns, and coupling

8.2.1 Exercises

8.3 Adding a booking to the database with object initializers

8.4 Unit testing with stubs

8.5 Programming with generics

8.6 Providing default arguments by using optional parameters

8.7 Conditionals, Func, switches, and switch expressions

8.7.1 Exercises

8.8 Summary

9 Extension methods, streams, and abstract classes

9.1 Implementing the Airport repository

9.2 Getting an Airport out of the database by its ID

9.3 Validating the AirportID input parameter

9.4 Output streams and being specifically abstract

9.5 Querying the database for an Airport object

9.6 Implementing the Flight repository

9.6.1 The IsPositiveInteger extension method and “magic numbers”

9.6.2 Getting a flight out of the database

9.7 Exercises

9.8 Summary

Part 5: The Service Layer

10 Reflection and mocks

10.1 The Repository/Service pattern revisited

10.1.1 What is the use of a service class?

10.1.2 Exercises

10.2 Implementing the CustomerService

10.2.1 Setting up for success: creating skeleton classes

10.2.2 How to delete your own code

10.2.3 Exercises

10.3 Implementing the BookingService

10.3.1 Unit testing across architectural layers

10.3.2 The difference between a stub and a mock

10.3.3 Mocking a class with the Moq library

10.3.4 Calling a repository from a service

10.3.5 Exercises

10.4 Summary

11 Runtime type checking revisited and error handling

11.1 Validating input parameters of a service layer method

11.1.1 Runtime type checks with the is and as keywords

11.1.2 Type checking with the is keyword

11.1.3 Type checking with the as keyword

11.1.4 What did we do in section 11.1

11.2 Cleaning up the BookingServiceTests class

11.3 Foreign key constraints in service classes

11.3.1 Calling the Flight Repository from a service class

11.4 Exercises

11.5 Summary

12 Using IAsyncEnumerable<T> and yield return

12.1 Do we need an AirportService class?

12.2 Implementing the FlightService class

12.2.1 Getting information on a specific flight from the FlightRepository

12.2.2 Combining two data streams into a View

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

12.2.4 Implementing GetFlightByFlightNumber

12.3 Exercises

12.4 Summary

Part 6: The Controller Layer

13 Middleware, HTTP routing, and HTTP responses

13.1 The controller class within the Repository/Service pattern

13.2 Determining what controllers to implement

13.3 Implementing the FlightController

13.3.1 Returning HTTP responses with the IActionResult interface (GetFlights)

13.3.2 Injecting dependencies into a controller using middleware

13.3.3 Implementing the GET /Flight/{FlightNumber} endpoint

13.4 Routing HTTP requests to controllers and methods

13.5 Exercises

13.6 Summary

14 JSON serialization / deserialization and custom model binding

14.1 Implementing the BookingController class

14.1.1 Introduction to data deserialization

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

14.1.3 Using a custom Model Binder and Method Attribute for model binding

14.1.4 Implementing the CreateBooking endpoint method logic

14.2 Acceptance Testing and Swagger middleware

14.2.1 Manual acceptance testing with an OpenAPI specification

14.2.2 Generating an OpenAPI specification at runtime

14.3 The end of the road

14.4 Summary

Part 7: Coding Katas

15 Coding kata: the “Welcome Aboard-a-Tron 3000”

15.1 Composition and the Strategy design pattern

15.2 Creating the interfaces and concrete classes

15.3 Reading a text file and using StringBuilder

15.4 Displaying passenger names and sleeping a thread

16 Coding kata: “Adjacency Matrix”

16.1 Using graphs and matrices to model our problem

16.1.1 Creating a graph from an input set

16.2 Extracting an adjacency matrix from a doubly linked list

16.3 Executing a loop in parallel

16.3.1 Using the Task Parallel Library to execute a for loop in parallel

16.4 Printing an adjacency matrix to the console

Appendixes

Appendix A: Appendix A: Exercise Answers

A.1 Chapter 2: Tour de C# and .NET

A.2 Chapter 3: Requirements, models, and views

A.3 Chapter 4: Examining the controller class

A.4 Chapter 5: Setting up a project and database with Entity Framework Core

A.5 Chapter 6: Async, Test-Driven Development, and dependency injection

A.6 Chapter 7: Equality, nullable types, and operator overloading

A.7 Chapter 8: Stubbing, generics, and coupling

A.8 Chapter 9: Extension methods, streams, and abstract classes

A.9 Chapter 10: Reflection and mocks

A.10 Chapter 11: Runtime type checking revisited and error handling

A.11 Chapter 12: Middleware, HTTP Routing, and HTTP responses

A.12 Chapter 13: JSON Serialization / deserialization and custom model binding

Appendix B: Appendix B: Clean Code checklist

Appendix C: Appendix C: Installation Guides

Appendix D: Appendix D: OpenAPI FlyTomorrow

Appendix E: Appendix E: Reading List

What's inside

  • Test-driven development
  • Refactoring an API to a multi-platform .NET Core service
  • Principles of clean code
  • Using Entity Framework Core and LINQ to query and manipulate databases

About the reader

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

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.

placing your order...

Don't refresh or navigate away from the page.
Manning Early Access Program (MEAP) Read chapters as they are written, get the finished eBook as soon as it’s ready, and receive the pBook long before it's in bookstores.
print book $35.99 $59.99 pBook + eBook + liveBook
Additional shipping charges may apply
Code like a Pro in C# (print book) added to cart
continue shopping
go to cart

eBook $38.39 $47.99 3 formats + liveBook
Code like a Pro in C# (eBook) added to cart
continue shopping
go to cart

Prices displayed in rupees will be charged in USD when you check out.
customers also reading

This book 1-hop 2-hops 3-hops

FREE domestic shipping on three or more pBooks