text
stringlengths
0
13.4k
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AspNetCoreTodo.Models;
32
Add a service class
namespace AspNetCoreTodo.Services
{
public interface ITodoItemService
{
Task<TodoItem[]> GetIncompleteItemsAsync();
}
}
Note that the namespace of this file is AspNetCoreTodo.Services .
Namespaces are a way to organize .NET code files, and it's customary for
the namespace to follow the directory the file is stored in
( AspNetCoreTodo.Services for files in the Services directory, and so on).
Because this file (in the AspNetCoreTodo.Services namespace) references
the TodoItem class (in the AspNetCoreTodo.Models namespace), it needs
to include a using statement at the top of the file to import that
namespace. Without the using statement, you'll see an error like:
The type or namespace name 'TodoItem' could not be found (are you
missing a using directive or an assembly reference?)
Since this is an interface, there isn't any actual code here, just the
definition (or method signature) of the GetIncompleteItemsAsync
method. This method requires no parameters and returns a
Task<TodoItem[]> .
If this syntax looks confusing, think: "a Task that contains an array
of TodoItems".
The Task type is similar to a future or a promise, and it's used here
because this method will be asynchronous. In other words, the method
may not be able to return the list of to-do items right away because it
needs to go talk to the database first. (More on this later.)
Create the service class
33
Add a service class
Now that the interface is defined, you're ready to create the actual
service class. I'll cover database code in depth in the Use a database
chapter, so for now you'll just fake it and always return two hard-coded
items:
Services/FakeTodoItemService.cs
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AspNetCoreTodo.Models;
namespace AspNetCoreTodo.Services
{
public class FakeTodoItemService : ITodoItemService
{
public Task<TodoItem[]> GetIncompleteItemsAsync()