text
stringlengths
0
13.4k
Since Humanizer will be used to rewrite dates rendered in the view, you
can use it directly in the view itself. First, add a @using statement at the
top of the view:
Views/Todo/Index.cshtml
@model TodoViewModel
@using Humanizer
// ...
Then, update the line that writes the DueAt property to use Humanizer's
Humanize method:
<td>@item.DueAt.Humanize()</td>
Now the dates are much more readable:
46
Add external packages
There are packages available on NuGet for everything from parsing XML
to machine learning to posting to Twitter. ASP.NET Core itself, under the
hood, is nothing more than a collection of NuGet packages that are
added to your project.
The project file created by dotnet new mvc includes a single
reference to the Microsoft.AspNetCore.All package, which is a
convenient "metapackage" that references all of the other
ASP.NET Core packages you need for a typical project. That way,
you don't need to have hundreds of package references in your
project file.
In the next chapter, you'll use another set of NuGet packages (a system
called Entity Framework Core) to write code that interacts with a
database.
47
Use a database
Use a database
Writing database code can be tricky. Unless you really know what you're
doing, it's a bad idea to paste raw SQL query strings into your application
code. An object-relational mapper (ORM) makes it easier to write code
that interacts with a database by adding a layer of abstraction between
your code and the database itself. Hibernate in Java and ActiveRecord in
Ruby are two well-known ORMs.
There are a number of ORMs for .NET, including one built by Microsoft
and included in ASP.NET Core by default: Entity Framework Core. Entity
Framework Core makes it easy to connect to a number of different
database types, and lets you use C# code to create database queries that
are mapped back into C# models (POCOs).
Remember how creating a service interface decoupled the
controller code from the actual service class? Entity Framework
Core is like a big interface over your database. Your C# code can
stay database-agnostic, and you can swap out different providers
depending on the underlying database technology.
Entity Framework Core can connect to relational databases like SQL
Server, PostgreSQL, and MySQL, and also works with NoSQL (document)
databases like Mongo. During development, you'll use SQLite in this
project to make things easy to set up.
48