Commits

Christian Specht committed 3c498cb Draft

extract MessageBox.ShowMessage() to MessageService

Comments (0)

Files changed (8)

src/MissileSharp.Launcher/App.xaml.cs

             var builder = new ContainerBuilder();
             builder.RegisterType<MainWindowViewModel>();
             builder.RegisterType<ConfigService>().As<IConfigService>();
+            builder.RegisterType<MessageService>().As<IMessageService>();
             var container = builder.Build();
 
             var window = new MainWindow();

src/MissileSharp.Launcher/MissileSharp.Launcher.csproj

     </Compile>
     <Compile Include="Services\ConfigService.cs" />
     <Compile Include="Services\IConfigService.cs" />
+    <Compile Include="Services\IMessageService.cs" />
+    <Compile Include="Services\MessageService.cs" />
     <Compile Include="ViewModels\MainWindowViewModel.cs" />
     <Compile Include="Views\MainWindow.xaml.cs">
       <DependentUpon>MainWindow.xaml</DependentUpon>

src/MissileSharp.Launcher/Services/IMessageService.cs

+
+namespace MissileSharp.Launcher.Services
+{
+    /// <summary>
+    /// helper service to abstract MessageBox.Show() away
+    /// </summary>
+    public interface IMessageService
+    {
+        void ShowMessage(string messageText);
+    }
+}

src/MissileSharp.Launcher/Services/MessageService.cs

+using System.Windows;
+
+namespace MissileSharp.Launcher.Services
+{
+    /// <summary>
+    /// helper service to abstract MessageBox.Show() away
+    /// </summary>
+    public class MessageService : IMessageService
+    {
+        public void ShowMessage(string messageText)
+        {
+            MessageBox.Show(messageText);
+        }
+    }
+}

src/MissileSharp.Launcher/ViewModels/MainWindowViewModel.cs

     {
         private CommandCenter model;
         private IConfigService configService;
+        private IMessageService messageService;
 
         public ObservableCollection<string> CommandSets { get; set; }
 
         public event PropertyChangedEventHandler PropertyChanged;
 
-        public MainWindowViewModel(IConfigService configService)
+        public MainWindowViewModel(IConfigService configService, IMessageService messageService)
         {
             this.model = new CommandCenter(new ThunderMissileLauncher());
 
             this.configService = configService;
+            this.messageService = messageService;
 
             try
             {
             }
             catch (Exception ex)
             {
-                MessageBox.Show(string.Format("Couldn't load config file. Error:{0}{1}", Environment.NewLine, ex.Message));
+                messageService.ShowMessage(string.Format("Couldn't load config file. Error:{0}{1}", Environment.NewLine, ex.Message));
                 Application.Current.Shutdown();
                 return;
             }

src/MissileSharp.Tests/Launcher/MainWindowViewModelTests.cs

             var config = new StubConfigService();
             config.SetConfig(new string[] { "[name2]", "up,5", "[name1]", "up,5" });
 
-            viewmodel = new MainWindowViewModel(config);
+            var messageService = new StubMessageService();
+
+            viewmodel = new MainWindowViewModel(config, messageService);
         }
 
         [Test]
         public void Constructor_ConfigIsEmpty_ThrowsException()
         {
             var config = new StubConfigService();
-            Assert.Throws<NullReferenceException>(() => new MainWindowViewModel(config));
+            Assert.Throws<NullReferenceException>(() => new MainWindowViewModel(config, new StubMessageService()));
         }
     }
 }

src/MissileSharp.Tests/Launcher/StubMessageService.cs

+using MissileSharp.Launcher.Services;
+
+namespace MissileSharp.Tests.Launcher
+{
+    public class StubMessageService : IMessageService
+    {
+        public void ShowMessage(string messageText)
+        {
+        }
+    }
+}

src/MissileSharp.Tests/MissileSharp.Tests.csproj

       <Link>AssemblyVersion.cs</Link>
     </Compile>
     <Compile Include="Launcher\StubConfigService.cs" />
+    <Compile Include="Launcher\StubMessageService.cs" />
     <Compile Include="Library\CommandCenterTests.cs" />
     <Compile Include="Library\ConfigReaderTests.cs" />
     <Compile Include="Library\CommandSetListTests.cs">