ASP.NET Core Quick Start Review
Welcome to this review of http://aspnetcorequickstart.com/ by Steve Smith.
ASP.NET Core Quick Start is a Dev IQ course, making a change from my regular reviews of Pluralsight courses. The course sells for $49.99 and I will be comparing it to other online course offerings at the end of this review.
You don’t need to have any previous experience of ASP.NET Core, or even much programming experience at all, to learn this course as Steve takes you all the way from the beginning.
It is beneficial, but not essential to have some very basic prior knowledge of web servers such as IIS.
I recommend learning the basics of the C# language and web programming before watching this course. You should understand common programming terms such as constructor, dependency, MVC, lambda, async and middleware. You will probably get some benefit from the course even if all of those terms are unfamiliar however.
It is mostly aimed at people who are new to ASP.NET Core but also contains more advanced content later on that is useful to intermediate and even highly experienced developers.
The Agenda for the course is:
- ASP.NET Core Goals and Benefits
- Installing ASP.NET Core
- Creating ASP.NET Core Apps in Visual Studio and VS Code
- ASP.NET Core Startup
- Injecting Dependencies in ASP.NET Core
- Real World Middleware
- ASP.NET Core MVC and Web APIs
- Testing ASP.NET Core Apps
- Clean DDD Architecture for Non-Trivial ASP.NET Core Apps
A feature that I find really important with online training is the ability to change the playback speed. Dev IQ allows you to choose between 1x, 1.25x, 1.5x, 1.75x or 2x. For this course I found 1.5x works well. Unfortunately it changes back to 1x again after each video clip, or other server side change. It’s a little annoying having to keep resetting this and I hope that Dev IQ change this to a cookie based solution soon.
The Goals and Benefits video clip begins with an explanation of ASP.NET 4.6 and ASP.NET Core 1.0. I recommend having a read of the latest news from Build 2017 on ASP.NET Core 2.0 conjunction with this.
Steve then explains the relationship with GitHub and many other benefits of this technology, including statistics on how fast it is.
Chapter 2 Installing (6 minutes)
Chapter 3 Creating Apps in VS and VS Code (9 minutes)
This begins with an introduction to the various project templates. This course covers ASP.NET Core 1.1 but there is likely not going to be any significant difference in future versions.
There’s a demo showing how to find your ways around a basic Web Application in Visual Studio 2017. Next we see the command line interface and some useful commands. Steve runs these commands from his Mac’s bash terminal. So by the end of this chapter you should know how to get started.
Chapter 4 Startup (17 minutes)
This introduces WebHostBuilder and shows how to use it to build a minimal ASP.NET Core application, i.e. a hello world web app.
10 minutes in, Steve introduces the Startup class, which is useful for configuring how our app behaves. He describes the constructor, ConfigureServices and Configure methods, and then gives a demonstration explaining the default web application code.
Also see Steve’s article Hosting in ASP.NET Core.
Chapter 5 Middleware (26 minutes)
This covers Built-in and Custom Middleware. As described in the Microsoft documentation, middleware are “software components that are assembled into an application pipeline to handle requests and responses”.
Steve likens middleware to nesting dolls, and we see a diagram showing how three pieces of middleware interact with each other in a nested fashion.
We see examples of some common functions for creating middleware:
We see a demonstration of how to create custom middleware in Startup, beginning with how to chain together two pieces of middleware with app.Use. We see our app displays either “Hello, world!” or “Hello, students!” depending on the URL we enter.
You don’t really want a lot of middleware code inside of your Startup class and Steve shows an example with your middleware in its own class, and demonstrates how to do this in Visual Studio Code. Fifteen minutes into this chapter we see the behavior is the same but our code is now much cleaner.
But as Steve explains, for most apps we don’t need to write our own middleware because there are already a selection of middleware to choose from. Steve describes the following pieces of Diagnostics middleware:
Seventeen minutes in, we see a demo of all of these middleware.
Finally, these other middleware are introduced:
Chapter 6 Dependency Injection (27 minutes)
To keep our code easy to maintain, we want it to be modular and loosely coupled, and we can achieve this using Dependency Injection.
If you are a Pluralsight subscriber, Steve Smith has also produced a course on the SOLID principles of Object Oriented Design which includes a through tutorial on Dependency Injection and Dependency Inversion.
However, Steve also introduces the concepts here and even if it is all new to you, once you see the examples you’ll understand that Dependency is essentially a $20 dollar term for a 20 cent concept!
Steve explains the Explicit Dependencies Principle and how it is important for looser coupling, followed by the Dependency Inversion Principle.
These principles lead us to what is sometimes called “Poor mans dependency injection” — manually constructing many objects. Inversion of Control (IOC) containers make use of the Factory pattern to significantly reduce the amount of work you need to do.
Steve explains some of the IOC containers available with ASP.NET Core. We learn IServiceProvider and IServiceCollection here, as well as the Transient, Scoped and Singleton Service Lifetimes.
9 minutes in, we see a dependency injection demonstration using Visual Studio Code, and this moves onto the HomeController 14 minutes in. There is nothing very complicated with this technique, but it is very powerful and leads to much more maintainable and testable code.
The demonstration includes creating a TimeService and a MessageService and the effect of the different Service Lifetimes on the program behavior.
Finally, Steve gives some advice about Service Lifetimes with Entity Framework Core.
Chapter 7 — MVC and Web API (77 minutes)
The aim of the video clip on MVC and Web API is to get you started writing business applications on top of these frameworks.
It is almost a course in itself, lasting a whopping 77 minutes. This is a huge amount of information to absorb at once, so here’s an overview of what might be of interest to you:
Steve begins by explaining what MVC is, discusses Controllers and Actions, and Returning Data or Views. He talks about the difference between models and viewmodels, and then moves on to describe ASP.NET MVC data annotations such as [HttpGet] and [AllowAnonymous].
We see that it is just a simple one line of code to return any of the following views or objects:
- default view
- view with a viewmodel
- a view with a specified name
- an object formatted based on request
- a JSON-formatted object
- a 200 HTTP status
- OK result with data
- a 400 Bad request status
Steve then gives a demonstration in Visual Studio showing all the relevant Microsoft AspNetCore and EntityFrameworkCore dependencies.
We see examples of returning specified views, and a view with a viewmodel containing contact details (Name, BlogName, BlogUrl and Twitter).
Steve and I are both fans of Postman and we see some of the useful features that this free tool offers us.
We see some best practices such as using asynchronous tasks and using Generics.
It is extremely easy to perform basic validations on request inputs using data annotations such as [Require] and [MaxLength(30)] and we see appropriate responses returned and displayed in Postman.
Working with Views
In ASP.NET Core, Views work much like they do in earlier versions of ASP.NET MVC. If you’re not familiar with them yet, you’ll learn everything you need to know about them here, including partial views and razor syntax.
34 minutes in Steve covers Routing to actions, and this hasn’t changed significantly from ASP.NET MVC 5. Steve gives a few tips for working with Routes painlessly, and then covers model binding and model binding attributes such as [BindRequired] and [BindNever].
Working with Data
We also see, beginning 40 minutes in, how to work with databases with EF Core. We’re introduced to the Microsoft.EntityFrameworkCore.InMemory package, and are encouraged to use the Repository pattern while using Entity Framework.
Starting at 45:42, we see a demonstration of how to modify data. While choosing the project template, the “Individual User Accounts” authentication option provides SQL Server database support.
Microsoft gives us a starter app, but when we login in we see an error page with the message “A database operation failed while processing the request”. This is easy to fix — we just click the “Apply migrations” button and the rest is magic.
Actually it’s not really magic, as Steve shows us the database that has been created for us, and the tables that are in it. We see how to add and update migrations using the command line.
We build a TodoItemsController and see how to use async with Tasks. The basic code just returns an empty list, so then we write a method to add new todo items. We fire off our HTTP requests using Postman.
Later in the demo we receive the error:
InvalidOperationException: Unable to resolve service for type ‘ModifyingDataDemo.Interfaces.IToDoItemRepository’ while attempting to activate ‘ModifyingDataDemo.Controllers.ToDoItemController’.
Steve says this is a common error related to Dependency Injection, and we see how to fix this by adding a new service to the ConfigureServices method in our Startup.cs class.
The last lesson in this chapter is on using filters to encapsulate cross-cutting concerns.
An example of a useful built-in filter is the [ValidateAntiForgeryToken] which generates an error if forgery tokens do not match.
Steve shows us that we can greatly reduce the amount of code needed in our controller methods by moving logic out into[ValidateModel] and [ValidateAuthorExists] filters. Then we can reduce the lines of code even further by applying filters at the class level rather than the method level.
Chapter 8 Testing (32 minutes)
Steve begins by stating that while we can always manually test our apps, computers can test for us so much faster it makes sense to automate.
Even if you have never written a test before, you’ll learn everything you need to know to get started writing both unit and integration tests, and understand when it is most appropriate to write either of these types of automated tests.
We also learn about the new Visual Studio 2017 live unit testing feature which enables continuous running of our tests.
Steve recommends writing unit tests for your business logic, but recommends trying to keeping that code away from ASP.NET Core. Test logic in your controllers, but there should only be minimal logic there.
Unit tests for your controllers don’t test middleware routes, model binding or filters, so they might not be as valuable as you hoped they would be.
For much more comprehensive integration testing, we can use TestServer.
The demonstration starts 9 minutes in and we start with a new xUnit Test Project.
We write an integration test called ReturnsListOfAuthors. This fails with a FileNotFoundException because we are missing a mandatory appsettings.json file. We can fix this by adding this file to the project and specifying that it is copied to the output folder.
The test still fails because there isn’t yet any data available. We see how to write a PopulateTestData function that fixes this problem.
We also see how to test an MVC method that returns a view.
Chapter 9 Clean DDD architecture (15 minutes)
This begins with a diagram showing various architectures: classical, two-tier, three-tier and N-tier.
Steve explains the benefits of an N-Tier architecture.
A couple of criticisms of N-Tier architecture are that each tier is tight coupled to its adjacent ones, and every tier it dependent on the data source. Steve says dependencies on the tier below are known as transitive dependencies.
It is really useful to be able to test a single tier of a large application without being dependent on the database. So we revisit the dependency inversion principle introduced in Chapter 6.
Steve describes the ports and adapters architecture, which is generally considered to be superior to N-Tier architectures for large scale applications, and lists some clean DDD architecture principles that we make testing a lot quicker and easier.
Steve has written a Clean Architecture project and hosted it on Github. We see that it is composed of Core, Infrastructure and Web projects. Steve says in a big software development project he would separate the Test project into two: one for unit tests and one for integration tests.
Comparison with courses on Pluralsight
Probably the closest course for comparison on Pluralsight is Scott Allen’s ASP.NET Core 1.0 Fundamentals. However that course is a lot longer at 5 hours 15 minutes, and that is either a good or a bad thing depending on how much time you want to invest.
Shawn Wildermuth’s Building a Web App with ASP.NET Core, MVC 6, EF Core, and Angular has been one of the most popular courses on Pluralsight for a long time now, but I haven’t watched much of it because 9 hours and 33 minutes is too big of a time investment for me.
I like that face that Steve Smith’s course keeps things very simple and sticks to just the stuff that you really need to know. It focuses on understanding the ASP.NET Core technology rather than getting bogged down with everything else needed to write professional applications.
After you’ve watched ASP.NET Core Quick Start you can decide for yourself whether or not you want to dive much deeper.
Pluralsight also has several several courses on Entity Framework by Julie Lerman that will complement the material covered in this course.