Code like a Pro in C#
eBook - ePub

Code like a Pro in C#

Jort Rodenburg

Share book
  1. 416 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Code like a Pro in C#

Jort Rodenburg

Book details
Book preview
Table of contents
Citations

About This Book

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

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Code like a Pro in C# an online PDF/ePUB?
Yes, you can access Code like a Pro in C# by Jort Rodenburg in PDF and/or ePUB format, as well as other popular books in Computer Science & Object Oriented Programming. We have over one million books available in our catalogue for you to explore.

Information

Publisher
Manning
Year
2021
ISBN
9781638356417

Part 1 Using C# and .NET

In this first part of the book, we’ll take a brief tour of the C# language and talk about some of its features. Chapter 1 covers what C# and .NET are and why you would (and would not) use them for your projects. Chapter 2 dives deeper into the various iterations of .NET and takes a C# method through its compilation process, stopping at each major step along the way.
Although this part is truly the introduction of this book, it still provides invaluable information to somebody familiar with C#. Some of the knowledge introduced in these first two chapters are things you need to know before moving on to more advanced topics.

1 Introducing C# and .NET

This chapter covers
  • Understanding what C# and .NET are
  • Learning why you would use C# for your projects (and why you wouldn’t)
  • Switching to C# and how to get started
Another book on C#, you say? Yes, another one. Plenty of books are written about C# and .NET, but this book has one fundamental difference: I wrote this book to help you develop clean, idiomatic C# code in your day-to-day life. This book is not a reference book but rather a practical guide. This book does not cover things like how to write an if statement, what a method signature is, or what an object is. We are not concerned about syntax but instead focus on concepts and ideas. There is a difference between knowing the syntax of a language and being able to write clean, idiomatic code. After going through this book, that is exactly what you will be able do. Whatever your background is and whatever programming languages you know, as long as you understand object-oriented programming, this book helps you shift into the C# and .NET ecosystem, as shown in figure 1.1.
Figure 1.1 Every chapter introduction contains a progress diagram, which allows you to quickly figure out where you are in the book.
What do organizations like Microsoft, Google, and the US government have in common? They all use C#—and for good reason. But why? C# is just another programming language. It bears similarities to Java and C++, allows for both object-oriented and functional programming, and enjoys wide support from a large open source community. Great. Now, why should you care? In this chapter, we’ll explore that question in depth, but let me reveal a couple of spoilers: C# excels at allowing you to create scalable software. To start writing C#, all you need is the .NET SDK of your choice (more on that in chapter 2) and perhaps an IDE. The language and runtime are open source.
Any time you look online for C#, chances are, you come across the .NET Framework. You can think about the .NET Framework as your warm blanket, a warm fire, and a mug of hot chocolate on a winter day, providing you with everything you need: libraries that encapsulate low-level Windows APIs, expose commonly used data structures and provide wrappers for complicated algorithms. Daily development in C# almost certainly involves the .NET Framework, .NET Core, or .NET 5, so we’ll explore these frameworks where appropriate.
Figure 1.2 shows where this book’s topics fit in a general .NET web architecture. It also shows the architecture we use to completely rewrite an existing application, which we’ll start in chapter 5 (the green/dashed arrows indicate this path).
Figure 1.2 An example of a typical web service architecture on a Microsoft stack. This book follows the approach shown by the green/dashed arrows. This book covers the presentation, business logic, and data access layers.
For those of you with prior experience in C#: this book sits between beginner and advanced resources. With the skills taught in this book, you can bridge the knowledge gap and prepare yourself for advanced skills. The first two chapters may seem a bit basic to you, but I invite you to not skim over these. It is always good to refresh your knowledge.

1.1 Why work in C#?

If you are already familiar with a programming language other than C# and like using it, why should you use C#? Perhaps you were hired by a company that uses only C#. Or maybe you just want to see what all the fuss is about.
I promise not to repeatedly tell you that C# is a “strongly typed object-oriented programming language that enables cross-platform development of scalable enterprise software.” You are likely aware of that, and it is hardly the most exciting sentence to dissect. In this section, we cover the buzzwords in that definition once and do not touch on it again. At the risk of sounding like I’m employed by Microsoft’s marketing department, for the rest of this section, we’ll focus on the following highlights and use cases of C#:
  • C# (and the .NET ecosystem) enables the development of software in an economical way. Economical solutions are important because enterprise development is the bread and butter of C#.
  • C# can improve code stability and is maintainable because of its support for self-documenting code, secure libraries, and ease of use.
  • C# is developer friendly and easy to use. There’s nothing worse than discovering that the programming language you want to use does not have good support for the things you love (such as a stable package manager, good support for unit testing, and a cross-platform runtime).
Of course, writing scalable, maintainable, and developer-friendly “clean code” can be done in most (if not all) programming languages. The difference lies in the developer experience. Some languages are really good at guiding you in writing clean code, whereas others are not. C# is not perfect, but it does try to help you in this regard.

1.1.1 Reason 1: C# is economical

C# is free to use and develop in. The language and platform are fully open source, all documentation is free, and most tooling has free options. For example, a common C# setup includes an installation of C# 8, .NET 5, and Visual Studio Community. All these are free and used in this book. No license fee is required for the runtime, and you can deploy the end product wherever you want.

1.1.2 Reason 2: C# is maintainable

When we talk about maintainability in this book, we mean the ability to fix bugs, change functionality, and address other issues without unintended side effects. This sounds like an obvious requirement for any programming language, but it is very hard to implement. C# has features that improve the maintainability (and, therefore, safe extensibility) of large codebases. Think, for example, about generics and Language-Integrated Query (LINQ). We’ll discuss these two things throughout the book, but they are examples of the platform exposing functionalities that can help you write better code.
For a company, maintainability might not be the number one priority on the surface, but if you develop code that is maintainable (meaning clean code that is easily extendable and backed by tests), development costs drop. Development costs dropping when writing maintainable code may seem counterintuitive at first: maintainable code takes longer to write and architect, driving up the initial costs of development. However, imagine what happens after a little while when a user discovers a bug or they want an additional feature. If we write maintainable code, we can quickly and easily find the bug (and fix it). Adding the feature is simpler because the codebase is extensible. If we can easily extend and fix a codebase, development costs go down.
Open/Closed Principle
In 1988, the French computer scientist Bertrand Meyer (creator of the Eiffel programming language) released a book called Object-Oriented Software Construction (Prentice Hall, 1988). The release of Meyer’s book was a pivotal moment in the history of object-oriented programming and design, because in it, he introduced the Open/Closed Principle (OCP). The OCP is aimed at improving the maintainability and flexibility of software designs. Meyer says the OCP means that “software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.”
But what does the OCP mean in practical terms? To examine that, let’s apply the OCP to a class: we deem a class “open” for extension and “closed” to modification if we can add functionality to the class without changing the existing functionality (and, therefore, potentially breaking parts of our code). If you abide by that rule, the odds of you introducing a regression (or new bug) in the existing code are much smaller than if you try to force in the bug fix or new feature with no regard for maintainability and extensibility. When you work with code that is more complicated (and coupled; discussed in chapter 8), you are more likely to introduce new bugs due to misunderstanding the side effects of your changes. This is what we want to avoid at all ...

Table of contents