text stringlengths 0 13.4k |
|---|
{ |
var item1 = new TodoItem |
{ |
Title = "Learn ASP.NET Core", |
DueAt = DateTimeOffset.Now.AddDays(1) |
}; |
var item2 = new TodoItem |
{ |
Title = "Build awesome apps", |
DueAt = DateTimeOffset.Now.AddDays(2) |
}; |
return Task.FromResult(new[] { item1, item2 }); |
} |
} |
} |
This FakeTodoItemService implements the ITodoItemService interface |
but always returns the same array of two TodoItem s. You'll use this to |
test the controller and view, and then add real database code in Use a |
database. |
34 |
Add a service class |
35 |
Use dependency injection |
Use dependency injection |
Back in the TodoController , add some code to work with the |
ITodoItemService : |
public class TodoController : Controller |
{ |
private readonly ITodoItemService _todoItemService; |
public TodoController(ITodoItemService todoItemService) |
{ |
_todoItemService = todoItemService; |
} |
public IActionResult Index() |
{ |
// Get to-do items from database |
// Put items into a model |
// Pass the view to a model and render |
} |
} |
Since ITodoItemService is in the Services namespace, you'll also need |
to add a using statement at the top: |
using AspNetCoreTodo.Services; |
The first line of the class declares a private variable to hold a reference to |
the ITodoItemService . This variable lets you use the service from the |
Index action method later (you'll see how in a minute). |
The public TodoController(ITodoItemService todoItemService) line |
defines a constructor for the class. The constructor is a special method |
that is called when you want to create a new instance of a class (the |
36 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.