Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
An Atypical ASP.NET Core 5 Design Patterns Guide

You're reading from   An Atypical ASP.NET Core 5 Design Patterns Guide A SOLID adventure into architectural principles, design patterns, .NET 5, and C#

Arrow left icon
Product type Paperback
Published in Dec 2020
Publisher Packt
ISBN-13 9781789346091
Length 762 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Carl-Hugo Marcotte Carl-Hugo Marcotte
Author Profile Icon Carl-Hugo Marcotte
Carl-Hugo Marcotte
Arrow right icon
View More author details
Toc

Table of Contents (27) Chapters Close

Preface 1. Section 1: Principles and Methodologies
2. Chapter 1: Introduction to .NET FREE CHAPTER 3. Chapter 2: Testing Your ASP.NET Core Application 4. Chapter 3: Architectural Principles 5. Section 2: Designing for ASP.NET Core
6. Chapter 4: The MVC Pattern using Razor 7. Chapter 5: The MVC Pattern for Web APIs 8. Chapter 6: Understanding the Strategy, Abstract Factory, and Singleton Design Patterns 9. Chapter 7: Deep Dive into Dependency Injection 10. Chapter 8: Options and Logging Patterns 11. Section 3: Designing at Component Scale
12. Chapter 9: Structural Patterns 13. Chapter 10: Behavioral Patterns 14. Chapter 11: Understanding the Operation Result Design Pattern 15. Section 4: Designing at Application Scale
16. Chapter 12: Understanding Layering 17. Chapter 13: Getting Started with Object Mappers 18. Chapter 14: Mediator and CQRS Design Patterns 19. Chapter 15: Getting Started with Vertical Slice Architecture 20. Chapter 16: Introduction to Microservices Architecture 21. Section 5: Designing the Client Side
22. Chapter 17: ASP.NET Core User Interfaces 23. Chapter 18: A Brief Look into Blazor 24. Assessment Answers 25. Acronyms Lexicon
26. Other Books You May Enjoy

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 grouped under SDKs and runtimes. The SDK allows you to build and run .NET programs, while the runtime only allows you to run .NET programs.

As a developer, you want to install the SDK on your deployment environment. On the server, you want to install the runtime. The runtime is lighter, while the SDK contains more tools, including the runtime.

.NET 5 versus .NET Standard

When building .NET projects, there are multiple types of projects, but basically, we could separate them into two categories:

  • Applications
  • Libraries

Applications are targeting a version of .NET, such as net5.0. Examples of that would be an ASP.NET application or a console application.

Libraries are bundles of code compiled together, often distributed as a NuGet package over NuGet.org. .NET Standard class library projects allow code to be shared between .NET Core, .NET 5+, and .NET Framework projects. .NET Standards came into play to bridge the compatibility gap between .NET Core and .NET Framework, which eased the transition. It was not easy when .NET Core 1.0 first came out.

With .NET 5 unifying all of the platforms and becoming the future of that unified .NET ecosystem, .NET Standards is no longer required.

Note

I'm sure that we are going to see .NET Standard libraries stick around for a while though. All projects are not going to migrate from .NET Framework to .NET 5 magically, and people are likely to want to continue sharing code between the two.

Next versions of .NET should be built over .NET 5, while .NET Framework 4.X is going to stay where it is today, receiving only security patches and minor updates.

Visual Studio Code versus Visual Studio versus the command-line interface (CLI)

How can one of those projects be created? .NET Core comes with the dotnet CLI, which exposes multiple commands, including new. Running the dotnet new command in a terminal generates a new project.

To create an empty class library, we can run the following commands:

md MyProject cd MyProject dotnet new classlib

That would generate an empty class library in the newly created MyProject directory. The -h option can come in handy when discovering available commands and their options. You can use dotnet -h to find the available SDK commands, or dotnet new -h to find out about options and available templates.

Visual Studio Code is my favorite text editor. I don't use it much for .NET coding, but I still do to reorganize projects, when the CLI time comes, or for any other task that is easier to complete using a text editor, such as writing documentation using markdown, writing JavaScript or TypeScript, or managing JSON, YAML, or XML files. To create a project or solution, or to add a NuGet package using VS Code, open a terminal and use the CLI.

As for Visual Studio, my favorite IDE, it uses the CLI under the hood to create the same projects, making it consistent between tools. Visual Studio adds a user interface over the CLI.

You can also create and install additional dotnet new project templates to the CLI or even create global tools. Those topics are beyond the scope of this book.

An overview of project templates

Here is an example of the templates that are installed (dotnet new):

Figure 1.1 – Project templates

Figure 1.1 – Project templates

A study of all the templates is beyond the scope of this book, but I'd like to visit the few that are worth mentioning or because we will use them later:

  • dotnet new console creates a console application.
  • dotnet new classlib creates a class library.
  • dotnet new xunit creates an xUnit test project.
  • dotnet new web creates an empty web project.
  • dotnet new webapp creates an empty web project with a preconfigured Startup class.
  • dotnet new mvc scaffolds an MVC application.
  • dotnet new webapi scaffolds a web API application.

Async main (C# 7.1)

From C# 7.1 onward, a console application can have an async main method, which is very convenient as more and more code is becoming async. This new feature allows the use of await directly in the main() method, without any quirks.

Previously, the signature of the main method had to fit one of the following:

public static void Main() { }
public static int Main() { }
public static void Main(string[] args) { }
public static int Main(string[] args) { }

Since C# 7.1, we can also use their async counterpart:

public static async Task Main() { }
public static async Task<int> Main() { }
public static async Task Main(string[] args) { }
public static async Task<int> Main(string[] args) { }

Now, we can create a console that looks like this:

class Program
{
    static async Task Main(string[] args)
    {
        Console.WriteLine("Entering Main");
        var myService = new MyService();
        await myService.ExecuteAsync();
        Console.WriteLine("Exiting Main");
    }
}
public class MyService
{
    public Task ExecuteAsync()
    {
        Console.WriteLine("Inside MyService.ExecuteAsync()");
        return Task.CompletedTask;
    }
}

When executing the program, the result is as follows:

Entering Main
Inside MyService.ExecuteAsync()
Exiting Main

Nothing fancy, but it allows advantage to be taken of the await/async language feature.

Since .NET Core, all types of application start with a Main method (usually Program.Main), including ASP.NET Core 5 web applications.

Running and building your program

If you are using Visual Studio, you can always hit the play button, or F5, and run your app. If you are using the CLI, you can use one of the following commands (and more). Each of them also offers different options to control their behavior. Add the -h flag with any command to get help on that command, such as dotnet build -h:

Technical requirements

Throughout the book, we explore and write code. I recommend that you install Visual Studio, Visual Studio Code, or both, to help out with that.

Note

You could also use Notepad or your favorite text editor if you prefer. I use VS and VS Code. Choose the tools that you prefer.

Unless you install Visual Studio, which comes with the .NET SDK, you may need to install the .NET 5 SDK. The SDK comes with the CLI that we explored earlier, as well as the building tools for running and testing your programs. Have a look at the README.md file of the GitHub repository for more information and links to those resources.

The source code of all chapters is available for download on GitHub at the following address: https://net5.link/code.

You have been reading a chapter from
An Atypical ASP.NET Core 5 Design Patterns Guide
Published in: Dec 2020
Publisher: Packt
ISBN-13: 9781789346091
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime
Visually different images