Ejemplo n.º 1
0
        /// <summary>
        /// Locates the controller info for performing the specified action with the specified type by looking at the base types of the provided type.
        /// </summary>
        /// <param name="action">The action that is to be performed by the controller.</param>
        /// <param name="type">The type that is involved in the action.</param>
        /// <returns>The controller info for the specified scenario.</returns>
        public ControllerInfo LocateFromBaseTypes(string action, Type type)
        {
            ControllerInfo controllerInfo = null;

            // Disabled logging to boost performance
            //using (LogGroup logGroup = LogGroup.Start("Locating via base types the controller for the action '" + action + "' and type '" + type.Name + "'.", NLog.LogLevel.Debug))
            //{
            TypeNavigator navigator = new TypeNavigator(type);

            while (navigator.HasNext && controllerInfo == null)
            {
                Type nextType = navigator.Next();

                string key = Controllers.GetControllerKey(action, nextType.Name);

                // If a controller exists for the base type then use it
                if (Controllers.ControllerExists(key))
                {
                    //			LogWriter.Debug("Found match with key: " + key);

                    controllerInfo = Controllers[key];

                    break;
                }
                // TODO: Check if needed. It shouldn't be. The other call to LocateFromInterfaces in LocateFromHeirarchy should be sufficient
                // Otherwise check the interfaces of that base type
                //else
                //{
                //	controllerInfo = LocateFromInterfaces(action, nextType);
                //}
            }
            //}

            return(controllerInfo);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Locates the controller info for performing the specified action with the specified type by looking at the interfaces of the provided type.
        /// </summary>
        /// <param name="action">The action that is to be performed by the controller.</param>
        /// <param name="type">The type that is involved in the action.</param>
        /// <returns>The controller info for the specified scenario.</returns>
        public ControllerInfo LocateFromInterfaces(string action, Type type)
        {
            ControllerInfo controllerInfo = null;

            // Disabled logging to boost performance
            //using (LogGroup logGroup = LogGroup.Start("Locating via interfaces the controller for the action '" + action + "' and type '" + type.Name + "'.", NLog.LogLevel.Debug))
            //{
            Type[] interfaceTypes = type.GetInterfaces();

            // Loop backwards through the interface types
            for (int i = interfaceTypes.Length - 1; i >= 0; i--)
            {
                Type interfaceType = interfaceTypes[i];

                string key = Controllers.GetControllerKey(action, interfaceType.Name);

                if (Controllers.ControllerExists(key))
                {
                    //			LogWriter.Debug("Found match with key: " + key);

                    controllerInfo = Controllers[key];

                    break;
                }
            }
            //}

            return(controllerInfo);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Loads the controller from the specified path.
        /// </summary>
        /// <param name="controllerPath">The full path to the controller to load.</param>
        /// <returns>The controller deserialized from the specified file path.</returns>
        public ControllerInfo LoadFromFile(string controllerPath)
        {
            ControllerInfo info = null;

            // Disabled logging to boost performance
            //using (LogGroup logGroup = LogGroup.Start("Loading the controller from the specified path.", NLog.LogLevel.Debug))
            //{
            if (!File.Exists(controllerPath))
            {
                throw new ArgumentException("The specified file does not exist.");
            }

            //	LogWriter.Debug("Path: " + controllerPath);


            using (StreamReader reader = new StreamReader(File.OpenRead(controllerPath)))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(ControllerInfo));

                info = (ControllerInfo)serializer.Deserialize(reader);

                reader.Close();
            }
            //}

            return(info);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Loads all the controllers found in the controllers directory.
        /// </summary>
        /// <param name="includeDisabled"></param>
        /// <returns>An array of the the controllers found in the directory.</returns>
        public ControllerInfo[] LoadInfoFromDirectory(bool includeDisabled)
        {
            // Logging disabled to boost performance
            //using (LogGroup logGroup = LogGroup.StartDebug("Loading the controllers from the XML file."))
            //{
            if (Controllers == null)
            {
                List <ControllerInfo> validControllers = new List <ControllerInfo>();

                ControllerInfo[] controllers = new ControllerInfo[] {};

                using (StreamReader reader = new StreamReader(File.OpenRead(FileNamer.ControllersInfoFilePath)))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(ControllerInfo[]));
                    controllers = (ControllerInfo[])serializer.Deserialize(reader);
                }

                foreach (ControllerInfo controller in controllers)
                {
                    if (controller.Enabled || includeDisabled)
                    {
                        validControllers.Add(controller);
                    }
                }

                Controllers = validControllers.ToArray();
            }
            //}
            return(Controllers);
        }
        /// <summary>
        /// Disposes the controllers found by the scanner.
        /// </summary>
        public void Dispose()
        {
            using (LogGroup logGroup = LogGroup.Start("Disposing the controllers.", NLog.LogLevel.Debug))
            {
                ControllerInfo[] controllers = new ControllerInfo[] {};

                Dispose(ControllerState.Controllers.ToArray());
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Creates a new instance of the controller with a Controller attribute matching the specified type name and action.
        /// </summary>
        /// <param name="action">The action that the new controller will be performing.</param>
        /// <param name="typeName">The name of the type involved in the action.</param>
        /// <returns>A controller that is suitable to perform the specified action with the specified type.</returns>
        public IController NewController(string action, string typeName)
        {
            IController controller = null;

            ControllerInfo info = ControllerState.Controllers[action, typeName];


            return(controller);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Creates a new instance of the controller with a Controller attribute matching the specified type name and action.
        /// </summary>
        /// <param name="controllerInfo">The controller info object that specified the controller to create.</param>
        /// <returns>A controller that is suitable to perform the specified action with the specified type.</returns>
        public IController CreateController(ControllerInfo controllerInfo)
        {
            IController controller = null;

            using (LogGroup logGroup = LogGroup.StartDebug("Creating a new controller based on the provided info."))
            {
                if (controllerInfo == null)
                {
                    throw new ArgumentNullException("controllerInfo");
                }

                Type controllerType = Type.GetType(controllerInfo.ControllerType);

                if (controllerType == null)
                {
                    throw new Exception("Can't get type '" + controllerInfo.ControllerType);
                }

                Type entityType = null;
                if (EntityState.IsType(controllerInfo.TypeName))
                {
                    entityType = EntityState.GetType(controllerInfo.TypeName);
                }

                LogWriter.Debug("Controller type: " + controllerType.FullName);
                LogWriter.Debug("Entity type: " + (entityType != null ? entityType.FullName : String.Empty));

                if (entityType != null && controllerType.IsGenericTypeDefinition)
                {
                    LogWriter.Debug("Is generic type definition.");

                    Type gType = controllerType.MakeGenericType(new Type[] { entityType });
                    controller = (IController)Activator.CreateInstance(gType);
                }
                else
                {
                    LogWriter.Debug("Is not generic type definition.");

                    controller = (IController)Activator.CreateInstance(controllerType);
                }

                if (controller == null)
                {
                    throw new ArgumentException("Unable to create instance of controller: " + entityType.ToString(), "controllerInfo");
                }

                LogWriter.Debug("Controller created.");
            }

            return(controller);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Creates the file name for the serialized info for the provided controller.
        /// </summary>
        /// <param name="controller">The controller to create the file name for.</param>
        /// <returns>The full file name for the serialized info for the provided controller.</returns>
        public virtual string CreateInfoFileName(ControllerInfo controller)
        {
            if (controller == null)
            {
                throw new ArgumentNullException("controller");
            }

            if (controller.Action == null)
            {
                throw new ArgumentNullException("controller.Action", "No action has been set to the Action property.");
            }

            string name = controller.TypeName + "-" + controller.Action + ".controller";

            return(name);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Locates the controller info for performing the specified action with the specified type.
        /// </summary>
        /// <param name="action">The action that is to be performed by the controller.</param>
        /// <param name="typeName">The short type name of the entity that is involved in the action.</param>
        /// <returns>The controller info for the specified scenario.</returns>
        public ControllerInfo Locate(string action, string typeName)
        {
            // Create the controller info variable to hold the return value
            ControllerInfo controllerInfo = null;

            // Disabled logging to boost performance
            //using (LogGroup logGroup = LogGroup.Start("Locating a controller for the action '" + action + "' and the type '" + typeName + "'.", NLog.LogLevel.Debug))
            //{
            // Get the specified type
            Type type = null;

            type = Entities.EntityState.GetType(typeName);

            if (type == null)
            {
                throw new ArgumentException("No type found with the name '" + typeName + "'.");
            }

            // Create a direct controller key for the specified type
            string key = Controllers.GetControllerKey(action, typeName);

            //	LogWriter.Debug("Direct key: " + key);

            // Check the direct key to see if a controller exists
            if (Controllers.ControllerExists(key))
            {
                //		LogWriter.Debug("Direct key matches.");

                controllerInfo = Controllers[key];
            }
            // If not then navigate up the heirarchy looking for a matching controller
            else if (type != null)                     // Only use heirarchy if an actual type was provided.
            {
                //		LogWriter.Debug("Direct key doesn't match. Locating through heirarchy.");
                controllerInfo = LocateFromHeirarchy(action, type);
            }

            //	if (controllerInfo == null)
            //		LogWriter.Debug("No controller found.");
            //	else
            //	{
            //		LogWriter.Debug("Controller type found: " + controllerInfo.ControllerType);
            //		LogWriter.Debug("Controller key: " + controllerInfo.Key);
            //	}
            //}
            return(controllerInfo);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Finds all the controllers in the available assemblies.
        /// </summary>
        /// <param name="includeTestControllers"></param>
        /// <returns>An array of info about the controllers found.</returns>
        public override ControllerInfo[] FindControllers(bool includeTestControllers)
        {
            List <ControllerInfo> controllers = new List <ControllerInfo>();

            //using (LogGroup logGroup = LogGroup.Start("Finding controllers by scanning the attributes of the available type.", NLog.LogLevel.Debug))
            //{
            foreach (string assemblyPath in AssemblyPaths)
            {
                Assembly assembly = Assembly.LoadFrom(assemblyPath);

                if (ContainsControllers(assembly, includeTestControllers))
                {
                    try
                    {
                        foreach (Type type in assembly.GetTypes())
                        {
                            if (IsController(type))
                            {
                                //LogWriter.Debug("Found controller type: " + type.ToString());

                                ControllerInfo controllerInfo = new ControllerInfo(type);

                                if (controllerInfo.TypeName != null && controllerInfo.TypeName != String.Empty &&
                                    controllerInfo.Action != null && controllerInfo.Action != String.Empty)
                                {
                                    //LogWriter.Debug("Found match.");

                                    controllers.Add(controllerInfo);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        LogWriter.Error("Error occurred while trying to scan for controllers.");

                        LogWriter.Error(ex);
                    }
                }
            }
            //}

            return(controllers.ToArray());
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Locates the controller info for performing the specified action with the specified type by looking at the base types and interfaces of the provided type.
        /// </summary>
        /// <param name="action">The action that is to be performed by the controller.</param>
        /// <param name="type">The type that is involved in the action.</param>
        /// <returns>The controller info for the specified scenario.</returns>
        public ControllerInfo LocateFromHeirarchy(string action, Type type)
        {
            ControllerInfo controllerInfo = null;

            // Disabled logging to boost performance
            //using (LogGroup logGroup = LogGroup.Start("Locating via heirarchy the controller for the action '" + action + "' and type '" + type.Name + "'.", NLog.LogLevel.Debug))
            //{
            controllerInfo = LocateFromInterfaces(action, type);

            if (controllerInfo == null)
            {
                //		LogWriter.Debug("Can't locate through interfaces. Trying base types.");

                controllerInfo = LocateFromBaseTypes(action, type);
            }

            //}
            return(controllerInfo);
        }
        /// <summary>
        /// Initializes the controllers and loads all controllers to state.
        /// </summary>
        /// <param name="includeTestControllers"></param>
        public void Initialize(bool includeTestControllers)
        {
            using (LogGroup logGroup = LogGroup.StartDebug("Initializing the web controllers."))
            {
                if (StateAccess.IsInitialized)
                {
                    if (!ControllerState.IsInitialized)
                    {
                        ControllerInfo[] controllers = new ControllerInfo[] {};
                        if (IsCached)
                        {
                            LogWriter.Debug("Is cached. Loading from XML.");

                            controllers = LoadControllers();
                        }
                        else
                        {
                            LogWriter.Debug("Is not cached. Scanning from type attributes.");

                            controllers = FindControllers(includeTestControllers);
                            Saver.SaveInfoToFile(controllers);
                        }

                        Initialize(controllers);
                    }
                    else
                    {
                        LogWriter.Debug("Already initialized.");
                    }
                }
                else
                {
                    LogWriter.Debug("State is not initialized. Skipping.");
                }
            }
        }