Esempio n. 1
0
        public XypericoDependencyResolver(IObjectContainer container)
        {
            Container = container;

            //
            //  Register all controller types so the container will be able to resolve them
            //
            Logger.Debug("Dependency resolver scanning for controllers");
            //foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
            foreach (Assembly a in BuildManager.GetReferencedAssemblies())
            {
                try
                {
                    Logger.DebugFormat("Scanning assembly {0}", a.FullName);
                    foreach (Type controllerType in (from t in a.GetTypes() where typeof(IController).IsAssignableFrom(t) select t))
                    {
                        Logger.DebugFormat("Adding {0} as a controller", controllerType);
                        Container.AddTransientComponent(controllerType, controllerType);
                        // OLD: AddComponentLifeStyle(controllerType.ToString(), controllerType, Castle.Core.LifestyleType.Transient);
                    }
                }
                catch (ReflectionTypeLoadException ex)
                {
                    Logger.Warn(string.Format("Could not load assembly {0}.", a.FullName), ex);
                    foreach (Exception ex2 in ex.LoaderExceptions)
                    {
                        Logger.Warn("Loader exception", ex2);
                    }
                }
                catch (Exception ex)
                {
                    Logger.Warn(string.Format("Could not load assembly {0}.", a.FullName), ex);
                }
            }
        }
Esempio n. 2
0
        public static object Instantiate(Type t, object namedConstructorArgs = null, IObjectContainer container = null)
        {
            if (container == null)
            {
                container = Container;
            }

            object result = null;

            try
            {
                if (!container.HasComponent(t))
                {
                    container.AddTransientComponent(t, t);
                }

                result = container.Resolve(t, namedConstructorArgs ?? new { });
            }
            catch (Exception ex)
            {
                throw new ArgumentException(string.Format("Could not instantiate type '{0}'. Got error: {1}.", t, ex.Message), ex);
            }

            if (result == null)
            {
                throw new ArgumentException(string.Format("Could not instantiate type '{0}'. No internal error message available.", t));
            }

            if (!t.IsInstanceOfType(result))
            {
                throw new ArgumentException(string.Format("Could not use instantiated type '{0}' since it does not inherit from '{1}'.", result.GetType(), t));
            }

            return(result);
        }
        public XypericoDependencyResolver(IObjectContainer container)
        {
            Container = container;

              //
              //  Register all controller types so the container will be able to resolve them
              //
              Logger.Debug("Dependency resolver scanning for controllers");
              //foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
              foreach (Assembly a in BuildManager.GetReferencedAssemblies())
              {
            try
            {
              Logger.DebugFormat("Scanning assembly {0}", a.FullName);
              foreach (Type controllerType in (from t in a.GetTypes() where typeof(IController).IsAssignableFrom(t) select t))
              {
            Logger.DebugFormat("Adding {0} as a controller", controllerType);
            Container.AddTransientComponent(controllerType, controllerType);
            // OLD: AddComponentLifeStyle(controllerType.ToString(), controllerType, Castle.Core.LifestyleType.Transient);
              }
            }
            catch (ReflectionTypeLoadException ex)
            {
              Logger.Warn(string.Format("Could not load assembly {0}.", a.FullName), ex);
              foreach (Exception ex2 in ex.LoaderExceptions)
            Logger.Warn("Loader exception", ex2);

            }
            catch (Exception ex)
            {
              Logger.Warn(string.Format("Could not load assembly {0}.", a.FullName), ex);
            }
              }
        }
Esempio n. 4
0
        public static IEnumerable <T> ScanForAndInstantiateTypesOf <T>(IObjectContainer container,
                                                                       object namedConstructorArgs       = null,
                                                                       IEnumerable <Assembly> assemblies = null)
        {
            IList <Type> types = ScanForTypesOf(typeof(T), assemblies).ToList();

            // Register all types first
            foreach (Type t in types)
            {
                if (!container.HasComponent(t))
                {
                    container.AddTransientComponent(t, t);
                }
            }

            // Instantiate each type (now that it's potential dependencies has been registered)
            foreach (Type t in types)
            {
                yield return((T)ObjectContainer.Instantiate(t, namedConstructorArgs, container));
            }
        }