Skip to content

Instantly share code, notes, and snippets.

@jpolvora
Last active December 10, 2015 08:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpolvora/4409138 to your computer and use it in GitHub Desktop.
Save jpolvora/4409138 to your computer and use it in GitHub Desktop.
By using ActionResult, you can yield an inline IResult by constructing it with a delegate (Action) via Lambda Expression. You will receive an action as parameter, which you must invoke in order to do the completion of the IResult.
//Usage:
public IEnumerable<IResult> Logout()
{
yield return new ActionResult(completed =>
{
var operation = WebContext.Current.Authentication.Logout(false);
operation.Completed += (sender, args) => completed();
});
}
//usage with unsubscription of the event
public IEnumerable<IResult> Logout()
{
yield return new ActionResult(completed =>
{
var operation = WebContext.Current.Authentication.Logout(false);
EventHandler operationOnCompleted = null;
operationOnCompleted = (sender, args) =>
{
operation.Completed -= operationOnCompleted;
completed();
};
operation.Completed += operationOnCompleted;
});
}
public class ActionResult : IResult
{
private readonly Action<System.Action> _completed;
public ActionResult(Action<System.Action> completed)
{
_completed = completed;
}
#region Implementation of IResult
public void Execute(ActionExecutionContext context)
{
_completed(Invoke);
}
void Invoke()
{
Completed(this, new ResultCompletionEventArgs());
}
public event EventHandler<ResultCompletionEventArgs> Completed = delegate { };
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment