Last active
December 10, 2015 08:38
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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