Exemplo n.º 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
            }
        }
Exemplo n.º 2
0
 public static void ThirdDbInit()
 {
     try
     {
         object[] args = new object[0];
         IList <ZLSoft.Model.PLATFORM.DataSource> list = DB.List <ZLSoft.Model.PLATFORM.DataSource>(null);
         foreach (ZLSoft.Model.PLATFORM.DataSource current in list)
         {
             if (DaoManager.GetInstance(current.ID) == null)
             {
                 if (dal.TheConfigurationScope.Providers.Contains(current.Provider))
                 {
                     ExecuteSqlProviderAttacher.DBType dBType =
                         ExecuteSqlProviderAttacher.CheckDBType(current.Provider);
                     string sERVER_NAME = current.DataSourceName;
                     string dATABASE    = current.Database;
                     string uSERNAME    = current.UserName;
                     string pASSWORD    = current.Password;
                     string key         = "resource";
                     string mAPSFILE    = "dbconfig/SqlMap_Interface.config";
                     string connectionString;
                     if (dBType.ToString() == "NONE" || dBType == ExecuteSqlProviderAttacher.DBType.ORACLE)
                     {
                         connectionString = "Data Source={0};User ID={1};Password={2};";
                         connectionString = string.Format(connectionString, dATABASE, uSERNAME, pASSWORD);
                     }
                     else
                     {
                         if (dBType != ExecuteSqlProviderAttacher.DBType.SQLSERVER)
                         {
                             continue;
                         }
                         connectionString = ExecuteSqlProviderAttacher.GetSqlServerConnectString(sERVER_NAME,
                                                                                                 dATABASE, uSERNAME, pASSWORD);
                     }
                     DaoManager daoManager = DaoManager.NewInstance(current.ID);
                     daoManager.IsDefault  = false;
                     daoManager.DbProvider =
                         (dal.TheConfigurationScope.Providers[current.Provider] as IDbProvider);
                     daoManager.DataSource = new IBatisNet.Common.DataSource
                     {
                         Name             = sERVER_NAME,
                         ConnectionString = connectionString,
                         DbProvider       = daoManager.DbProvider
                     };
                     IDictionary dictionary = new Hashtable();
                     dictionary.Add("DataSource", daoManager.DataSource);
                     dictionary.Add("UseConfigFileWatcher", false);
                     dictionary.Add(key, mAPSFILE);
                     Type type =
                         TypeUtils.ResolveType("IBatisNet.DataAccess.DaoSessionHandlers.SqlMapDaoSessionHandler");
                     IDaoSessionHandler daoSessionHandler =
                         System.Activator.CreateInstance(type, args) as IDaoSessionHandler;
                     daoSessionHandler.Configure(dal.TheConfigurationScope.Properties, dictionary);
                     daoManager.DaoSessionHandler = daoSessionHandler;
                     DaoManager.RegisterDaoManager(daoManager.Id, daoManager);
                 }
             }
         }
     }
     catch
     {
     }
 }