Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Starting at The Bottom With Your Data Access Layer Slides

Uploaded by

Borce Markovski
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Starting at The Bottom With Your Data Access Layer Slides

Uploaded by

Borce Markovski
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Starting at the Bottom

with Your Data Access


Layer
Kevin Dockx
Architect

@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

void Task Task<T>


void
- Only advised for event handlers
Async Return - Hard to handle exceptions
Types - Difficult to test
- No easy way to notify the calling code of
their status
Task and Task<T>
- Represents a single operation that returns
Async Return nothing (Task) or a value of type T (Task<T>)
Types and usually executes asynchronously.
- Represents the execution of the
asynchronous method
Task and Task<T>
- Status, IsCanceled, IsCompleted, and
IsFaulted properties allow determining the
Async Return state of a Task
Types - Gets status complete when the method
completes (and optionally returns the
method value as the task’s result)
Through Task and Task<T>
we can know the state of an
asynchronous operation
Tasks are managed by a
state machine generated by
the compiler when a
method is marked with the
async modifier
public class StateMachineExample : IAsyncStateMachine
{

public void MoveNext()


{
// move to the next state
}

public void SetStateMachine(IAsyncStateMachine stateMachine)


{
// set the state machine
}
}

State Machine Example


An implementation of IAsyncStateMachine
Task-based Asynchronous Pattern (TAP)
Async Patterns:
- Best practice today
TAP, EAP, and - Based on Task and Task<T> (and other
APM async return types)
Event-based Asynchronous Pattern (EAP)
- Multithreading without the complexity
Async Patterns: - MethodNameAsync (method)
TAP, EAP, and - MethodNameCompleted (event)
APM - MethodNameAsyncCancel (method)

Mainly used before .NET 4 (full framework)


Asynchronous Programming Model (APM)
- Async operations are implemented as two
Async Patterns: methods named BeginOperationName and
TAP, EAP, and EndOperationName
APM FileStream used to default to this model,
which has since been replaced by TAP
Demo

Starting from scratch with a DAL


Without the repository pattern, we’re likely to …
The Repository - … run into code duplication
Pattern - … create error-prone code
- … make it harder to test the consuming class
The repository pattern
An abstraction that reduces complexity and aims to make the code, safe
for the repository implementation, persistence ignorant
When using the repository pattern, we can
achieve …
The Repository - … less code duplication
Pattern - … less error-prone code
- … better testability of the consuming class
Persistence ignorant
Switching out the persistence technology is not the main purpose of the
repository pattern, choosing the best one for each repository method is
Demo

Designing the repository contract


An interface is a contract, which makes the
GetBooksAsync() definition a contract detail
Contracts and
Using the async await keywords tell us how
Async Modifiers the method is implemented, which makes it an
implementation detail
Demo

Implementing the repository contract


Summary
Marking a method with the async modifier
- Ensures that the await keyword can be
used inside that method
- Transforms the method into a
state machine
Summary Using the await operator
- Tells the compiler that the async method
can't continue until the awaited
asynchronous process is complete
- Returns control to the caller of the async
method
Summary A task
- Represents a single operation that
returns nothing (Task) or a value of type
T (Task<T>)
- Represents the execution of the
async method
Summary
Through Task and Task<T> we can know
the state of an async operation
Tasks are managed by a state machine
generated by the compiler when a method
is marked with the async modifier
Asynchronously Reading Resources

You might also like