Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Step elaboration, typo fixes and using statements reorganization #71

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion chapters/add-external-packages/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ You could write code yourself that converted an ISO 8601 date into a human-frien

The Humanizer package on NuGet solves this problem by providing methods that can "humanize" or rewrite almost anything: dates, times, durations, numbers, and so on. It's a fantastic and useful open-source project that's published under the permissive MIT license.

To add it to your project, run this command in the terminal:
To add it to your project, open the terminal and navigate to the `AspNetCoreTodo` project directory (use `cd` if necessary) and run this command:

```
dotnet add package Humanizer
Expand Down
16 changes: 8 additions & 8 deletions chapters/automated-testing/integration-testing.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Integration testing

Compared to unit tests, integration tests are much larger in scope. exercise the whole application stack. Instead of isolating one class or method, integration tests ensure that all of the components of your application are working together properly: routing, controllers, services, database code, and so on.
Compared to unit tests, integration tests are much larger in scope and exercise the whole application stack. Instead of isolating one class or method, integration tests ensure that all of the components of your application are working together properly: routing, controllers, services, database code, and so on.

Integration tests are slower and more involved than unit tests, so it's common for a project to have lots of small unit tests but only a handful of integration tests.

Expand Down Expand Up @@ -34,16 +34,17 @@ AspNetCoreTodo/

> If you prefer, you can keep your unit tests and integration tests in the same project. For large projects, it's common to split them up so it's easy to run them separately.

Since the test project will use the classes defined in your main project, you'll need to add a reference to the main project:
Since the test project will use the classes defined in your main project, you'll need to add a reference to the main project. `cd` to the newly-created project and type:

```
dotnet add reference ../AspNetCoreTodo/AspNetCoreTodo.csproj
dotnet add reference ..\AspNetCoreTodo\AspNetCoreTodo.csproj

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This depends on the OS (Windows vs. linux) - lets keep that consistent across the whole project

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point.

```

You'll also need to add the `Microsoft.AspNetCore.TestHost` NuGet package:
You'll also need to add `Microsoft.AspNetCore.TestHost` and `Microsoft.Extensions.Configuration.Json` NuGet packages:

```
dotnet add package Microsoft.AspNetCore.TestHost
dotnet add package Microsoft.Extensions.Configuration.Json
```

Delete the `UnitTest1.cs` file that's created by `dotnet new`. You're ready to write an integration test.
Expand All @@ -55,13 +56,12 @@ There are a few things that need to be configured on the test server before each
**AspNetCoreTodo.IntegrationTests/TestFixture.cs**

```csharp
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Configuration;
using System;
using System.IO;
using System.Net.Http;

namespace AspNetCoreTodo.IntegrationTests
{
Expand Down
10 changes: 6 additions & 4 deletions chapters/automated-testing/unit-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ AspNetCoreTodo/
AspNetCoreTodo.UnitTests.csproj
```

Since the test project will use the classes defined in your main project, you'll need to add a reference to the `AspNetCoreTodo` project:
Since the test project will use the classes defined in your main project, you'll need to add a reference to the `AspNetCoreTodo` project.

Use `cd` to navigate to the newly-created AspNetCoreTodo.UnitTests project directory, and type:

```
dotnet add reference ../AspNetCoreTodo/AspNetCoreTodo.csproj
dotnet add reference ..\AspNetCoreTodo\AspNetCoreTodo.csproj

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see my previous comment

```

Delete the `UnitTest1.cs` file that's automatically created. You're ready to write your first test.
Expand Down Expand Up @@ -79,12 +81,12 @@ To write a unit test that will verify the logic in the `TodoItemService`, create
**AspNetCoreTodo.UnitTests/TodoItemServiceShould.cs**

```csharp
using System;
using System.Threading.Tasks;
using AspNetCoreTodo.Data;
using AspNetCoreTodo.Models;
using AspNetCoreTodo.Services;
using Microsoft.EntityFrameworkCore;
using System;
using System.Threading.Tasks;
using Xunit;

namespace AspNetCoreTodo.UnitTests
Expand Down
2 changes: 1 addition & 1 deletion chapters/mvc-basics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ In this book, you'll build a to-do app that lets the user add items to their to-
* Web pages and an interface that the user will interact with via their browser, using HTML, CSS, and JavaScript (called the "frontend")
* A login form and security checks so each user's to-do list is kept private

Sound good? Let's built it! If you haven't already created a new ASP.NET Core project using `dotnet new mvc`, follow the steps in the previous chapter. You should be able to build and run the project and see the default welcome screen.
Sound good? Let's build it! If you haven't already created a new ASP.NET Core project using `dotnet new mvc`, follow the steps in the previous chapter. You should be able to build and run the project and see the default welcome screen.
2 changes: 1 addition & 1 deletion chapters/mvc-basics/finish-controller.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public async Task<IActionResult> Index()
{
var items = await _todoItemService.GetIncompleteItemsAsync();

var model = new TodoViewModel()
var model = new TodoViewModel
{
Items = items
};
Expand Down
6 changes: 6 additions & 0 deletions chapters/mvc-basics/use-dependency-injection.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,10 @@ This line tells ASP.NET Core to use the `FakeTodoItemService` whenever the `ITod

`AddSingleton` adds your service to the service container as a **singleton**. This means that only one copy of the `FakeTodoItemService` is created, and it's reused whenever the service is requested. Later, when you write a different service class that talks to a database, you'll use a different approach (called **scoped**) instead. I'll explain why in the *Use a database* chapter.

Once again, since both the `ITodoItemService` and `FakeTodoItemService` reside in the `Services` namespace, you'll also need to add a `using` statement at the top:

```csharp
using AspNetCoreTodo.Services;
```

That's it! When a request comes in and is routed to the `TodoController`, ASP.NET Core will look at the available services and automatically supply the `FakeTodoItemService` when the controller asks for an `ITodoItemService`. Because the services are "injected" from the service container, this pattern is called **dependency injection**.
15 changes: 7 additions & 8 deletions chapters/security-and-identity/authorization-with-roles.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ First, create a new controller:
**Controllers/ManageUsersController.cs**

```csharp
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using AspNetCoreTodo.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using AspNetCoreTodo.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Threading.Tasks;

namespace AspNetCoreTodo.Controllers
{
Expand Down Expand Up @@ -145,13 +144,13 @@ Create a new class in the root of the project called `SeedData`:
**SeedData.cs**

```csharp
using System;
using System.Linq;
using System.Threading.Tasks;
using AspNetCoreTodo.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace AspNetCoreTodo
{
Expand Down
2 changes: 1 addition & 1 deletion chapters/use-a-database/connect-to-a-database.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Connect to a database

There are a few things you need to use Entity Framework Core to connect to a database. Since you used `dotnet new` and the MVC + Individual Auth template to set your project, you've already got them:
There are a few things you need, to use Entity Framework Core to connect to a database. Since you used `dotnet new` and the MVC + Individual Auth template to set your project, you've already got them:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No comma needed here!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea what went through my head at the time of writing.


* **The Entity Framework Core packages**. These are included by default in all ASP.NET Core projects.

Expand Down
6 changes: 6 additions & 0 deletions chapters/use-a-database/update-context.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public DbSet<TodoItem> Items { get; set; }
// ...
```

And set up the `@using` statement for the Models:

```csharp
using AspNetCoreTodo.Models;
```

A `DbSet` represents a table or collection in the database. By creating a `DbSet<TodoItem>` property called `Items`, you're telling Entity Framework Core that you want to store `TodoItem` entities in a table called `Items`.

You've updated the context class, but now there's one small problem: the context and database are now out of sync, because there isn't actually an `Items` table in the database. (Just updating the code of the context class doesn't change the database itself.)
Expand Down