Code like a Pro in C#
eBook - ePub

Code like a Pro in C#

Jort Rodenburg

Condividi libro
  1. 416 pagine
  2. English
  3. ePUB (disponibile sull'app)
  4. Disponibile su iOS e Android
eBook - ePub

Code like a Pro in C#

Jort Rodenburg

Dettagli del libro
Anteprima del libro
Indice dei contenuti
Citazioni

Informazioni sul libro

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

Domande frequenti

Come faccio ad annullare l'abbonamento?
È semplicissimo: basta accedere alla sezione Account nelle Impostazioni e cliccare su "Annulla abbonamento". Dopo la cancellazione, l'abbonamento rimarrà attivo per il periodo rimanente già pagato. Per maggiori informazioni, clicca qui
È possibile scaricare libri? Se sì, come?
Al momento è possibile scaricare tramite l'app tutti i nostri libri ePub mobile-friendly. Anche la maggior parte dei nostri PDF è scaricabile e stiamo lavorando per rendere disponibile quanto prima il download di tutti gli altri file. Per maggiori informazioni, clicca qui
Che differenza c'è tra i piani?
Entrambi i piani ti danno accesso illimitato alla libreria e a tutte le funzionalità di Perlego. Le uniche differenze sono il prezzo e il periodo di abbonamento: con il piano annuale risparmierai circa il 30% rispetto a 12 rate con quello mensile.
Cos'è Perlego?
Perlego è un servizio di abbonamento a testi accademici, che ti permette di accedere a un'intera libreria online a un prezzo inferiore rispetto a quello che pagheresti per acquistare un singolo libro al mese. Con oltre 1 milione di testi suddivisi in più di 1.000 categorie, troverai sicuramente ciò che fa per te! Per maggiori informazioni, clicca qui.
Perlego supporta la sintesi vocale?
Cerca l'icona Sintesi vocale nel prossimo libro che leggerai per verificare se è possibile riprodurre l'audio. Questo strumento permette di leggere il testo a voce alta, evidenziandolo man mano che la lettura procede. Puoi aumentare o diminuire la velocità della sintesi vocale, oppure sospendere la riproduzione. Per maggiori informazioni, clicca qui.
Code like a Pro in C# è disponibile online in formato PDF/ePub?
Sì, puoi accedere a Code like a Pro in C# di Jort Rodenburg in formato PDF e/o ePub, così come ad altri libri molto apprezzati nelle sezioni relative a Computer Science e Object Oriented Programming. Scopri oltre 1 milione di libri disponibili nel nostro catalogo.

Informazioni

Editore
Manning
Anno
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 ...

Indice dei contenuti