Example #1
0
        /// <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);
        }
Example #2
0
        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);
        }
Example #3
0
        public void EmptyContainerCompilationTest()
        {
            var compiler = new ContainerCompiler(new Dictionary <Type, IDependencyConstructor>());

            var container = compiler.CompileDependencies(new Dictionary <Type, IDependency>());

            Assert.IsNotNull(container);
            Assert.Throws <NotImplementedException>(() => container.Resolve <object>());
        }
Example #4
0
        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);
        }
Example #6
0
        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);
        }
Example #7
0
        public void ContainerCompilationTest()
        {
            var compiler = new ContainerCompiler(new Dictionary <Type, IDependencyConstructor>
            {
                [typeof(TypeDependency)] = new TypeDependencyConstructor()
            });

            var dependencies = new Dictionary <Type, IDependency>();

            dependencies.Add(typeof(IDependentService), new TypeDependency(typeof(DependentService), dependencies));
            dependencies.Add(typeof(IDependencyService), new TypeDependency(typeof(DependencyService), dependencies));
            dependencies.Add(typeof(IPlainService), new TypeDependency(typeof(PlainService), dependencies));

            var container = compiler.CompileDependencies(dependencies);

            Assert.IsNotNull(container);
            Assert.Throws <NotImplementedException>(() => container.Resolve <object>());

            Assert.DoesNotThrow(() => container.Resolve <IDependentService>());
            Assert.DoesNotThrow(() => container.Resolve <IDependencyService>());
            Assert.DoesNotThrow(() => container.Resolve <IPlainService>());
        }
Example #8
0
        /// <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();
 }