Example #1
0
        public static void LoadContainer(StructureMap.IContainer container, string path, string pattern)
        {
            var dirCat    = new DirectoryCatalog(path, pattern);
            var importDef = BuildImportDefinition();

            try
            {
                var assemblies = dirCat.LoadedFiles;
                container.Configure(ct => ct.Scan(scan =>
                {
                    //scan.TheCallingAssembly();
                    //scan.(assembly.FullName);
                    scan.WithDefaultConventions();
                    scan.AssembliesFromApplicationBaseDirectory(assembly => assembly.FullName.Contains("CodingTest."));
                }));

                using (var aggregateCatalog = new AggregateCatalog())
                {
                    aggregateCatalog.Catalogs.Add(dirCat);

                    using (var componsitionContainer = new CompositionContainer(aggregateCatalog))
                    {
                        IEnumerable <Export> exports = componsitionContainer.GetExports(importDef);

                        IEnumerable <IModule> modules =
                            exports.Select(export => export.Value as IModule).Where(m => m != null);

                        var registrar = new StructureMapModuleRegistrar(container);
                        foreach (IModule module in modules)
                        {
                            module.Initialize(registrar);
                        }
                    }
                }
            }
            catch (ReflectionTypeLoadException typeLoadException)
            {
                var builder = new StringBuilder();
                foreach (Exception loaderException in typeLoadException.LoaderExceptions)
                {
                    builder.AppendFormat("{0}\n", loaderException.Message);
                }

                throw new TypeLoadException(builder.ToString(), typeLoadException);
            }
        }
Example #2
0
        public static void LoadContainer(StructureMap.IContainer container, string path, string pattern)
        {
            var dirCat    = new DirectoryCatalog(path, pattern);
            var importDef = BuildImportDefinition();

            try
            {
                using (var aggregateCatalog = new AggregateCatalog())
                {
                    aggregateCatalog.Catalogs.Add(dirCat);

                    using (var componsitionContainer = new CompositionContainer(aggregateCatalog))
                    {
                        IEnumerable <Export> exports = componsitionContainer.GetExports(importDef);

                        IEnumerable <IModule> modules =
                            exports.Select(export => export.Value as IModule).Where(m => m != null);

                        var registrar = new StructureMapModuleRegistrar(container);
                        foreach (IModule module in modules)
                        {
                            module.Initialize(registrar);
                        }
                    }
                }
            }
            catch (ReflectionTypeLoadException typeLoadException)
            {
                var builder = new StringBuilder();
                foreach (Exception loaderException in typeLoadException.LoaderExceptions)
                {
                    builder.AppendFormat("{0}\n", loaderException.Message);
                }

                throw new TypeLoadException(builder.ToString(), typeLoadException);
            }
        }