Last active
August 29, 2015 13:57
-
-
Save tecnocrata/9731881 to your computer and use it in GitHub Desktop.
Patrón para ejecución de procesos largos con notificación-actualización de UI de resultados parciales
This file contains hidden or 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
| //Ambas soluciones estan propuestas por Jorge Fioranelli: jorge.fioranelli@gmail.com | |
| public interface IStep | |
| { | |
| event EventHandler<StepEventArgs> Started; | |
| event EventHandler<StepEventArgs> Finished; | |
| void Execute(); | |
| } | |
| public class StepEventArgs : EventArgs | |
| { | |
| public string Message { get; set; } | |
| } | |
| public class StepA : IStep | |
| { | |
| public event EventHandler<StepEventArgs> Started; | |
| public event EventHandler<StepEventArgs> Finished; | |
| public void Execute() | |
| { | |
| OnStarted(new StepEventArgs{ Message = "Paso A Iniciado" }); | |
| // Ejecutar las acciones del Paso A aqui | |
| OnFinished(new StepEventArgs{ Message = "Paso A Finalizado" }); | |
| } | |
| protected virtual void OnStarted(StepEventArgs args) | |
| { | |
| if(Started != null) | |
| Started(this, args); | |
| } | |
| protected virtual void OnFinished(StepEventArgs args) | |
| { | |
| if(Finished != null) | |
| Finished(this, args); | |
| } | |
| } | |
| public class StepB : IStep | |
| { | |
| public event EventHandler<StepEventArgs> Started; | |
| public event EventHandler<StepEventArgs> Finished; | |
| public void Execute() | |
| { | |
| OnStarted(new StepEventArgs{ Message = "Paso B Iniciado" }); | |
| // Ejecutar las acciones del Paso B aqui | |
| OnFinished(new StepEventArgs{ Message = "Paso B Finalizado" }); | |
| } | |
| protected virtual void OnStarted(StepEventArgs args) | |
| { | |
| if(Started != null) | |
| Started(this, args); | |
| } | |
| protected virtual void OnFinished(StepEventArgs args) | |
| { | |
| if(Finished != null) | |
| Finished(this, args); | |
| } | |
| } | |
| public class Engine | |
| { | |
| public IEnumerable<IStep> Steps | |
| { | |
| get | |
| { | |
| yield return new StepA(); | |
| yield return new StepB(); | |
| } | |
| } | |
| public void ExecuteSteps() | |
| { | |
| foreach (var step in Steps) | |
| { | |
| step.Started += Step_Started; | |
| step.Finished += Step_Finished; | |
| step.Execute(); | |
| step.Started -= Step_Started; | |
| step.Finished -= Step_Finished; | |
| } | |
| } | |
| void Step_Started(object sender, StepEventArgs e) | |
| { | |
| Console.WriteLine(e.Message); | |
| } | |
| void Step_Finished(object sender, StepEventArgs e) | |
| { | |
| Console.WriteLine(e.Message); | |
| } | |
| } | |
| Otra solucion seria: | |
| public interface ILogger | |
| { | |
| void WriteMessage(string message); | |
| } | |
| public class ConsoleLogger : ILogger | |
| { | |
| public void WriteMessage(string message) | |
| { | |
| Console.WriteLine(message); | |
| } | |
| } | |
| public class StepA | |
| { | |
| private readonly ILogger logger; | |
| public StepA(ILogger logger) | |
| { | |
| this.logger = logger; | |
| } | |
| public void Execute() | |
| { | |
| logger.WriteMessage("Paso A Iniciado"); | |
| // Executar las acciones del paso A aqui | |
| logger.WriteMessage("Paso A Finalizado"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment