From: <fab...@us...> - 2009-05-15 06:31:53
|
Revision: 4312 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=4312&view=rev Author: fabiomaulo Date: 2009-05-15 06:31:42 +0000 (Fri, 15 May 2009) Log Message: ----------- Minor (refactoring) Modified Paths: -------------- trunk/nhibernate/src/NHibernate/SqlTypes/SqlTypeFactory.cs Modified: trunk/nhibernate/src/NHibernate/SqlTypes/SqlTypeFactory.cs =================================================================== --- trunk/nhibernate/src/NHibernate/SqlTypes/SqlTypeFactory.cs 2009-05-15 05:10:15 UTC (rev 4311) +++ trunk/nhibernate/src/NHibernate/SqlTypes/SqlTypeFactory.cs 2009-05-15 06:31:42 UTC (rev 4312) @@ -1,6 +1,7 @@ using System; -using System.Collections; using System.Data; +using System.Collections.Generic; +using NHibernate.Util; namespace NHibernate.SqlTypes { @@ -8,18 +9,14 @@ /// SqlTypeFactory provides Singleton access to the SqlTypes. /// </summary> [Serializable] - public sealed class SqlTypeFactory + public static class SqlTypeFactory { // key = typeof(sqlType).Name : ie - BinarySqlType(l), BooleanSqlType, DecimalSqlType(p,s) // value = SqlType - private static Hashtable sqlTypes = Hashtable.Synchronized(new Hashtable(41)); + private static readonly IDictionary<string, SqlType> SqlTypes = + new ThreadSafeDictionary<string, SqlType>(new Dictionary<string, SqlType>(128)); - private SqlTypeFactory() - { - } - public static readonly SqlType Guid = new SqlType(DbType.Guid); - public static readonly SqlType Boolean = new SqlType(DbType.Boolean); public static readonly SqlType Byte = new SqlType(DbType.Byte); public static readonly SqlType Currency = new SqlType(DbType.Currency); @@ -41,47 +38,38 @@ public static readonly SqlType[] NoTypes = new SqlType[0]; - public static AnsiStringSqlType GetAnsiString(int length) + private delegate SqlType TypeWithLenCreateDelegate(int length); // Func<int, T> + + private static T GetTypeWithLen<T>(int length, TypeWithLenCreateDelegate createDelegate) where T : SqlType { - string key = GetKeyForLengthBased(typeof(AnsiStringSqlType).Name, length); - AnsiStringSqlType returnSqlType = (AnsiStringSqlType) sqlTypes[key]; - if (returnSqlType == null) + string key = GetKeyForLengthBased(typeof (T).Name, length); + SqlType result; + if (!SqlTypes.TryGetValue(key, out result)) { - returnSqlType = new AnsiStringSqlType(length); - sqlTypes.Add(key, returnSqlType); + result = createDelegate(length); + SqlTypes.Add(key, result); } - return returnSqlType; + return (T) result; } - public static BinarySqlType GetBinary(int length) + public static AnsiStringSqlType GetAnsiString(int length) { - string key = GetKeyForLengthBased(typeof(BinarySqlType).Name, length); - BinarySqlType returnSqlType = (BinarySqlType) sqlTypes[key]; - if (returnSqlType == null) - { - returnSqlType = new BinarySqlType(length); - sqlTypes.Add(key, returnSqlType); - } - return returnSqlType; + return GetTypeWithLen<AnsiStringSqlType>(length, l => new AnsiStringSqlType(l)); } - public static SqlType GetDecimal(byte precision, byte scale) + public static BinarySqlType GetBinary(int length) { - return new SqlType(DbType.Decimal, precision, scale); + return GetTypeWithLen<BinarySqlType>(length, l => new BinarySqlType(l)); } public static StringSqlType GetString(int length) { - string key = GetKeyForLengthBased(typeof(StringSqlType).Name, length); + return GetTypeWithLen<StringSqlType>(length, l => new StringSqlType(l)); + } - StringSqlType returnSqlType = (StringSqlType) sqlTypes[key]; - if (returnSqlType == null) - { - returnSqlType = new StringSqlType(length); - sqlTypes.Add(key, returnSqlType); - } - - return returnSqlType; + public static SqlType GetDecimal(byte precision, byte scale) + { + return new SqlType(DbType.Decimal, precision, scale); } private static string GetKeyForLengthBased(string name, int length) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |