Code like a Pro in C#

Book description

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

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.

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.

Quotes
Worth reading multiple times.
- Prabhuti Prakash, Synechron Technologies

Packed with guidance and insights to get you there quickly. Highly recommended!
- Edin Kapic, isolutions

This book really helped me to move to the next level.
- Daniel Vásquez Estupiñan, Tokiota

This is the book you are looking for when you want to know how to code in the most idiomatic way possible with C#.
- Gustavo Filipe Ramos Gomes, Troido

Teaches excellent techniques and best practices for modern C# development.
- Foster Haines, J2 Interactive

Publisher resources

View/Submit Errata

Table of contents

  1. inside front cover
    1. Clean Code Checklist
  2. Code like a Pro in C#
  3. Copyright
  4. contents
  5. front matter
    1. preface
    2. acknowledgments
    3. about this book
    4. Who should read this book
    5. How this book is organized: A roadmap
    6. About the code
    7. liveBook discussion forum
    8. about the author
    9. about the cover illustration
  6. Part 1 Using C# and .NET
  7. 1 Introducing C# and .NET
    1. 1.1 Why work in C#?
      1. 1.1.1 Reason 1: C# is economical
      2. 1.1.2 Reason 2: C# is maintainable
      3. 1.1.3 Reason 3: C# is developer friendly and easy to use
    2. 1.2 Why not work in C#?
      1. 1.2.1 Operating system development
      2. 1.2.2 Real-time operating system embedded development in C#
      3. 1.2.3 Numerical computing and C#
    3. 1.3 Switching to C#
    4. 1.4 What you will learn in this book
    5. 1.5 What you will not learn in this book
    6. Summary
  8. 2 .NET and how it compiles
    1. 2.1 What is the .NET Framework?
    2. 2.2 What is .NET 5?
    3. Exercises
    4. 2.3 How CLI-compliant languages are compiled
      1. 2.3.1 Step 1: C# code (high-level)
      2. 2.3.2 Step 2: Common Intermediate Language (assembly level)
      3. 2.3.3 Step 3: Native code (processor level)
    5. Exercises
    6. Summary
  9. Part 2 The existing codebase
  10. 3 How bad is this code?
    1. 3.1 Introducing Flying Dutchman Airlines
    2. 3.2 Pieces of the puzzle: Taking a look at our requirements
      1. 3.2.1 Object-relational mapping
      2. 3.2.2 The GET /flight endpoint: Retrieving information on all flights
      3. 3.2.3 The GET /flight/{flightNumber} endpoint: Getting specific flight information
      4. 3.2.4 The POST /booking/{flightNumber} endpoint: Booking a flight
    3. 3.3 Coming to terms with the existing codebase
      1. 3.3.1 Assessing the existing database schema and its tables
      2. 3.3.2 The existing codebase: Web service configuration files
      3. 3.3.3 Considering models and views in the existing codebase
    4. Summary
  11. 4 Manage your unmanaged resources!
    1. 4.1 The FlightController: Assessing the GET /flight endpoint
      1. 4.1.1 The GET /flight endpoint and what it does
      2. 4.1.2 Method signature: The meaning of ResponseType and typeof
      3. 4.1.3 Collecting flight information with collections
      4. 4.1.4 Connection strings, or how to give a security engineer a heart attack
      5. 4.1.5 Using IDisposable to release unmanaged resources
      6. 4.1.6 Querying a database with SqlCommand
    2. 4.2 The FlightController: Assessing GET /flight/{flightNumber}
    3. 4.3 The FlightController: POST /flight
    4. 4.4 The FlightController: DELETE /flight/{flightNumber}
    5. Exercises
    6. Summary
  12. Part 3 The database access layer
  13. 5 Setting up a project and database with Entity Framework Core
    1. 5.1 Creating a .NET 5 solution and project
    2. 5.2 Setting up and configuring a web service
      1. 5.2.1 Configuring a .NET 5 web service
      2. 5.2.2 Creating and using HostBuilder
      3. 5.2.3 Implementing the Startup class
      4. 5.2.4 Using the repository/service pattern for our web service architecture
    3. 5.3 Implementing the database access layer
      1. 5.3.1 Entity Framework Core and reverse-engineering
      2. 5.3.2 DbSet and the Entity Framework Core workflow
      3. 5.3.3 Configuration methods and environment variables
      4. 5.3.4 Setting an environment variable on Windows
      5. 5.3.5 Setting an environment variable on macOS
      6. 5.3.6 Retrieving environment variables at run time in your code
    4. Exercises
    5. Summary
  14. Part 4 The repository layer
  15. 6 Test-driven development and dependency injection
    1. 6.1 Test-driven development
    2. Exercises
    3. 6.2 The CreateCustomer method
      1. 6.2.1 Why you should always validate input arguments
      2. 6.2.2 Using “arrange, act, assert” to write unit tests
      3. 6.2.3 Validating against invalid characters
      4. 6.2.4 In-lining test data with the [DataRow] attribute
      5. 6.2.5 Object initializers and autogenerated code
      6. 6.2.6 Constructors, reflection, and asynchronous programming
      7. 6.2.7 Locks, mutexes, and semaphores
      8. 6.2.8 Synchronous to asynchronous execution . . . continued
      9. 6.2.9 Testing Entity Framework Core
      10. 6.2.10 Controlling dependencies with dependency injection
    4. Exercises
    5. Summary
  16. 7 Comparing objects
    1. 7.1 The GetCustomerByName method
      1. 7.1.1 Question marks: Nullable types and their applications
      2. 7.1.2 Custom exceptions, LINQ, and extension methods
    2. 7.2 Congruence: From the Middle Ages to C#
      1. 7.2.1 Creating a “comparer” class using EqualityComparer<T>
      2. 7.2.2 Testing equality by overriding the Equals method
      3. 7.2.3 Overloading the equality operator
    3. Exercises
    4. Summary
  17. 8 Stubbing, generics, and coupling
    1. 8.1 Implementing the Booking repository
    2. 8.2 Input validation, separation of concerns, and coupling
    3. Exercises
    4. 8.3 Using object initializers
    5. 8.4 Unit testing with stubs
    6. 8.5 Programming with generics
    7. 8.6 Providing default arguments by using optional parameters
    8. 8.7 Conditionals, Func, switches, and switch expressions
      1. 8.7.1 The ternary conditional operator
      2. 8.7.2 Branching using an array of functions
      3. 8.7.3 Switch statements and expressions
      4. 8.7.4 Querying for pending changes in Entity Framework Core
    9. Exercises
    10. Summary
  18. 9 Extension methods, streams, and abstract classes
    1. 9.1 Implementing the Airport repository
    2. 9.2 Getting an Airport out of the database by its ID
    3. 9.3 Validating the AirportID input parameter
    4. 9.4 Output streams and being specifically abstract
    5. 9.5 Querying the database for an Airport object
    6. 9.6 Implementing the Flight repository
      1. 9.6.1 The IsPositive extension method and “magic numbers”
      2. 9.6.2 Getting a flight out of the database
    7. Exercises
    8. Summary
  19. Part 5 The service layer
  20. 10 Reflection and mocks
    1. 10.1 The repository/service pattern revisited
      1. 10.1.1 What is the use of a service class?
    2. Exercises
    3. 10.2 Implementing the CustomerService
      1. 10.2.1 Setting up for success: Creating skeleton classes
      2. 10.2.2 How to delete your own code
    4. Exercises
    5. 10.3 Implementing the BookingService
      1. 10.3.1 Unit testing across architectural layers
      2. 10.3.2 The difference between a stub and a mock
      3. 10.3.3 Mocking a class with the Moq library
      4. 10.3.4 Calling a repository from a service
    6. Exercises
    7. Summary
  21. 11 Runtime type checking revisited and error handling
    1. 11.1 Validating input parameters of a service layer method
      1. 11.1.1 Runtime type checks with the is and as operators
      2. 11.1.2 Type checking with the is operator
      3. 11.1.3 Type checking with the as operator
      4. 11.1.4 What did we do in section 11.1?
    2. 11.2 Cleaning up the BookingServiceTests class
    3. 11.3 Foreign key constraints in service classes
      1. 11.3.1 Calling the Flight repository from a service class
    4. Exercises
    5. Summary
  22. 12 Using IAsyncEnumerable<T> and yield return
    1. 12.1 Do we need an AirportService class?
    2. 12.2 Implementing the FlightService class
      1. 12.2.1 Getting information on a specific flight from the FlightRepository
      2. 12.2.2 Combining two data streams into a view
      3. 12.2.3 Using the yield return keywords with try-catch code blocks
      4. 12.2.4 Implementing GetFlightByFlightNumber
    3. Exercises
    4. Summary
  23. Part 6 The controller layer
  24. 13 Middleware, HTTP routing, and HTTP responses
    1. 13.1 The controller class within the repository/service pattern
    2. 13.2 Determining what controllers to implement
    3. 13.3 Implementing the FlightController
      1. 13.3.1 Returning HTTP responses with the IActionResult interface (GetFlights)
      2. 13.3.2 Injecting dependencies into a controller using middleware
      3. 13.3.3 Implementing the GET /Flight/{FlightNumber} endpoint
    4. 13.4 Routing HTTP requests to controllers and methods
    5. Exercises
    6. Summary
  25. 14 JSON serialization/ deserialization and custom model binding
    1. 14.1 Implementing the BookingController class
      1. 14.1.1 Introduction to data deserialization
      2. 14.1.2 Using the [FromBody] attribute to deserialize incoming HTTP data
      3. 14.1.3 Using a custom model binder and method attribute for model binding
      4. 14.1.4 Implementing the CreateBooking endpoint method logic
    2. 14.2 Acceptance testing and Swagger middleware
      1. 14.2.1 Manual acceptance testing with an OpenAPI specification
      2. 14.2.2 Generating an OpenAPI specification at runtime
    3. 14.3 The end of the road
    4. Summary
  26. appendix A Exercise answers
    1. Chapter 2: .NET and how it compiles
    2. Chapter 4: Manage your unmanaged resources!
    3. Chapter 5: Setting up a project and database with Entity Framework Core
    4. Chapter 6: Test-driven development and dependency injection
    5. Chapter 7: Comparing objects
    6. Chapter 8: Stubbing, generics, and coupling
    7. Chapter 9: Extension methods, streams, and abstract classes
    8. Chapter 10: Reflection and mocks
    9. Chapter 11: Runtime type checking revisited and error handling
    10. Chapter 12: Using IAsyncEnumerable<T> and yield return
    11. Chapter 13: Middleware, HTTP routing, and HTTP responses
  27. appendix B Clean code checklist
  28. appendix C Installation guides
  29. appendix D OpenAPI FlyTomorrow
  30. appendix E Reading list
  31. index
  32. inside back cover
    1. Clean Code Checklist

Product information

  • Title: Code like a Pro in C#
  • Author(s): Jort Rodenburg
  • Release date: July 2021
  • Publisher(s): Manning Publications
  • ISBN: 9781617298028