Starting at The Bottom With Your Data Access Layer Slides
Starting at The Bottom With Your Data Access Layer Slides
@KevinDockx | www.kevindockx.com
Coming Up Keywords async & await
The purpose of Task and Task<T>
The data access layer and
repository pattern
Naming guidelines, conventions
and best practices
Marking a method with the async modifier
- Ensures that the await keyword can be used
The async / await inside that method
Keywords - Transforms the method into a state machine
(generated by the compiler)
Using the await operator
- Tells the compiler that the asynchronous
method can't continue until the awaited
The async / await asynchronous process is complete
Keywords - Returns control to the caller of the
asynchronous method (potentially right
back up to the thread being freed)
A method that is not marked with the async
modifier should not be awaited
The async / await
When an asynchronous method doesn't
Keywords contain an await operator, the method simply
executes as a synchronous method does
The async / await Keywords
public async IActionResult MyActionThatCallsGetBooksAsync()
{ …
var books = await GetBooksAsync();
…
}
public async Task<IEnumerable<Book>> GetBooksAsync()
{
var bookIds = CalculateBookIdsForUser();
var books = await _context.Books.Where(b =>
bookIds.Contains(b.Id)).ToListAsync();
return books;
}
public IEnumerable<Guid> CalculateBookIdsForUser()
{ …
return bookIdsForUser;
}
The async / await Keywords
public async IActionResult MyActionThatCallsGetBooksAsync()
{ …
var books = await GetBooksAsync();
…
}
public async Task<IEnumerable<Book>> GetBooksAsync()
{
var bookIds = CalculateBookIdsForUser();
var books = await _context.Books.Where(b =>
bookIds.Contains(b.Id)).ToListAsync();
return books;
}
public IEnumerable<Guid> CalculateBookIdsForUser()
{ …
return bookIdsForUser;
}
The async / await Keywords
public async IActionResult MyActionThatCallsGetBooksAsync()
{ …
var books = await GetBooksAsync();
…
}
public async Task<IEnumerable<Book>> GetBooksAsync()
{
var bookIds = CalculateBookIdsForUser();
var books = await _context.Books.Where(b =>
bookIds.Contains(b.Id)).ToListAsync();
return books;
}
public IEnumerable<Guid> CalculateBookIdsForUser()
{ …
return bookIdsForUser;
}
The async / await Keywords
public async IActionResult MyActionThatCallsGetBooksAsync()
{ …
var books = await GetBooksAsync();
…
}
public async Task<IEnumerable<Book>> GetBooksAsync()
{
var bookIds = CalculateBookIdsForUser();
var books = await _context.Books.Where(b =>
bookIds.Contains(b.Id)).ToListAsync();
return books;
}
public IEnumerable<Guid> CalculateBookIdsForUser()
{ …
return bookIdsForUser;
}
The async / await Keywords
public async IActionResult MyActionThatCallsGetBooksAsync()
{ …
var books = await GetBooksAsync();
…
}
public async Task<IEnumerable<Book>> GetBooksAsync()
{
var bookIds = CalculateBookIdsForUser();
var books = await _context.Books.Where(b =>
bookIds.Contains(b.Id)).ToListAsync();
return books;
}
public IEnumerable<Guid> CalculateBookIdsForUser()
{ …
return bookIdsForUser;
}
The async / await Keywords
public async IActionResult MyActionThatCallsGetBooksAsync()
{ …
var books = await GetBooksAsync();
…
}
public async Task<IEnumerable<Book>> GetBooksAsync()
{
var bookIds = CalculateBookIdsForUser();
var books = await _context.Books.Where(b =>
bookIds.Contains(b.Id)).ToListAsync();
return books;
}
public IEnumerable<Guid> CalculateBookIdsForUser()
{ …
return bookIdsForUser;
}
The async / await Keywords
public async IActionResult MyActionThatCallsGetBooksAsync()
{ …
var books = await GetBooksAsync();
…
}
public async Task<IEnumerable<Book>> GetBooksAsync()
{
var bookIds = CalculateBookIdsForUser();
var books = await _context.Books.Where(b =>
bookIds.Contains(b.Id)).ToListAsync();
return books;
}
public IEnumerable<Guid> CalculateBookIdsForUser()
{ …
return bookIdsForUser;
}
The async / await Keywords
public async IActionResult MyActionThatCallsGetBooksAsync()
{ …
var books = await GetBooksAsync();
…
}
public async Task<IEnumerable<Book>> GetBooksAsync()
{
var bookIds = CalculateBookIdsForUser();
var books = await _context.Books.Where(b =>
bookIds.Contains(b.Id)).ToListAsync();
return books;
}
public IEnumerable<Guid> CalculateBookIdsForUser()
{ …
return bookIdsForUser;
}
The async / await Keywords
public async IActionResult MyActionThatCallsGetBooksAsync()
{ …
var books = await GetBooksAsync();
…
}
public async Task<IEnumerable<Book>> GetBooksAsync()
{
var bookIds = CalculateBookIdsForUser();
var books = await _context.Books.Where(b =>
bookIds.Contains(b.Id)).ToListAsync();
return books;
}
public IEnumerable<Guid> CalculateBookIdsForUser()
{ …
return bookIdsForUser;
}
Async Return Types