Provides a base class for all controllers within the application.
Inheritance: Controller, IModuleFactory
コード例 #1
0
ファイル: ViewInfo.cs プロジェクト: igorfrance/sage
        /// <summary>
        /// Initializes a new instance of the <see cref="ViewInfo" /> class, using the specified
        /// <paramref name="controller" /> and <paramref name="action" />.
        /// </summary>
        /// <param name="controller">The controller for which to create this instance.</param>
        /// <param name="action">The controller action for which to create this instance.</param>
        /// <param name="controllerName">Optional name of the controller to use for resolving configuration and template paths.</param>
        public ViewInfo(SageController controller, string action, string controllerName = null)
        {
            this.Context = controller.Context;
            this.ContentType = "text/html";
            this.Controller = controller;

            this.Action = action;
            this.ConfigPath = null;

            this.ViewSource = ViewSource.BuiltIn;
            this.ViewPath = DefaultBuiltInStylesheetPath;

            if (controllerName == null)
                controllerName = controller.Name;

            string pathTemplate = controller.ViewPathTemplate;
            string folderPath = this.Context.MapPath(string.Format("{0}/{1}", pathTemplate, controllerName));
            string basePath = this.Context.MapPath(string.Format("{0}/{1}", folderPath, action));

            foreach (string extension in ConfigExtensions)
            {
                var path = basePath + extension;
                if (!File.Exists(path))
                    continue;

                this.ConfigPath = path;
                this.ConfigName = string.Concat(controllerName, "/", Path.GetFileName(this.ConfigPath));
                this.ConfigExists = true;
                break;
            }

            foreach (string extension in ViewExtensions)
            {
                var path = basePath + extension;
                if (!File.Exists(path))
                    continue;

                this.ViewPath = path;
                this.ViewSource = ViewSource.Specific;
                break;
            }

            if (this.ViewSource == ViewSource.BuiltIn && File.Exists(this.Context.Path.DefaultStylesheetPath))
            {
                this.ViewPath = this.Context.Path.DefaultStylesheetPath;
                this.ViewSource = ViewSource.Project;
            }
        }
コード例 #2
0
ファイル: ViewConfiguration.cs プロジェクト: igorfrance/sage
        private ViewConfiguration(SageController controller, ViewInfo viewInfo)
        {
            Contract.Requires<ArgumentNullException>(controller != null);
            Contract.Requires<ArgumentNullException>(viewInfo != null);

            configElement = viewInfo.ConfigDocument.DocumentElement;

            this.Name = viewInfo.Action;
            this.Controller = controller;
            this.Info = viewInfo;
            this.Modules = new OrderedDictionary<string, IModule>();

            if (string.IsNullOrWhiteSpace(ViewConfiguration.ModuleSelectXPath))
                return;

            var moduleDependencies = new Dictionary<string, List<string>>();
            var moduleNodes = this.Info.ConfigDocument.SelectNodes(ViewConfiguration.ModuleSelectXPath, XmlNamespaces.Manager);
            var moduleList = moduleNodes.ToList();

            // tag all modules with unique ids
            for (int i = 0; i < moduleNodes.Count; i++)
            {
                XmlElement moduleElem = (XmlElement) moduleNodes[i];
                string moduleId = moduleElem.GetAttribute("id");
                if (string.IsNullOrEmpty(moduleId))
                    moduleId = string.Format(ModuleIdPattern, i);

                moduleElem.SetAttribute("id", moduleId);
                moduleDependencies.Add(moduleId, new List<string>());
            }

            // determine module dependencies
            foreach (XmlElement moduleElem in moduleNodes)
            {
                string moduleId = moduleElem.GetAttribute("id");
                var parent = moduleElem.ParentNode;
                while (parent != null)
                {
                    if (moduleList.Contains(parent))
                    {
                        string parentId = ((XmlElement) parent).GetAttribute("id");
                        moduleDependencies[parentId].Add(moduleId);
                        moduleId = parentId;
                    }

                    parent = parent.ParentNode;
                }
            }

            // order the modules by processing order (nested modules processed first)
            moduleList = moduleList.OrderBy((node1, node2) =>
            {
                var parentId = ((XmlElement) node1).GetAttribute("id");
                var childId = ((XmlElement) node2).GetAttribute("id");

                var contains = moduleDependencies[parentId].Contains(childId);
                return contains ? 1 : 0;

            }).ToList();

            // create the modules and populate the modules dictionary
            SageModuleFactory factory = new SageModuleFactory();
            foreach (XmlElement moduleElem in moduleList)
            {
                var moduleId = moduleElem.GetAttribute("id");
                IModule module = factory.CreateModule(moduleElem);
                this.Modules.Add(moduleId, module);

                log.DebugFormat("Mapped element {0} to module {1} ({2}).", moduleElem.LocalName, module.GetType().Name, module.GetType().FullName);
            }
        }
コード例 #3
0
ファイル: ViewConfiguration.cs プロジェクト: igorfrance/sage
 /// <summary>
 /// Creates a new <see cref="ViewConfiguration"/> instance for the specified <paramref name="controller"/> and
 /// <paramref name="viewInfo"/>.
 /// </summary>
 /// <param name="controller">The controller for which to create the view configuration.</param>
 /// <param name="viewInfo">The object that contains the information about the view.</param>
 /// <returns>
 /// A new <see cref="ViewConfiguration"/> instance for the specified <paramref name="controller"/> and
 /// <paramref name="viewInfo"/>.
 /// </returns>
 public static ViewConfiguration Create(SageController controller, ViewInfo viewInfo)
 {
     return new ViewConfiguration(controller, viewInfo);
 }