Book Image

Improving your C# Skills

By : Ovais Mehboob Ahmed Khan, John Callaway, Clayton Hunt, Rod Stephens
Book Image

Improving your C# Skills

By: Ovais Mehboob Ahmed Khan, John Callaway, Clayton Hunt, Rod Stephens

Overview of this book

This Learning Path shows you how to create high performing applications and solve programming challenges using a wide range of C# features. You’ll begin by learning how to identify the bottlenecks in writing programs, highlight common performance pitfalls, and apply strategies to detect and resolve these issues early. You'll also study the importance of micro-services architecture for building fast applications and implementing resiliency and security in .NET Core. Then, you'll study the importance of defining and testing boundaries, abstracting away third-party code, and working with different types of test double, such as spies, mocks, and fakes. In addition to describing programming trade-offs, this Learning Path will also help you build a useful toolkit of techniques, including value caching, statistical analysis, and geometric algorithms. This Learning Path includes content from the following Packt products: • C# 7 and .NET Core 2.0 High Performance by Ovais Mehboob Ahmed Khan • Practical Test-Driven Development using C# 7 by John Callaway, Clayton Hunt • The Modern C# Challenge by Rod Stephens
Table of Contents (26 chapters)
Title Page
Copyright and Credits
About Packt
Contributors
Preface
8
What to Know Before Getting Started
17
Files and Directories
18
Advanced C# and .NET Features
Index

Exploring .NET Core CLI and New Project Templates


Command Line Interface (CLI) is a very popular tool is almost all popular frameworks like Yeoman Generator, Angular, and others. It lends developers access to execute commands to create, build, and run projects, restore packages, and so on.

.NET CLI provides a toolset with a handful commands that can be executed from the command line interface to create .NET Core projects, restore dependencies, and build and run projects. Under the wire, Visual Studio 2015/2017 and Visual Studio Code even uses this tool to perform different options taken by the developers from their IDE; for example, to create a new project using .NET CLI, we can run the following command:

dotnet new

It will list down the available templates and the short name that can be used while creating the project.

Here is the screenshot containing the list of project templates that can be used to create/scaffold projects using .NET Core CLI:

And by running the following command, a new ASP.NET Core MVC application will be created:

dotnet new mvc

The following screenshot shows the provisioning of the new MVC project after running the preceding command. It creates the project in the same directory where the command is running and restores all the dependencies:

To install the .NET Core CLI toolset, there are some native installers available for Windows, Linux, and macOS. These installers can install and set up the .NET CLI tooling on your machine and developers can run the commands from the CLI.

Here is the list of commands with their descriptions that are provided in the .NET Core CLI:

Command

Description

Example

new

Creates a new project based on the template selected

dotnet new razor

restore

Restores all the dependencies defined in the project

dotnet restore

build

Builds the project

dotnet build

run

Runs the source code without any additional compile

dotnet run

publish

Packages the application files into a folder for deployment

dotnet publish

test

Used to execute unit tests

dotnet test

vstest

Executes unit tests from specified files

dotnet vstest [<TEST_FILE_NAMES>]

pack

Packs the code into a NuGet package

dotnet pack

migrate

Migrates .NET Core preview 2 to .NET Core 1.0

dotnet migrate

clean

Cleans the output of the project

dotnet clean

sln

Modifies a .NET Core solution

dotnet sln

help

Displays the list of commands available to execute through .NET CLI

dotnet help

store

Stores the specified assemblies in the runtime package store

dotnet store

 

Here are some of the project level commands that can be used to add a new NuGet package, remove an existing one, list references, and others:

Command

Description

Example

add package

Adds a package reference to the project

dotnet add package Newtonsoft.Json

remove package

Removes a package reference from the project

dotnet remove package Newtonsoft.Json

add reference

Adds a project reference to the project

dotnet add reference chapter1/proj1.csproj

remove reference

Removes the project reference from the project

dotnet remove reference chapter1/proj1.csproj

list reference

List down all the project references in the project

dotnet list reference

 

The following are some common Entity Framework Core commands that can be used to add migration, remove migration, update the database, and so on.

Command

Description

Example

dotnet ef migrations add

Adds a new migration

dotnet ef migrations add Initial

- Initial is the name of migration

dotnet ef migrations list

List available migrations

dotnet ef migrations list

dotnet ef migrations remove

Remove specific migration

dotnet ef migrations remove Initial

- Initial is the name of migration

dotnet ef database update

To update the database to a specified migration

dotnet ef database update Initial

- Initial is the name of migration

dotnet ef database drop

Drops the database

dotnet ef database drop

 

Here are some of the server level commands that can be used to delete the NuGet package from its actual source repository from the machine, add NuGet package into its actual source repository on the machine, and so on:

Command

Description

Example

nuget delete

Deletes the package from the server

dotnet nuget delete Microsoft.AspNetCore.App 2.0

nuget push

Pushes a package to the server and publishes it

dotnet nuget push foo.nupkg

nuget locals

Lists the local NuGet resources

dotnet nuget locals -l all

msbuild

Builds a project and all of its dependencies

dotnet msbuild

dotnet install script

The script to install the .NET CLI tools and the shared runtime

./dotnet-install.ps1 -Channel LTS

 

To run the preceding commands, we can use the tool known as dotnet from the command line and specify the actual command followed by that. When the .NET Core CLI is installed, it is set into the PATH variable in Windows OS and can be accessed from any folder. So, for example, if you are at your project root folder and wanted to restore the dependencies, you can just call the following command and it will restore all the dependencies that have been defined in your project file:

dotnet restore

The preceding command will start restoring the dependencies or project-specific tools, as defined in the project file. The restoration of tools and dependencies are done in parallel:

We can also set the path where packages can be restored by using the --packages argument. However, if this is not specified, it uses the .nuget/packages folder under the system's user folder. For example, the default NuGet folder for Windows OS is {systemdrive}:\Users\{user}\.nuget\packages and /home/{user} for Linux OS, respectively.