dictionarymanager.cs

来自「该项目中对 SQLHelper 类进行了简单封装」· CS 代码 · 共 111 行

CS
111
字号
/* 
 * DictionaryManager.cs @Microsoft Visual Studio 2005 <.NET Framework 2.0>
 * AfritXia
 * 2007-05-31
 * 
 * Copyright(c) http://www.AfritXia.NET/
 * 
 */

using System;
using System.Collections.Generic;
using System.Xml;

namespace NET.AfritXia.DBUtility
{
	/// <summary>字典管理器</summary>
	internal sealed class DictionaryManager
	{
		// 字典缓存
		private Dictionary<Type, Dictionary<string, string>> m_dictionaryCache = null;
		// 字典供应者
		private IOrmDictionaryProvider m_dictionaryProvider = null;

		#region 类 DictionaryManager 构造器
		/// <summary>
		/// 类 DictionaryManager 默认构造器
		/// </summary>
		internal DictionaryManager()
		{
			this.m_dictionaryCache = new Dictionary<Type, Dictionary<string, string>>();
		}
		#endregion

		/// <summary>
		/// 设置或获取字典供应者
		/// </summary>
		internal IOrmDictionaryProvider DictionaryProvider
		{
			set
			{
				this.m_dictionaryProvider = value;
			}

			get
			{
				return this.m_dictionaryProvider;
			}
		}

		/// <summary>
		/// 通过类型获取字典
		/// </summary>
		/// <param name="type">类型声明</param>
		/// <returns></returns>
		internal Dictionary<string, string> GetOrmDictionary(Type type)
		{
			// 获取字典对象
			Dictionary<string, string> ormDictionary = null;

			if (this.m_dictionaryCache.ContainsKey(type) == true)
				ormDictionary = this.m_dictionaryCache[type];

			if (ormDictionary != null)
				return ormDictionary;

			if (this.DictionaryProvider == null)
				return null;

			lock (this)
			{
				if (this.m_dictionaryCache.ContainsKey(type) == false)
				{
					ormDictionary = new Dictionary<string, string>();

					// 从字典供应者中获取 XML 字典
					XmlDocument xmlDoc = this.DictionaryProvider.GetXmlDictionary(type);

					// 抛出 NullTypeDictionaryException 异常
					if (xmlDoc == null || xmlDoc.DocumentElement == null)
						throw new NullOrmDictionaryException(type);

					// <OrmDictionary></OrmDictionary>
					XmlNode rootNode = xmlDoc.DocumentElement;
					// <item></item>
					XmlNodeList itemNodeList = rootNode.SelectNodes("item");

					foreach (XmlNode itemNode in itemNodeList)
					{
						// <dbColumnName></dbColumnName>
						XmlNode dbColumnNameNode = itemNode.SelectSingleNode("dbColumnName");
						// <objectPropertyName></objectPropertyName>
						XmlNode objectPropertyNameNode = itemNode.SelectSingleNode("objectPropertyName");

						// 获取数据库字段名称
						string dbColumnName = dbColumnNameNode.InnerText;
						// 获取对象属性名称
						string objectPropertyName = objectPropertyNameNode.InnerText;

						// 添加键值到字典
						ormDictionary.Add(dbColumnName, objectPropertyName);
					}

					// 缓存字典对象
					this.m_dictionaryCache.Add(type, ormDictionary);
				}
			}

			return ormDictionary;
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?