This commit is contained in:
2008-08-12 11:22:10 +00:00
parent d0dba2aad4
commit 0c505e9455
3 changed files with 146 additions and 75 deletions

View File

@@ -36,32 +36,40 @@ namespace VEPROMS.CSLA.Library
private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#endregion
#region Collection
protected static List<ConnectionInfo> _AllList = new List<ConnectionInfo>();
private static Dictionary<string, List<ConnectionInfo>> _AllByPrimaryKey = new Dictionary<string, List<ConnectionInfo>>();
private static List<ConnectionInfo> _CacheList = new List<ConnectionInfo>();
protected static void AddToCache(ConnectionInfo connectionInfo)
{
if (!_CacheList.Contains(connectionInfo)) _CacheList.Add(connectionInfo); // In AddToCache
}
protected static void RemoveFromCache(ConnectionInfo connectionInfo)
{
while (_CacheList.Contains(connectionInfo)) _CacheList.Remove(connectionInfo); // In RemoveFromCache
}
private static Dictionary<string, List<ConnectionInfo>> _CacheByPrimaryKey = new Dictionary<string, List<ConnectionInfo>>();
private static void ConvertListToDictionary()
{
List<ConnectionInfo> remove = new List<ConnectionInfo>();
foreach (ConnectionInfo tmp in _AllList)
foreach (ConnectionInfo tmp in _CacheList)
{
if (!_AllByPrimaryKey.ContainsKey(tmp.DBID.ToString()))
if (!_CacheByPrimaryKey.ContainsKey(tmp.DBID.ToString()))
{
_AllByPrimaryKey[tmp.DBID.ToString()] = new List<ConnectionInfo>(); // Add new list for PrimaryKey
_CacheByPrimaryKey[tmp.DBID.ToString()] = new List<ConnectionInfo>(); // Add new list for PrimaryKey
}
_AllByPrimaryKey[tmp.DBID.ToString()].Add(tmp); // Add to Primary Key list
_CacheByPrimaryKey[tmp.DBID.ToString()].Add(tmp); // Add to Primary Key list
remove.Add(tmp);
}
foreach (ConnectionInfo tmp in remove)
_AllList.Remove(tmp);
RemoveFromCache(tmp);
}
internal static void AddList(ConnectionInfoList lst)
{
foreach (ConnectionInfo item in lst) _AllList.Add(item);
foreach (ConnectionInfo item in lst) AddToCache(item);
}
public static ConnectionInfo GetExistingByPrimaryKey(int dbid)
protected static ConnectionInfo GetCachedByPrimaryKey(int dbid)
{
ConvertListToDictionary();
string key = dbid.ToString();
if (_AllByPrimaryKey.ContainsKey(key)) return _AllByPrimaryKey[key][0];
if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
return null;
}
#endregion
@@ -194,11 +202,11 @@ namespace VEPROMS.CSLA.Library
return _ConnectionFolders;
}
}
internal void RefreshConnectionFolders()
public void RefreshConnectionFolders()
{
ConvertListToDictionary();
if (_AllByPrimaryKey.ContainsKey(_DBID.ToString()))
foreach (ConnectionInfo tmp in _AllByPrimaryKey[_DBID.ToString()])
if (_CacheByPrimaryKey.ContainsKey(_DBID.ToString()))
foreach (ConnectionInfo tmp in _CacheByPrimaryKey[_DBID.ToString()])
tmp._ConnectionFolderCount = -1; // This will cause the data to be requeried
}
// TODO: Replace base ConnectionInfo.ToString function as necessary
@@ -222,24 +230,23 @@ namespace VEPROMS.CSLA.Library
#endregion
#region Factory Methods
private static int _ConnectionInfoUnique = 0;
private int _MyConnectionInfoUnique;
private static int ConnectionInfoUnique
{ get { return ++_ConnectionInfoUnique; } }
private int _MyConnectionInfoUnique = ConnectionInfoUnique;
public int MyConnectionInfoUnique
{
get { return _MyConnectionInfoUnique; }
}
private ConnectionInfo()
{ get { return _MyConnectionInfoUnique; } }
protected ConnectionInfo()
{/* require use of factory methods */
_MyConnectionInfoUnique = ++_ConnectionInfoUnique;
_AllList.Add(this);
AddToCache(this);
}
public void Dispose()
{
_AllList.Remove(this);
if (!_AllByPrimaryKey.ContainsKey(DBID.ToString())) return;
List<ConnectionInfo> listConnectionInfo = _AllByPrimaryKey[DBID.ToString()]; // Get the list of items
listConnectionInfo.Remove(this); // Remove the item from the list
RemoveFromCache(this);
if (!_CacheByPrimaryKey.ContainsKey(DBID.ToString())) return;
List<ConnectionInfo> listConnectionInfo = _CacheByPrimaryKey[DBID.ToString()]; // Get the list of items
while (listConnectionInfo.Contains(this)) listConnectionInfo.Remove(this); // Remove the item from the list
if (listConnectionInfo.Count == 0) // If there are no items left in the list
_AllByPrimaryKey.Remove(DBID.ToString()); // remove the list
_CacheByPrimaryKey.Remove(DBID.ToString()); // remove the list
}
public virtual Connection Get()
{
@@ -249,11 +256,11 @@ namespace VEPROMS.CSLA.Library
{
string key = tmp.DBID.ToString();
ConvertListToDictionary();
if (_AllByPrimaryKey.ContainsKey(key))
foreach (ConnectionInfo tmpInfo in _AllByPrimaryKey[key])
if (_CacheByPrimaryKey.ContainsKey(key))
foreach (ConnectionInfo tmpInfo in _CacheByPrimaryKey[key])
tmpInfo.RefreshFields(tmp);
}
private void RefreshFields(Connection tmp)
protected virtual void RefreshFields(Connection tmp)
{
_Name = tmp.Name;
_Title = tmp.Title;
@@ -271,13 +278,17 @@ namespace VEPROMS.CSLA.Library
// throw new System.Security.SecurityException("User not authorized to view a Connection");
try
{
ConnectionInfo tmp = GetExistingByPrimaryKey(dbid);
ConnectionInfo tmp = GetCachedByPrimaryKey(dbid);
if (tmp == null)
{
tmp = DataPortal.Fetch<ConnectionInfo>(new PKCriteria(dbid));
if (!_AllList.Contains(tmp)) _AllList.Add(tmp);
AddToCache(tmp);
}
if (tmp.ErrorMessage == "No Record Found")
{
tmp.Dispose(); // Clean-up ConnectionInfo
tmp = null;
}
if (tmp.ErrorMessage == "No Record Found") tmp = null;
return tmp;
}
catch (Exception ex)
@@ -289,7 +300,6 @@ namespace VEPROMS.CSLA.Library
#region Data Access Portal
internal ConnectionInfo(SafeDataReader dr)
{
_MyConnectionInfoUnique = ++_ConnectionInfoUnique;
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] ConnectionInfo.Constructor", GetHashCode());
try
{