/// <summary> /// Compiles the target IOC container. /// </summary> /// <returns>Returns <c>true</c> if the operation succeeded. Otherwise, it will return <c>false</c>.</returns> public override bool Execute() { bool result = true; try { string targetPath = GetTargetPath(TargetAssemblies); targetPath = string.IsNullOrEmpty(targetPath) ? Environment.CurrentDirectory : targetPath; targetPath = Path.GetDirectoryName(targetPath); var targetFiles = Path.GetFileName(TargetAssemblies); var loader = new DependencyMapLoader(); var dependencyMap = loader.LoadFrom(targetPath, targetFiles); var typeName = "MicroContainer" ?? TypeName; var namespaceName = "Hiro.Containers" ?? NamespaceName; var assemblyName = "Hiro.CompiledContainers" ?? AssemblyName; var compiler = new ContainerCompiler(); var compiledAssembly = compiler.Compile(typeName, namespaceName, assemblyName, dependencyMap); Console.WriteLine("Compiling {0}", OutputAssemblyFileName); compiledAssembly.Write(OutputAssemblyFileName); } catch (Exception ex) { result = false; Log.LogError(string.Format("Exception thrown: {0}", ex)); } return(result); }
private static IMicroContainer Compile(DependencyMap map) { var compiler = new ContainerCompiler(); var assembly = compiler.Compile("MicroContainer", "Hiro.Containers", "Hiro.CompiledContainers", map); try { if (File.Exists("output.dll")) { File.Delete("output.dll"); } assembly.Write("output.dll"); } catch { // Do nothing } var loadedAssembly = assembly.ToAssembly(); Assert.IsNotNull(loadedAssembly); var targetType = (from t in loadedAssembly.GetTypes() where typeof(IMicroContainer).IsAssignableFrom(t) select t).First(); var container = Activator.CreateInstance(targetType) as IMicroContainer; Assert.IsNotNull(container); return(container); }
public void ShouldPullAvailableDependenciesFromDependencyContainer() { var dependency = new Dependency(typeof(IVehicle), string.Empty); var dependencyList = new IDependency[] { dependency }; var implementation = new Mock <IImplementation>(); implementation.Expect(i => i.Emit(It.IsAny <IDependency>(), It.IsAny <IDictionary <IDependency, IImplementation> >(), It.IsAny <MethodDefinition>())); var map = new Mock <IDependencyContainer>(); map.Expect(m => m.Dependencies).Returns(dependencyList); map.Expect(m => m.GetImplementations(It.IsAny <IDependency>(), It.IsAny <bool>())).Returns(new IImplementation[] { implementation.Object }); var compiler = new ContainerCompiler(); compiler.Compile("MicroContainer", "Hiro.Containers", "Hiro.CompiledContainers", map.Object); map.VerifyAll(); }
public HiroUseCase() { var map = new DependencyMap(); map.AddService(typeof(IWebService), typeof(WebService)); map.AddService(typeof(IAuthenticator), typeof(Authenticator)); map.AddService(typeof(IStockQuote), typeof(StockQuote)); map.AddService(typeof(IDatabase), typeof(Database)); map.AddService(typeof(IErrorHandler), typeof(ErrorHandler)); map.AddService(typeof(ILogger), typeof(Logger)); IContainerCompiler compiler = new ContainerCompiler(); var assembly = compiler.Compile(map); var loadedAssembly = assembly.ToAssembly(); var containerType = loadedAssembly.GetTypes()[0]; container = (IMicroContainer)Activator.CreateInstance(containerType); }
public void ShouldBeAbleToGetAllInstancesOfATypeFromACompiledContainer() { var map = new DependencyMap(); map.AddService(typeof(IVehicle), typeof(Vehicle)); map.AddService("Truck", typeof(IVehicle), typeof(Truck)); var compiler = new ContainerCompiler(); var assembly = compiler.Compile("MicroContainer", "Hiro.Containers", "Hiro.CompiledContainers", map); var container = Compile(map); var instances = container.GetAllInstances(typeof(IVehicle)); Assert.IsNotNull(instances); Assert.IsTrue(instances.Count() == 2); var items = instances.ToArray(); Assert.IsTrue(items[0] is Vehicle); Assert.IsTrue(items[1] is Truck); }
/// <summary> /// Compiles the target IOC container. /// </summary> /// <returns>Returns <c>true</c> if the operation succeeded. Otherwise, it will return <c>false</c>.</returns> public override bool Execute() { bool result = true; try { string targetPath = GetTargetPath(TargetAssemblies); targetPath = string.IsNullOrEmpty(targetPath) ? Environment.CurrentDirectory : targetPath; targetPath = Path.GetDirectoryName(targetPath); var targetFiles = Path.GetFileName(TargetAssemblies); // Use the loaded modules from the target assemblies // to determine which services will be compiled var dependencyMap = new DependencyMap(); var loader = new ModuleLoader(dependencyMap); loader.LoadModulesFrom(targetPath, targetFiles); var typeName = TypeName ?? "MicroContainer"; var namespaceName = NamespaceName ?? "Hiro.Containers"; var assemblyName = AssemblyName ?? "Hiro.CompiledContainers"; var compiler = new ContainerCompiler(); var compiledAssembly = compiler.Compile(typeName, namespaceName, assemblyName, dependencyMap); Console.WriteLine("Compiling {0}", OutputAssemblyFileName); compiledAssembly.Write(OutputAssemblyFileName); } catch (Exception ex) { result = false; Log.LogError(string.Format("Exception thrown: {0}", ex)); } return(result); }
/// <summary> /// Compiles the target IOC container. /// </summary> /// <returns>Returns <c>true</c> if the operation succeeded. Otherwise, it will return <c>false</c>.</returns> public override bool Execute() { bool result = true; try { string targetPath = GetTargetPath(TargetAssemblies); targetPath = string.IsNullOrEmpty(targetPath) ? Environment.CurrentDirectory : targetPath; targetPath = Path.GetDirectoryName(targetPath); var targetFiles = Path.GetFileName(TargetAssemblies); var loader = new DependencyMapLoader(); var dependencyMap = loader.LoadFrom(targetPath, targetFiles); var typeName = "MicroContainer" ?? TypeName; var namespaceName = "Hiro.Containers" ?? NamespaceName; var assemblyName = "Hiro.CompiledContainers" ?? AssemblyName; var compiler = new ContainerCompiler(); var compiledAssembly = compiler.Compile(typeName, namespaceName, assemblyName, dependencyMap); Console.WriteLine("Compiling {0}", OutputAssemblyFileName); compiledAssembly.Write(OutputAssemblyFileName); } catch (Exception ex) { result = false; Log.LogError(string.Format("Exception thrown: {0}", ex)); } return result; }
/// <summary> /// Compiles the target IOC container. /// </summary> /// <returns>Returns <c>true</c> if the operation succeeded. Otherwise, it will return <c>false</c>.</returns> public override bool Execute() { bool result = true; try { string targetPath = GetTargetPath(TargetAssemblies); targetPath = string.IsNullOrEmpty(targetPath) ? Environment.CurrentDirectory : targetPath; targetPath = Path.GetDirectoryName(targetPath); var targetFiles = Path.GetFileName(TargetAssemblies); // Use the loaded modules from the target assemblies // to determine which services will be compiled var dependencyMap = new DependencyMap(); var loader = new ModuleLoader(dependencyMap); loader.LoadModulesFrom(targetPath, targetFiles); var typeName = TypeName ?? "MicroContainer"; var namespaceName = NamespaceName ?? "Hiro.Containers"; var assemblyName = AssemblyName ?? "Hiro.CompiledContainers"; var compiler = new ContainerCompiler(); var compiledAssembly = compiler.Compile(typeName, namespaceName, assemblyName, dependencyMap); Console.WriteLine("Compiling {0}", OutputAssemblyFileName); compiledAssembly.Write(OutputAssemblyFileName); } catch (Exception ex) { result = false; Log.LogError(string.Format("Exception thrown: {0}", ex)); } return result; }
private void CompileSourceCode() { var compiler = new ContainerCompiler(Settings, ReferencedAssemblies, _template); compiler.Compile(); }