Commits

Christian Specht  committed 81596df Draft

factory to load launcher model by string

  • Participants
  • Parent commits 5f6480f

Comments (0)

Files changed (8)

File src/MissileSharp.Demo/Program.cs

             string choice = Console.ReadLine();
             Console.WriteLine();
 
-            using (var launcher = new CommandCenter(new ThunderMissileLauncher()))
+            // instead of hardcoding the name, you could load it from a config file here
+            var launcherModel = LauncherModelFactory.GetLauncher("MissileSharp.ThunderMissileLauncher");
+            using (var launcher = new CommandCenter(launcherModel))
             {
                 if (launcher.IsReady)
                 {

File src/MissileSharp.Launcher/ViewModels/MainWindowViewModel.cs

 
         public MainWindowViewModel(IConfigService configService, IMessageService messageService)
         {
-            this.model = new CommandCenter(new ThunderMissileLauncher());
-
             this.configService = configService;
             this.messageService = messageService;
 
+            var launcher = LauncherModelFactory.GetLauncher("MissileSharp.ThunderMissileLauncher");
+
+            this.model = new CommandCenter(launcher);
+
             this.FireCommand = new RelayCommand(new Action<object>(this.FireMissile));
 
             try

File src/MissileSharp.Tests/Library/LauncherModelFactoryTests.cs

+using System;
+using System.IO;
+using NUnit.Framework;
+
+namespace MissileSharp.Tests.Library
+{
+    [TestFixture]
+    public class LauncherModelFactoryTests
+    {
+        [Test]
+        public void GetLauncher_ValidLauncherName_ReturnsLauncherInstance()
+        {
+            var launcher = LauncherModelFactory.GetLauncher("MissileSharp.ThunderMissileLauncher");
+            Assert.That(launcher is ThunderMissileLauncher);
+        }
+
+        [Test]
+        public void GetLauncher_ValidLauncherNameInDifferentAssembly_ReturnsLauncherInstance()
+        {
+            var launcher = LauncherModelFactory.GetLauncher("MissileSharp.Tests.StubMissileLauncher", "MissileSharp.Tests.dll");
+            Assert.That(launcher is StubMissileLauncher);
+        }
+
+        [Test]
+        public void GetLauncher_InvalidLauncherName_ThrowsException()
+        {
+            Assert.Throws<TypeLoadException>(() => LauncherModelFactory.GetLauncher("MissileSharp.InvalidLauncher"));
+        }
+
+        [Test]
+        public void GetLauncher_InvalidAssemblyName_Throws()
+        {
+            Assert.Throws<FileNotFoundException>(() => LauncherModelFactory.GetLauncher("MissileSharp.InvalidLauncher", "Invalid.dll"));
+        }
+    }
+}

File src/MissileSharp.Tests/MissileSharp.Tests.csproj

     </Compile>
     <Compile Include="Library\LauncherCommandTests.cs" />
     <Compile Include="Launcher\MainWindowViewModelTests.cs" />
+    <Compile Include="Library\LauncherModelFactoryTests.cs" />
     <Compile Include="MockHidDevice.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="StubMissileLauncher.cs" />

File src/MissileSharp/LauncherModelFactory.cs

+using System;
+using System.Reflection;
+using MissileSharp.Properties;
+
+namespace MissileSharp
+{
+    /// <summary>
+    /// Factory for loading an ILauncherModel by its name
+    /// </summary>
+    public class LauncherModelFactory
+    {
+        /// <summary>
+        /// Loads the ILauncherModel with the given name
+        /// </summary>
+        /// <param name="className">The ILauncherModel class to load (syntax: Namespace.Class)</param>
+        /// <param name="assemblyName">The name of the assembly (if the class is not in the MissileSharp assembly)</param>
+        /// <returns></returns>
+        public static ILauncherModel GetLauncher(string className, string assemblyName)
+        {
+            Type type;
+
+            if (string.IsNullOrWhiteSpace(assemblyName))
+            {
+                type = Type.GetType(className);
+            }
+            else
+            {
+                Assembly assembly = Assembly.LoadFrom(assemblyName);
+                type = assembly.GetType(className);
+            }
+
+            if (type == null)
+            {
+                throw new TypeLoadException(Resources.LauncherModelNotFound + className);
+            }
+
+            return (ILauncherModel)Activator.CreateInstance(type);
+        }
+
+        /// <summary>
+        /// Loads the ILauncherModel with the given name
+        /// </summary>
+        /// <param name="className">The ILauncherModel class to load (syntax: Namespace.Class)</param>
+        /// <returns></returns>
+        public static ILauncherModel GetLauncher(string className)
+        {
+            return GetLauncher(className, null);
+        }
+    }
+}

File src/MissileSharp/MissileSharp.csproj

     <Compile Include="CommandCenter.cs" />
     <Compile Include="LauncherCommand.cs" />
     <Compile Include="CommandSetList.cs" />
+    <Compile Include="LauncherModelFactory.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Properties\Resources.Designer.cs">
       <AutoGen>True</AutoGen>

File src/MissileSharp/Properties/Resources.Designer.cs

         }
         
         /// <summary>
+        ///   Sucht eine lokalisierte Zeichenfolge, die No launcher model with this name available:  ähnelt.
+        /// </summary>
+        internal static string LauncherModelNotFound {
+            get {
+                return ResourceManager.GetString("LauncherModelNotFound", resourceCulture);
+            }
+        }
+        
+        /// <summary>
         ///   Sucht eine lokalisierte Zeichenfolge, die This line in the config file does not contain exactly two items:  ähnelt.
         /// </summary>
         internal static string LineDoesNotContainTwoItems {

File src/MissileSharp/Properties/Resources.resx

   <data name="FirstLineMustBeCommandSetName" xml:space="preserve">
     <value>The first line in the config must be a command set name. There can be no commands before the first command set name!</value>
   </data>
+  <data name="LauncherModelNotFound" xml:space="preserve">
+    <value>No launcher model with this name available: </value>
+  </data>
   <data name="LineDoesNotContainTwoItems" xml:space="preserve">
     <value>This line in the config file does not contain exactly two items: </value>
   </data>