#region Apache Notice /***************************************************************************** * $Revision: 387044 $ * $LastChangedDate$ * $LastChangedBy$ * * iBATIS.NET Data Mapper * Copyright (C) 2008/2005 - The Apache Software Foundation * * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ********************************************************************************/ #endregion using System; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.CompilerServices; using System.Text; using Apache.Ibatis.Common.Configuration; using Apache.Ibatis.Common.Data; namespace Apache.Ibatis.DataMapper.Configuration { /// /// This implementation of /// [Serializable] [DebuggerDisplay("DefaultConfigurationStore")] public class DefaultConfigurationStore : IConfigurationStore { #region private fieds private readonly Dictionary properties = new Dictionary(); private readonly Dictionary settings = new Dictionary(); private readonly Dictionary providers = new Dictionary(); private readonly Dictionary databases = new Dictionary(); private readonly Dictionary typeHandlers = new Dictionary(); private readonly Dictionary alias = new Dictionary(); private readonly Dictionary caches = new Dictionary(); private readonly Dictionary resultMaps = new Dictionary(); private readonly Dictionary statements = new Dictionary(); private readonly Dictionary parameterMaps = new Dictionary(); private readonly List propertyList = new List(); private readonly ConfigurationCollection settingList = new ConfigurationCollection(); private readonly List providerList = new List(); private readonly List databaseList = new List(); private readonly List typeHandlerList = new List(); private readonly List aliasList = new List(); private readonly List sqlMappingList = new List(); private readonly List cacheList = new List(); private readonly List resultMapList = new List(); private readonly List statementList = new List(); private readonly List parameterMapList = new List(); #endregion #region IConfigurationStore Members /// /// Associates a configuration node with a property key /// /// Configuration node [MethodImpl(MethodImplOptions.Synchronized)] public void AddPropertyConfiguration(IConfiguration config) { AddToList(config, properties, propertyList); properties[config.Id] = config; } /// /// Associates a configuration node with a Setting key /// /// Configuration node [MethodImpl(MethodImplOptions.Synchronized)] public void AddSettingConfiguration(IConfiguration config) { AddToList(config, settings, settingList); settings[config.Id] = config; } /// /// Associates a configuration node with a Provider key /// /// Configuration node [MethodImpl(MethodImplOptions.Synchronized)] public void AddProviderConfiguration(IConfiguration config) { AddToList(config, providers, providerList); providers[config.Id] = config; } /// /// Associates a configuration node with a Database key /// /// Configuration node [MethodImpl(MethodImplOptions.Synchronized)] public void AddDatabaseConfiguration(IConfiguration config) { AddToList(config, databases, databaseList); databases[config.Id] = config; } /// /// Associates a configuration node with a TypeHandler key /// /// Configuration node [MethodImpl(MethodImplOptions.Synchronized)] public void AddTypeHandlerConfiguration(IConfiguration config) { AddToList(config, typeHandlers, typeHandlerList); typeHandlers[config.Id] = config; } /// /// Associates a configuration node with a Alias key /// /// Configuration node [MethodImpl(MethodImplOptions.Synchronized)] public void AddAliasConfiguration(IConfiguration config) { AddToList(config, alias, aliasList); alias[config.Id] = config; } /// /// Associates a configuration node with a Cache key /// /// Configuration node [MethodImpl(MethodImplOptions.Synchronized)] public void AddCacheConfiguration(IConfiguration config) { AddToList(config, caches, cacheList); caches[config.Id] = config; } /// /// Associates a configuration node with a ResultMap key /// /// Configuration node [MethodImpl(MethodImplOptions.Synchronized)] public void AddResultMapConfiguration(IConfiguration config) { AddToList(config, resultMaps, resultMapList); resultMaps[config.Id] = config; } /// /// Associates a configuration node with a Statement key /// /// Configuration node [MethodImpl(MethodImplOptions.Synchronized)] public void AddStatementConfiguration(IConfiguration config) { AddToList(config, statements, statementList); statements[config.Id] = config; } /// /// Associates a configuration node with a ParameterMap key /// /// Configuration node [MethodImpl(MethodImplOptions.Synchronized)] public void AddParameterMapConfiguration(IConfiguration config) { AddToList(config, parameterMaps, parameterMapList); parameterMaps[config.Id] = config; } /// /// Returns the configuration node associated with /// the specified setting key. Should return null /// if no association exists. /// /// item key /// public IConfiguration GetPropertyConfiguration(string key) { return GetConfiguration(properties, key); } /// /// Returns all configuration nodes for property /// /// public IConfiguration[] Properties { get { return propertyList.ToArray(); } } /// /// Returns the configuration node associated with /// the specified setting key. Should return null /// if no association exists. /// /// item key /// public IConfiguration GetSettingConfiguration(string key) { return GetConfiguration(settings, key); } /// /// Returns all configuration nodes for setting /// /// public ConfigurationCollection Settings { get { return settingList; } } /// /// Returns the configuration node associated with /// the specified key. Should return null /// if no association exists. /// /// item key /// public IConfiguration GetProviderConfiguration(string key) { return GetConfiguration(providers, key); } /// /// Returns all configuration nodes for /// /// public IConfiguration[] Providers { get { return providerList.ToArray();} } /// /// Returns the configuration node associated with /// the specified Database key. Should return null /// if no association exists. /// /// item key /// public IConfiguration GetDatabaseConfiguration(string key) { return GetConfiguration(databases, key); } /// /// Returns all configuration nodes for Database /// /// public IConfiguration[] Databases { get { return databaseList.ToArray();} } /// /// Returns the configuration node associated with /// the specified TypeHandler key. Should return null /// if no association exists. /// /// item key /// public IConfiguration GetTypeHandlerConfiguration(string key) { return GetConfiguration(typeHandlers, key); } /// /// Returns all configuration nodes for TypeHandler /// /// public IConfiguration[] TypeHandlers { get { return typeHandlerList.ToArray();} } /// /// Returns the configuration node associated with /// the specified Alias key. Should return null /// if no association exists. /// /// item key /// public IConfiguration GetAliasConfiguration(string key) { return GetConfiguration(alias, key); } /// /// Returns all configuration nodes for Alias /// /// public IConfiguration[] Alias { get { return aliasList.ToArray();} } /// /// Returns all configuration nodes for SqlMapping /// /// public IConfiguration[] SqlMappings { get { return sqlMappingList.ToArray();} } /// /// Returns the configuration node associated with /// the specified Cache key. Should return null /// if no association exists. /// /// item key /// public IConfiguration GetCacheConfiguration(string key) { return GetConfiguration(caches, key); } /// /// Returns all configuration nodes for Cache /// /// public IConfiguration[] CacheModels { get { return cacheList.ToArray();} } /// /// Returns the configuration node associated with /// the specified ResultMap key. Should return null /// if no association exists. /// /// item key /// public IConfiguration GetResultMapConfiguration(string key) { return GetConfiguration(resultMaps, key); } /// /// Returns all configuration nodes for ResultMap /// /// public IConfiguration[] ResultMaps { get { return resultMapList.ToArray();} } /// /// Returns the configuration node associated with /// the specified Statement key. Should return null /// if no association exists. /// /// item key /// public IConfiguration GetStatementConfiguration(string key) { return GetConfiguration(statements, key); } /// /// Returns all configuration nodes for Statement /// /// public IConfiguration[] Statements { get { return statementList.ToArray();} } /// /// Returns the configuration node associated with /// the specified ParameterMap key. Should return null /// if no association exists. /// /// item key /// public IConfiguration GetParameterMapConfiguration(string key) { return GetConfiguration(parameterMaps, key); } /// /// Returns all configuration nodes for ParameterMap /// /// public IConfiguration[] ParameterMaps { get { return parameterMapList.ToArray(); } } #endregion private IConfiguration GetConfiguration(Dictionary map, string key) { IConfiguration config = null; map.TryGetValue(key, out config); return config; } private void Browse(StringBuilder builder, IConfiguration[] tab, int level) { for (int i = 0; i < tab.Length; i++) { builder.AppendLine(string.Empty.PadLeft(level * 4, ' ') + tab[i].Type + "/" + tab[i].Id); IEnumerator> attributes = tab[i].Attributes.GetEnumerator(); while (attributes.MoveNext()) { builder.AppendLine(string.Empty.PadLeft((level + 1) * 4, ' ') + attributes.Current.Key + "/" + attributes.Current.Value); } Browse(builder, tab[i].Children.ToArray(), level + 1); } } /// /// Returns a that represents the current . /// /// /// A that represents the current . /// public override string ToString() { StringBuilder builder = new StringBuilder(); builder.AppendLine("Databases:"); Browse(builder, Databases, 1); builder.AppendLine(string.Empty); builder.AppendLine("Alias:"); Browse(builder, Alias, 1); builder.AppendLine(string.Empty); builder.AppendLine("TypeHandlers:"); Browse(builder, TypeHandlers, 1); builder.AppendLine(string.Empty); builder.AppendLine("CacheModel:"); Browse(builder, CacheModels, 1); builder.AppendLine(string.Empty); builder.AppendLine("ResultMaps:"); Browse(builder, ResultMaps, 1); builder.AppendLine(string.Empty); builder.AppendLine("Statements:"); Browse(builder, Statements, 1); builder.AppendLine(string.Empty); builder.AppendLine("ParameterMaps:"); Browse(builder, ParameterMaps, 1); builder.AppendLine(string.Empty); return builder.ToString(); } /// /// Adds to list, remove the element /// if it is already register under the same id in the dictionary. /// /// The config. /// The dictionary. /// The list. private void AddToList(IConfiguration config, IDictionary dictionary, List list) { if (dictionary.ContainsKey(config.Id)) { IConfiguration configuration = list.Find(delegate(IConfiguration item) { return (item.Id == config.Id); }); list.Remove(configuration); } list.Add(config); } } }