Created
January 5, 2011 23:49
-
-
Save joshka/767252 to your computer and use it in GitHub Desktop.
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
public class MainViewModel : ViewModelBase | |
{ | |
public MainViewModel() | |
{ | |
ActivityProcessor processor = new ActivityProcessor(); | |
GetUnicornsCommand = processor.CreateCommand(GetUnicorns).WithContinuation(HandleGetUnicornErrors); | |
} | |
IEnumerable<IActivity> GetUnicorns() | |
{ | |
yield return new BusyIndicator(true); // show that we're busy | |
yield return new UnicornDownloader(); // get the unicorns! | |
yield return new BusyIndicator(false); // we're no longer busy | |
} | |
void HandleGetUnicornErrors(ActivityContext activityContext) | |
{ | |
foreach (var exception in activityContext.Exceptions) | |
{ | |
if (exception.InnerException is AnimalNotFoundException) | |
{ | |
// Log / show message / ... | |
actitivityContext.Continue = true; | |
} | |
else | |
{ | |
// do something with errors that we can't handle. | |
} | |
} | |
} | |
} | |
public class ActivityCommand | |
{ | |
private readonly Func<IEnumerable<IActivity>> _execute; | |
private Action<ActivityContext> _continuation; | |
public void WithContinuation(Action<ActivityContext> continuation) | |
{ | |
_continuation = continuation; | |
} | |
public void Execute(object parameter) | |
{ | |
_processor.Process(_execute, _continuation, _completed); | |
} | |
} | |
public class ActivityProcessor | |
{ | |
public void Process(Func<IEnumerable<IActivity>> execute, Action<ActivityContext> continuation, Action<ActivityContext> completedAction) | |
{ | |
var activities = execute(); | |
foreach (var activity in activities) | |
{ | |
try | |
{ | |
activity.Execute(); | |
} | |
catch (Exception e) | |
{ | |
var activityException = new ActivityException("An error occurred in the activity", e); | |
activityContext.Exceptions.Add(activityException); | |
continuation(activityContext); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment