Saturday, February 22, 2014

Async methods in ASP.NET 4.5

This one is an useful read from Scott Hanselman's post, particularly the comments from Stephen Cleary

http://www.hanselman.com/blog/TheMagicOfUsingAsynchronousMethodsInASPNET45PlusAnImportantGotcha.aspx

Friday, August 30, 2013 9:54:09 AM UTC
@Ben, @Daniel: You should not use Task.Run on the server side. It makes sense on the client (to free up the UI thread), but it does not make sense on the server side (you're freeing up a worker thread... by using another worker thread). This is covered in an excellent video on Channel9.

@Daniel: You should not use Task.Result in async code; it's easy to cause deadlocks.

@Ben: All three tasks are running when you call the first await, so even though that method is suspended, the other tasks are already running.

@MarcelWijnands: It's the difference between "wait for the first task, then continue the method, wait for the second task, then continue the method, wait for the third task, then continue the method" and "wait for all tasks, then continue the method". The Task.WhenAll approach has less switching back and forth.


Also from Stephen, never block on async code on a synchronous method (using Task.Wait()) like so

Figure 3 A Common Deadlock Problem When Blocking on Async Code

1.public static class DeadlockDemo
2.{
3.  private static async Task DelayAsync()
4.  {
5.    await Task.Delay(1000);
6.  }
7.  // This method causes a deadlock when called in a GUI or ASP.NET context.
8.  public static void Test()
9.  {
10.    // Start the delay.
11.    var delayTask = DelayAsync();
12.    // Wait for the delay to complete.
13.    delayTask.Wait();
14.  }
15.}

Monday, February 17, 2014

A scenario when to use Func/Action

Can you tell the difference between the following 2 code snippets?

1) Using a Func/Action within a static method

public static class ExpensiveMethodWithAFuncClass<T>
{
      public static readonly Func<T> ExpensiveMethod= ExpensiveMethodImplementation();
      private static Func<T> ExpensiveMethodImplementaion
     {
           //some expensive operations here
          // returns a compiled delegate
     }
}

2)  Same class without using a Func<T>

public static class ExpensiveMethodClass<T>
{
      public static T ExpensiveMethod
     {
         //build the expression here, compile and invoke the expression
      }
}

Answer :  In 1) when we call ExpensiveMethod, the implementation ExpensiveMethodImplementation() is executed only once.

In the case of 2) we will execute ExpensiveMethod every time we call it.

 If the implementation is an expensive operation, 1) will benefit from using the delegate Func<T>

Saturday, February 15, 2014

Data Binding in WPF

To use Data Binding in WPF implement INotifyPropertyChanged (INPC) for your datasource (typically a POCO object) which will serve as the datacontext for your View.

Implementing INPC requires calling an event called PropertyChanged from within your POCO class, whenever one of its property changes. The signature for the event is

public event PropertyChangedEventhandler PropertyChanged;

To call this event, by convention we define a helper method called OnPropertyChanged like so

private void OnPropertyChanged([CallerMemberName] string title="")
{
     if (PropertyChanged != null)
     {
          PropertyChanged(this, new PropertyChangedEventArgs(caller));
      }
}

This helper method should be called from every  property setter in the POCO class and that's how we get  Data Binding in WPF.

private string _title;
public string Title
{
   get { return _title;}
   set {
           _title = value;
           OnPropertyChanged();
         }
}

Simple and neat!  Do ko.observable() and $watch sound familiar?