Commit 4d3d498

mokhan <mokhan@ce5e1baf-6525-42e4-a1b2-857ea38da20a>
2009-03-30 00:13:28
refactoring how the checkforupdatespresenter checks for an update.
git-svn-id: https://svn.xp-dev.com/svn/mokhan-mo.money@124 ce5e1baf-6525-42e4-a1b2-857ea38da20a
1 parent f8745c1
trunk/product/MyMoney/boot/container/registration/wire_up_the_essential_services_into_the.cs
@@ -1,3 +1,4 @@
+using System.ComponentModel;
 using System.Threading;
 using System.Windows.Forms;
 using MoMoney.Infrastructure.Container;
@@ -32,6 +33,7 @@ namespace MoMoney.boot.container.registration
                         }
                         return SynchronizationContext.Current;
                     });
+            registration.singleton<AsyncOperation>(() => AsyncOperationManager.CreateOperation(null));
         }
     }
 }
\ No newline at end of file
trunk/product/MyMoney/Infrastructure/Threading/AsynchronousCommandProcessor.cs
@@ -9,12 +9,13 @@ namespace MoMoney.Infrastructure.Threading
     {
         readonly Queue<ICommand> queued_commands;
         readonly EventWaitHandle manual_reset;
-        Thread worker_thread;
+        readonly IList<Thread> worker_threads;
         bool keep_working;
 
         public AsynchronousCommandProcessor()
         {
             queued_commands = new Queue<ICommand>();
+            worker_threads = new List<Thread>();
             manual_reset = new ManualResetEvent(false);
         }
 
@@ -37,8 +38,9 @@ namespace MoMoney.Infrastructure.Threading
         {
             reset_thread();
             keep_working = true;
-            worker_thread = new Thread(run_commands);
+            var worker_thread = new Thread(run_commands);
             worker_thread.SetApartmentState(ApartmentState.STA);
+            worker_threads.Add(worker_thread);
             worker_thread.Start();
         }
 
trunk/product/MyMoney/Presentation/Presenters/updates/CheckForUpdatesPresenter.cs
@@ -1,10 +1,8 @@
 using MoMoney.Domain.Core;
-using MoMoney.Infrastructure.Threading;
 using MoMoney.Presentation.Core;
-using MoMoney.Presentation.Model.updates;
 using MoMoney.Presentation.Presenters.Commands;
 using MoMoney.Presentation.Views.updates;
-using MoMoney.Tasks.infrastructure;
+using MoMoney.Tasks.infrastructure.updating;
 using MoMoney.Utility.Core;
 
 namespace MoMoney.Presentation.Presenters.updates
@@ -22,13 +20,13 @@ namespace MoMoney.Presentation.Presenters.updates
         readonly ICheckForUpdatesView view;
         readonly IUpdateTasks tasks;
         readonly IRestartCommand command;
-        readonly ICommandProcessor processor;
+        readonly IDisplayNextAvailableVersion display_version_command;
 
         public CheckForUpdatesPresenter(ICheckForUpdatesView view, IUpdateTasks tasks, IRestartCommand command,
-                                        ICommandProcessor processor)
+                                        IDisplayNextAvailableVersion display_version_command)
         {
             this.view = view;
-            this.processor = processor;
+            this.display_version_command = display_version_command;
             this.tasks = tasks;
             this.command = command;
         }
@@ -36,11 +34,7 @@ namespace MoMoney.Presentation.Presenters.updates
         public void run()
         {
             view.attach_to(this);
-            processor.add(
-                new RunQueryCommand<ApplicationVersion>(
-                    view,
-                    new ProcessQueryCommand<ApplicationVersion>(new WhatIsTheAvailableVersion(tasks)))
-                );
+            display_version_command.run(view);
             view.display();
         }
 
@@ -71,59 +65,4 @@ namespace MoMoney.Presentation.Presenters.updates
             else view.downloaded(completed);
         }
     }
-
-    public interface IProcessQueryCommand<T> : IParameterizedCommand<ICallback<T>>
-    {
-    }
-
-    public class ProcessQueryCommand<T> : IProcessQueryCommand<T>
-    {
-        readonly IQuery<T> query;
-
-        public ProcessQueryCommand(IQuery<T> query)
-        {
-            this.query = query;
-        }
-
-        public void run(ICallback<T> item)
-        {
-            item.run(query.fetch());
-        }
-    }
-
-    public interface IRunQueryCommand<T> : ICommand
-    {
-    }
-
-    public class RunQueryCommand<T> : IRunQueryCommand<T>
-    {
-        readonly ICallback<T> callback;
-        readonly IProcessQueryCommand<T> command;
-
-        public RunQueryCommand(ICallback<T> callback, IProcessQueryCommand<T> command)
-        {
-            this.callback = callback;
-            this.command = command;
-        }
-
-        public void run()
-        {
-            command.run(callback);
-        }
-    }
-
-    public class WhatIsTheAvailableVersion : IQuery<ApplicationVersion>
-    {
-        readonly IUpdateTasks tasks;
-
-        public WhatIsTheAvailableVersion(IUpdateTasks tasks)
-        {
-            this.tasks = tasks;
-        }
-
-        public ApplicationVersion fetch()
-        {
-            return tasks.current_application_version();
-        }
-    }
 }
\ No newline at end of file
trunk/product/MyMoney/Presentation/Presenters/updates/CheckForUpdatesPresenterSpecs.cs
@@ -2,7 +2,7 @@ using developwithpassion.bdd.contexts;
 using MoMoney.Presentation.Model.updates;
 using MoMoney.Presentation.Presenters.Commands;
 using MoMoney.Presentation.Views.updates;
-using MoMoney.Tasks.infrastructure;
+using MoMoney.Tasks.infrastructure.updating;
 using MoMoney.Testing.MetaData;
 using MoMoney.Testing.spechelpers.contexts;
 using MoMoney.Testing.spechelpers.core;
trunk/product/MyMoney/Presentation/Views/core/ApplicationWindow.cs
@@ -1,7 +1,4 @@
 using System;
-using System.ComponentModel;
-using System.Diagnostics;
-using System.Threading;
 using System.Windows.Forms;
 using MoMoney.Infrastructure.Extensions;
 using MoMoney.Presentation.Resources;
@@ -18,17 +15,11 @@ namespace MoMoney.Presentation.Views.core
 
     public partial class ApplicationWindow : Form, IApplicationWindow
     {
-        protected readonly SynchronizationContext context;
-        AsyncOperation operation;
-
         public ApplicationWindow()
         {
             InitializeComponent();
             Icon = ApplicationIcons.Application;
             this.log().debug("created {0}", GetType());
-            context = SynchronizationContext.Current;
-            operation = AsyncOperationManager.CreateOperation(null);
-            Debug.Assert(null != context, "The Synchronization Context is not there");
         }
 
         public IApplicationWindow create_tool_tip_for(string title, string caption, Control control)
@@ -60,17 +51,8 @@ namespace MoMoney.Presentation.Views.core
 
         protected void on_ui_thread(Action action)
         {
-            //context.Post(x => action(), new object());
-            //context.Send(x => action(), new object());
-            //operation.Post(x => action(), new object());
-            if (InvokeRequired)
-            {
-                BeginInvoke(action);
-            }
-            else
-            {
-                action();
-            }
+            if (InvokeRequired) BeginInvoke(action);
+            else action();
         }
     }
 }
\ No newline at end of file
trunk/product/MyMoney/Tasks/infrastructure/updating/DisplayNextAvailableVersion.cs
@@ -0,0 +1,27 @@
+using MoMoney.Infrastructure.Threading;
+using MoMoney.Presentation.Model.updates;
+using MoMoney.Utility.Core;
+
+namespace MoMoney.Tasks.infrastructure.updating
+{
+    public interface IDisplayNextAvailableVersion : IParameterizedCommand<ICallback<ApplicationVersion>>
+    {
+    }
+
+    public class DisplayNextAvailableVersion : IDisplayNextAvailableVersion
+    {
+        readonly IWhatIsTheAvailableVersion query;
+        readonly ICommandProcessor processor;
+
+        public DisplayNextAvailableVersion(IWhatIsTheAvailableVersion query, ICommandProcessor processor)
+        {
+            this.query = query;
+            this.processor = processor;
+        }
+
+        public void run(ICallback<ApplicationVersion> item)
+        {
+            processor.add(new RunQueryCommand<ApplicationVersion>(item, new ProcessQueryCommand<ApplicationVersion>(query)));
+        }
+    }
+}
\ No newline at end of file
trunk/product/MyMoney/Tasks/infrastructure/updating/ProcessQueryCommand.cs
@@ -0,0 +1,32 @@
+using System.Threading;
+using MoMoney.Infrastructure.Container;
+using MoMoney.Utility.Core;
+
+namespace MoMoney.Tasks.infrastructure.updating
+{
+    public interface IProcessQueryCommand<T> : IParameterizedCommand<ICallback<T>>
+    {
+    }
+
+    public class ProcessQueryCommand<T> : IProcessQueryCommand<T>
+    {
+        readonly IQuery<T> query;
+        readonly SynchronizationContext context;
+
+        public ProcessQueryCommand(IQuery<T> query) : this(query, resolve.dependency_for<SynchronizationContext>())
+        {
+        }
+
+        public ProcessQueryCommand(IQuery<T> query, SynchronizationContext context)
+        {
+            this.query = query;
+            this.context = context;
+        }
+
+        public void run(ICallback<T> item)
+        {
+            var result = query.fetch();
+            context.Post(x => item.run(result), null);
+        }
+    }
+}
\ No newline at end of file
trunk/product/MyMoney/Tasks/infrastructure/updating/RunQueryCommand.cs
@@ -0,0 +1,25 @@
+using MoMoney.Utility.Core;
+
+namespace MoMoney.Tasks.infrastructure.updating
+{
+    public interface IRunQueryCommand<T> : ICommand
+    {
+    }
+
+    public class RunQueryCommand<T> : IRunQueryCommand<T>
+    {
+        readonly ICallback<T> callback;
+        readonly IProcessQueryCommand<T> command;
+
+        public RunQueryCommand(ICallback<T> callback, IProcessQueryCommand<T> command)
+        {
+            this.callback = callback;
+            this.command = command;
+        }
+
+        public void run()
+        {
+            command.run(callback);
+        }
+    }
+}
\ No newline at end of file
trunk/product/MyMoney/Tasks/infrastructure/UpdateTasks.cs → trunk/product/MyMoney/Tasks/infrastructure/updating/UpdateTasks.cs
@@ -4,7 +4,7 @@ using MoMoney.Domain.Core;
 using MoMoney.Presentation.Model.updates;
 using MoMoney.Utility.Core;
 
-namespace MoMoney.Tasks.infrastructure
+namespace MoMoney.Tasks.infrastructure.updating
 {
     public interface IUpdateTasks
     {
trunk/product/MyMoney/Tasks/infrastructure/updating/WhatIsTheAvailableVersion.cs
@@ -0,0 +1,24 @@
+using MoMoney.Presentation.Model.updates;
+using MoMoney.Utility.Core;
+
+namespace MoMoney.Tasks.infrastructure.updating
+{
+    public interface IWhatIsTheAvailableVersion : IQuery<ApplicationVersion>
+    {
+    }
+
+    public class WhatIsTheAvailableVersion : IWhatIsTheAvailableVersion
+    {
+        readonly IUpdateTasks tasks;
+
+        public WhatIsTheAvailableVersion(IUpdateTasks tasks)
+        {
+            this.tasks = tasks;
+        }
+
+        public ApplicationVersion fetch()
+        {
+            return tasks.current_application_version();
+        }
+    }
+}
\ No newline at end of file
trunk/product/MyMoney/MyMoney.csproj
@@ -560,7 +560,11 @@
     <Compile Include="Tasks\application\CustomerTasks.cs" />
     <Compile Include="Tasks\application\IncomeTasks.cs" />
     <Compile Include="Tasks\infrastructure\LogFileTasks.cs" />
-    <Compile Include="Tasks\infrastructure\UpdateTasks.cs" />
+    <Compile Include="Tasks\infrastructure\updating\DisplayNextAvailableVersion.cs" />
+    <Compile Include="Tasks\infrastructure\updating\ProcessQueryCommand.cs" />
+    <Compile Include="Tasks\infrastructure\updating\RunQueryCommand.cs" />
+    <Compile Include="Tasks\infrastructure\updating\UpdateTasks.cs" />
+    <Compile Include="Tasks\infrastructure\updating\WhatIsTheAvailableVersion.cs" />
     <Compile Include="Testing\MetaData\IntegrationAttribute.cs" />
     <Compile Include="Testing\spechelpers\contexts\concerns.cs" />
     <Compile Include="Testing\spechelpers\contexts\behaves_like_a_repository.cs" />