An Atypical ASP.NET Core 5 Design Patterns Guide

By Carl-Hugo Marcotte
    What do you get with a Packt Subscription?

  • Instant access to this title and 7,500+ eBooks & Videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Free Chapter
    Chapter 1: Introduction to .NET
About this book
Design patterns are a set of solutions to many of the common problems occurring in software development. Knowledge of these design patterns helps developers and professionals to craft software solutions of any scale. ASP.NET Core 5 Design Patterns starts by exploring basic design patterns, architectural principles, dependency injection, and other ASP.NET Core mechanisms. You’ll explore the component scale as you discover patterns oriented toward small chunks of the software, and then move to application-scale patterns and techniques to understand higher-level patterns and how to structure the application as a whole. The book covers a range of significant GoF (Gangs of Four) design patterns such as strategy, singleton, decorator, facade, and composite. The chapters are organized based on scale and topics, allowing you to start small and build on a strong base, the same way that you would develop a program. With the help of use cases, the book will show you how to combine design patterns to display alternate usage and help you feel comfortable working with a variety of design patterns. Finally, you’ll advance to the client side to connect the dots and make ASP.NET Core a viable full-stack alternative. By the end of the book, you’ll be able to mix and match design patterns and have learned how to think about architecture and how it works.
Publication date:
December 2020


Chapter 1: Introduction to .NET

The goal behind this book is not to create yet another design pattern book, but instead, it organizes the chapters cohesively based on scale and topics, allowing you to start small with strong bases and build slowly on top, in just the same way that you would build a program.

Instead of writing a guide that covers a few ways of applying a design pattern, we explore the thinking process behind the systems that we are designing from a software engineer's point of view.

This is not a magic recipe book, and from experience, there is no magic recipe when designing software; there is only your logic, knowledge, experience, and analytical skills. From that last sentence, let's define experience according to your past successes and failures. And don't worry, you will fail during your career, but don't get discouraged by it. The faster you fail, the faster you can recover and learn, leading to successful products. Many techniques covered...


What is a design pattern?

Since you just purchased a book about design patterns, I guess that you have some idea of what they are, but let's just make sure that we are on the same page:

Abstract definition: A design pattern is a proven technique that can be used to solve a specific problem.

In this book, we apply different patterns to solve different problems and how to leverage some open source tools to go further, faster! Abstract definitions make people sound intelligent and all, but there is no better way to learn than by experimenting with something, and design patterns are no different.

If that definition does not make sense to you yet, don't worry. You should have enough information at the end of the book to correlate the multiple practical examples and explanations with that definition, making it clear enough.

I like to compare programming to playing with LEGO® because what you must do is mostly the same: snap small pieces together to create something...


Anti-patterns and code smells

Anti-patterns and code smells are architectural bad practices or tips about possible bad design. Learning about best practices is as important as learning about bad ones, which is where we start. Moreover, there are multiple anti-patterns and code smells throughout the book to help you get started.

Unfortunately, we can't cover every detail of every topic, so I encourage you to dig deeper into those fields as well as into design patterns and architectural principles.


An anti-pattern is the opposite of a design pattern: it is a proven flawed technique that will most likely cause you some trouble and cost you time and money (and probably give you a headache or two along the way).

A priori, an anti-pattern is a pattern that seems to be a good idea, and that seems to be the solution that you were looking for, but in the end, it will most likely cause more harm than good. Some anti-patterns started out as legitimate design patterns...


Understanding the web – Request/Response

Before going any further, it is imperative to understand the basic concept of the web. The idea behind HTTP 1.X is that a client sends an HTTP request to a server, and then the server responds to that client. That can sound trivial if you have web development experience. However, it is one of the most important web programming concepts, irrespective of whether you are building web APIs, websites, or complex cloud applications.

Let's reduce an HTTP request lifetime to the following:

  1. The communication starts.
  2. The client sends a request to the server.
  3. The server receives the request.
  4. The server most likely does something (executes some code/logic).
  5. The server responds to the client.
  6. The communication ends.

After that cycle, the server is no longer aware of the client. Moreover, if the client sends another request, the server is not aware that it responded to a request earlier for that same client...


Getting started with .NET

A bit of history; the team did a magnificent job building ASP.NET Core from the ground up, cutting out compatibility with older versions. That brought its share of problems at first, but those interoperability issues got alleviated by the creation of .NET Standards. Now, with the reunification of most technologies into .NET 5 and the promise of a shared BCL, the name ASP.NET Core is (almost) no more, but became the future. Afterward, Microsoft is planning a major release of .NET every year, so 2021 should be .NET 6, and so on.

The good thing is that architectural principles and design patterns should remain relevant in the future and are not tightly coupled with the versions of .NET you are using. Minor changes to the code sample should be enough to migrate the knowledge and the code to the new versions.

Now, let's cover some key information surrounding .NET 5 and the .NET ecosystem.

.NET SDK versus runtime

You can install different binaries...



In this chapter, we peaked at design patterns, anti-patterns, and code smells. We also explored a few of those. We then moved to a reminder about the request/response cycle of a typical web application.

We continued by exploring .NET essentials, such as SDK versus runtime and app target versus .NET Standard. This sets us on the path to exploring the different possibilities that we have when it comes to building our .NET applications. We then dug a little more into the .NET CLI, where I laid down a list of essential commands, including dotnet build and dotnet watch run. We also covered how to create new projects.

In the next two chapters, we explore automated testing and architectural principles. These are foundational chapters for anyone wishing to build robust, flexible, and maintainable applications.



Let's take a look at a few practice questions:

  1. Can we add a body to a GET request?
  2. Why are long methods a code smell?
  3. Is it true that .NET Standard should be your default target when creating libraries?
  4. What is a code smell?

Further reading

Here are some links to consolidate what has been learned in the chapter:

About the Author
  • Carl-Hugo Marcotte

    Carl-Hugo Marcotte has been developing, designing, and architecting web applications professionally since 2005, wrote his first line of code at about eight years old, and holds a bachelor's degree in computer science. After working at a firm for a few years, he became an independent consultant, where he developed projects of different sizes for SMEs and educational institutions. He is now a Senior Solutions Architect at Export Development Canada and is passionate about software architecture, C#, ASP.NET Core, and the Cloud. He loves to share his knowledge, which led him to teaching programming, blogging, and creating, maintaining and contributing to multiple open-source projects.

    Browse publications by this author
An Atypical ASP.NET Core 5 Design Patterns Guide
Unlock this book and the full library FREE for 7 days
Start now