Пример #1
0
        /// <summary>
        /// Build dao contexts
        /// </summary>
        /// <param name="configurationScope">The scope of the configuration</param>
        private void GetContexts(ConfigurationScope configurationScope)
        {
            DaoManager   daoManager = null;
            XmlAttribute attribute  = null;

            // Init
            DaoManager.Reset();

            // Build one daoManager for each context
            foreach (XmlNode contextNode in configurationScope.DaoConfigDocument.SelectNodes(ApplyNamespacePrefix(XML_DAO_CONTEXT), configurationScope.XmlNamespaceManager))
            {
                configurationScope.ErrorContext.Activity = "build daoManager";
                configurationScope.NodeContext           = contextNode;

                #region Configure a new DaoManager

                attribute  = contextNode.Attributes["id"];
                daoManager = DaoManager.NewInstance(attribute.Value);

                configurationScope.ErrorContext.Activity += daoManager.Id;

                // default
                attribute = contextNode.Attributes["default"];
                if (attribute != null)
                {
                    if (attribute.Value == "true")
                    {
                        daoManager.IsDefault = true;
                    }
                    else
                    {
                        daoManager.IsDefault = false;
                    }
                }
                else
                {
                    daoManager.IsDefault = false;
                }
                #endregion

                #region Properties
                ParseGlobalProperties(configurationScope);
                #endregion

                #region provider
                daoManager.DbProvider = ParseProvider(configurationScope);

                configurationScope.ErrorContext.Resource = string.Empty;
                configurationScope.ErrorContext.MoreInfo = string.Empty;
                configurationScope.ErrorContext.ObjectId = string.Empty;
                #endregion

                #region DataSource
                daoManager.DataSource            = ParseDataSource(configurationScope);
                daoManager.DataSource.DbProvider = daoManager.DbProvider;
                #endregion

                #region DaoSessionHandler

                XmlNode nodeSessionHandler = contextNode.SelectSingleNode(ApplyNamespacePrefix(XML_DAO_SESSION_HANDLER), configurationScope.XmlNamespaceManager);

                configurationScope.ErrorContext.Activity = "configure DaoSessionHandler";

                // The resources use to initialize the SessionHandler
                IDictionary resources = new Hashtable();
                // By default, add the DataSource
                resources.Add("DataSource", daoManager.DataSource);
                // By default, add the useConfigFileWatcher
                resources.Add("UseConfigFileWatcher", configurationScope.UseConfigFileWatcher);

                IDaoSessionHandler sessionHandler = null;
                Type typeSessionHandler           = null;

                if (nodeSessionHandler != null)
                {
                    configurationScope.ErrorContext.Resource = nodeSessionHandler.InnerXml.ToString();

                    typeSessionHandler = configurationScope.DaoSectionHandlers[nodeSessionHandler.Attributes[ID_ATTRIBUTE].Value] as Type;

                    // Parse property node
                    foreach (XmlNode nodeProperty in nodeSessionHandler.SelectNodes(ApplyNamespacePrefix(XML_PROPERTY), configurationScope.XmlNamespaceManager))
                    {
                        resources.Add(nodeProperty.Attributes["name"].Value,
                                      NodeUtils.ParsePropertyTokens(nodeProperty.Attributes["value"].Value, configurationScope.Properties));
                    }
                }
                else
                {
                    typeSessionHandler = configurationScope.DaoSectionHandlers[DEFAULT_DAOSESSIONHANDLER_NAME] as Type;
                }

                // Configure the sessionHandler
                configurationScope.ErrorContext.ObjectId = typeSessionHandler.FullName;

                try
                {
                    sessionHandler = (IDaoSessionHandler)Activator.CreateInstance(typeSessionHandler, EmptyObjects);
                }
                catch (Exception e)
                {
                    throw new ConfigurationException(
                              string.Format("DaoManager could not configure DaoSessionHandler. DaoSessionHandler of type \"{0}\", failed. Cause: {1}", typeSessionHandler.Name, e.Message), e
                              );
                }

                sessionHandler.Configure(configurationScope.Properties, resources);

                daoManager.DaoSessionHandler = sessionHandler;

                configurationScope.ErrorContext.Resource = string.Empty;
                configurationScope.ErrorContext.MoreInfo = string.Empty;
                configurationScope.ErrorContext.ObjectId = string.Empty;
                #endregion

                #region Build Daos
                ParseDaoFactory(configurationScope, daoManager);
                #endregion

                #region Register DaoManager

                configurationScope.ErrorContext.MoreInfo = "register DaoManager";
                configurationScope.ErrorContext.ObjectId = daoManager.Id;

                DaoManager.RegisterDaoManager(daoManager.Id, daoManager);

                configurationScope.ErrorContext.Resource = string.Empty;
                configurationScope.ErrorContext.MoreInfo = string.Empty;
                configurationScope.ErrorContext.ObjectId = string.Empty;
                #endregion
            }
        }