Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSharp Assíncrono

Alex Tercete
September 22, 2014

CSharp Assíncrono

Apresentação realizada como parte da iniciativa VTEX Tech Talks.
--
Presentation made as a part of the VTEX Tech Talks initiative.

Alex Tercete

September 22, 2014
Tweet

More Decks by Alex Tercete

Other Decks in Technology

Transcript

  1. t

  2. t

  3. t

  4. C#

  5. .NET 1.1+ APM public interface IAsynchronousProgrammingModel<T> { IAsyncResult BeginSomething( AsyncCallback

    callback, object state); T EndSomething(IAsyncResult result); } https://gist.github.com/alextercete/f34691cb9e4df4d2e78b
  6. .NET 1.1+ APM public class AsynchronousProgrammingModelWrapper<T> { private readonly IAsynchronousProgrammingModel<T>

    _apm; // Constructor public Task<T> SomethingAsync() { return Task.Factory.FromAsync<T>( _apm.BeginSomething, _apm.EndSomething, null); } } https://gist.github.com/alextercete/f34691cb9e4df4d2e78b
  7. .NET 1.1+ APM public class AsynchronousProgrammingModelWrapper<T> { private readonly IAsynchronousProgrammingModel<T>

    _apm; // Constructor public Task<T> SomethingAsync() { return Task.Factory.FromAsync<T>( _apm.BeginSomething, _apm.EndSomething, null); } } https://gist.github.com/alextercete/f34691cb9e4df4d2e78b
  8. .NET 4.0+ TPL public interface IDependency<T> { Task<object> BeforeAsync(); Task<T>

    AfterAsync(object result); } https://gist.github.com/alextercete/a011bafde7187104303b
  9. .NET 4.0+ TPL public interface IDependency<T> { Task<object> BeforeAsync(); Task<T>

    AfterAsync(object result); } https://gist.github.com/alextercete/a011bafde7187104303b
  10. .NET 4.0+ TPL public class TaskParallelLibrary<T> { private readonly IDependency<T>

    _dependency; // Constructor public Task<T> SomethingAsync() { return _dependency.BeforeAsync().ContinueWith( previous => _dependency.AfterAsync(previous.Result) ).Unwrap(); } } https://gist.github.com/alextercete/a011bafde7187104303b
  11. .NET 4.0+ TPL public class TaskParallelLibrary<T> { private readonly IDependency<T>

    _dependency; // Constructor public Task<T> SomethingAsync() { return _dependency.BeforeAsync().ContinueWith( previous => _dependency.AfterAsync(previous.Result) ).Unwrap(); } } https://gist.github.com/alextercete/a011bafde7187104303b
  12. .NET 4.5+ async/await public class TaskParallelLibrary<T> { private readonly IDependency<T>

    _dependency; // Constructor public async Task<T> SomethingAsync() { var result = await _dependency.BeforeAsync(); return await _dependency.AfterAsync(result); } } https://gist.github.com/alextercete/a011bafde7187104303b
  13. .NET 4.5+ async/await public class TaskParallelLibrary<T> { private readonly IDependency<T>

    _dependency; // Constructor public async Task<T> SomethingAsync() { var result = await _dependency.BeforeAsync(); return await _dependency.AfterAsync(result); } } https://gist.github.com/alextercete/a011bafde7187104303b
  14. public interface IDependency<T> { Task<object> BeforeAsync(); Task<T> AfterAsync(object result); Task<T>

    SomethingAsync(); Task<T> SomethingAsync(CancellationToken token); Task<T> SomethingAsync( Func<object, Task<T>> callback); } https://gist.github.com/alextercete/1acbaf93605383fcea51
  15. public class TheAlmostAsync<T> { // Private fields and constructor public

    T Something() { return SomethingAsync().Result; } public async Task<T> SomethingAsync() { var result = await _dependency.BeforeAsync(); return await _dependency.AfterAsync(result); } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  16. public class TheAlmostAsync<T> { // Private fields and constructor public

    T Something() { return SomethingAsync().Result; } public async Task<T> SomethingAsync() { var result = await _dependency.BeforeAsync(); return await _dependency.AfterAsync(result); } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  17. public class TheAlmostAsync<T> { // Private fields and constructor public

    T Something() { return SomethingAsync().Result; } public async Task<T> SomethingAsync() { var result = await _dependency.BeforeAsync(); return await _dependency.AfterAsync(result); } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  18. public class TheAlmostAsyncFixed<T> { // Private fields and constructor public

    T Something() { return SomethingAsync().Result; } public async Task<T> SomethingAsync() { var result = await _dependency.BeforeAsync().ConfigureAwait(false); return await _dependency.AfterAsync(result).ConfigureAwait(false); } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  19. public class ThePseudoAsync<T> { public async Task<T> SomethingAsync() { return

    default(T); } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  20. public class ThePseudoAsync<T> { public async Task<T> SomethingAsync() { return

    default(T); } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  21. public class TheSuperAsync<T> { // Private fields and constructor public

    async Task<T> SomethingAsync() { return await _dependency.SomethingAsync( async result => await _dependency.AfterAsync(result)); } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  22. public class TheSuperAsync<T> { // Private fields and constructor public

    async Task<T> SomethingAsync() { return await _dependency.SomethingAsync( async result => await _dependency.AfterAsync(result)); } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  23. public class TheSuperAsync<T> { // Private fields and constructor public

    async Task<T> SomethingAsync() { return await _dependency.SomethingAsync( async result => await _dependency.AfterAsync(result)); } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  24. public class TheSuperAsyncFixed<T> { // Private fields and constructor public

    Task<T> SomethingAsync() { return _dependency.SomethingAsync( result => _dependency.AfterAsync(result)); } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  25. public class TheNullReturn<T> { // Private fields and constructor public

    Task<T> AfterAsync(object result) { if (result == null) return null; return _dependency.AfterAsync(result); } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  26. public class TheNullReturn<T> { // Private fields and constructor public

    Task<T> AfterAsync(object result) { if (result == null) return null; return _dependency.AfterAsync(result); } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  27. public class TheNullReturnFixed<T> where T : class { // Private

    fields and constructor public Task<T> AfterAsync(object result) { if (result == null) return Task.FromResult<T>(null); return _dependency.AfterAsync(result); } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  28. public class TheMissingContext<T> { // Private fields and constructor public

    async Task<T> SomethingAsync() { var result = await _dependency.BeforeAsync() .ConfigureAwait(false); // HttpContext.Current == null return await _dependency.AfterAsync(result) .ConfigureAwait(false); } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  29. public class TheMissingContextFixed<T> { // Private fields and constructor public

    async Task<T> SomethingAsync() { var context = HttpContext.Current; var result = await _dependency.BeforeAsync() .ConfigureAwait(false); HttpContext.Current = context; return await _dependency.AfterAsync(result) .ConfigureAwait(false); } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  30. public class TheForgottenCall<T> { // Private fields and constructor public

    Task SomethingAsync() { SomethingNotAsync(); return CompletedTask; } private async void SomethingNotAsync() { await _dependency.BeforeAsync(); } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  31. https://gist.github.com/alextercete/1acbaf93605383fcea51 public class TheForgottenCall<T> { // Private fields and constructor

    public Task SomethingAsync() { SomethingNotAsync(); return CompletedTask; } private async void SomethingNotAsync() { await _dependency.BeforeAsync(); } } private static Task CompletedTask { get { return Task.FromResult(0); } }
  32. public class TheForgottenCall<T> { // Private fields and constructor public

    Task SomethingAsync() { SomethingNotAsync(); return CompletedTask; } private async void SomethingNotAsync() { await _dependency.BeforeAsync(); } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  33. public class TheForgottenCallFixed<T> { // Private fields and constructor public

    async Task SomethingAsync() { await SomethingNowAsync(); } private async Task SomethingNowAsync() { await _dependency.BeforeAsync(); } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  34. public class TheForgottenCall<T> { // Private fields and constructor public

    Task AfterAsync(object result) { _dependency.AfterAsync(result); return CompletedTask; } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  35. public class TheForgottenCall<T> { // Private fields and constructor public

    Task AfterAsync(object result) { _dependency.AfterAsync(result); return CompletedTask; } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  36. public class TheForgottenCallFixed<T> { // Private fields and constructor public

    async Task AfterAsync(object result) { await _dependency.AfterAsync(result); } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  37. public class TheForgottenCallReallyFixed<T> { // Private fields and constructor public

    Task AfterAsync(object result) { return _dependency.AfterAsync(result); } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  38. public class TheIgnoredCancellation<T> { // Private fields and constructor public

    Task<T> SomethingAsync(CancellationToken token) { return _dependency.SomethingAsync(); } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  39. public class TheIgnoredCancellation<T> { // Private fields and constructor public

    Task<T> SomethingAsync(CancellationToken token) { return _dependency.SomethingAsync(); } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  40. public class TheIgnoredCancellationFixed<T> { // Private fields and constructor public

    Task<T> SomethingAsync(CancellationToken token) { return _dependency.SomethingAsync(token); } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  41. public class TheThreadsDispute<T> { // Private fields and constructor public

    T Something() { return Task.Run(() => _dependency.SomethingAsync()).Result; } public IList<T> Somethings(int count) { var results = new List<T>(count); Parallel.For(0, count, i => results[i] = _dependency.SomethingAsync().Result); return results; } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  42. public class TheThreadsDispute<T> { // Private fields and constructor public

    T Something() { return Task.Run(() => _dependency.SomethingAsync()).Result; } public IList<T> Somethings(int count) { var results = new List<T>(count); Parallel.For(0, count, i => results[i] = _dependency.SomethingAsync().Result); return results; } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  43. public class TheThreadsDispute<T> { // Private fields and constructor public

    T Something() { return Task.Run(() => _dependency.SomethingAsync()).Result; } public IList<T> Somethings(int count) { var results = new List<T>(count); Parallel.For(0, count, i => results[i] = _dependency.SomethingAsync().Result); return results; } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  44. public class TheThreadsDisputeFixed<T> { // Private fields and constructor public

    T Something() { return _dependency.SomethingAsync().Result; } public IList<T> Somethings(int count) { var results = new List<T>(count); Parallel.For(0, count, i => results[i] = _dependency.SomethingAsync().Result); return results; } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  45. public class TheThreadsDisputeFixed<T> { // Private fields and constructor public

    T Something() { return _dependency.SomethingAsync().Result; } public IList<T> Somethings(int count) { var tasks = Enumerable.Range(0, count).Select( i => _dependency.SomethingAsync()); return Task.WhenAll(tasks).Result; } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  46. public class TheThreadsDisputeReallyFixed<T> { // Private fields and constructor public

    Task<T> SomethingAsync() { return _dependency.SomethingAsync(); } public async Task<IList<T>> SomethingsAsync(int count) { var tasks = Enumerable.Range(0, count).Select( i => _dependency.SomethingAsync()); return await Task.WhenAll(tasks); } } https://gist.github.com/alextercete/1acbaf93605383fcea51
  47. Referências Blogs: – http://blogs.msdn.com/b/pfxteam/ – http://blog.stephencleary.com/ Talks: – The zen

    of async: Best practices for best performance – Async'ing Your Way to a Successful App with .NET – Async in ASP.NET
  48. Créditos – http://thenounproject.com/term/cash-register/41958/ – http://thenounproject.com/term/lunch/62503/ – http://thenounproject.com/term/server/33544/ – http://thenounproject.com/term/server/32430/ –

    http://thenounproject.com/term/server/32429/ – http://thenounproject.com/term/server/32431/ – http://thenounproject.com/term/lock/5427/ – http://thenounproject.com/term/hour-glass/16705/ – http://thenounproject.com/term/money/27047/ – http://thenounproject.com/term/printer/1235/