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

@@ -64,38 +64,46 @@ namespace VEPROMS.CSLA.Library
}
#endregion
#region Collection
protected static List<Connection> _AllList = new List<Connection>();
private static Dictionary<string, List<Connection>> _AllByPrimaryKey = new Dictionary<string, List<Connection>>();
private static Dictionary<string, List<Connection>> _AllByName = new Dictionary<string, List<Connection>>();
private static List<Connection> _CacheList = new List<Connection>();
protected static void AddToCache(Connection connection)
{
if (!_CacheList.Contains(connection)) _CacheList.Add(connection); // In AddToCache
}
protected static void RemoveFromCache(Connection connection)
{
while (_CacheList.Contains(connection)) _CacheList.Remove(connection); // In RemoveFromCache
}
private static Dictionary<string, List<Connection>> _CacheByPrimaryKey = new Dictionary<string, List<Connection>>();
private static Dictionary<string, List<Connection>> _CacheByName = new Dictionary<string, List<Connection>>();
private static void ConvertListToDictionary()
{
List<Connection> remove = new List<Connection>();
foreach (Connection tmp in _AllList)
foreach (Connection tmp in _CacheList)
{
if (!_AllByPrimaryKey.ContainsKey(tmp.DBID.ToString()))
if (!_CacheByPrimaryKey.ContainsKey(tmp.DBID.ToString()))
{
_AllByPrimaryKey[tmp.DBID.ToString()] = new List<Connection>(); // Add new list for PrimaryKey
_AllByName[tmp.Name.ToString()] = new List<Connection>(); // Add new list for Name
_CacheByPrimaryKey[tmp.DBID.ToString()] = new List<Connection>(); // Add new list for PrimaryKey
_CacheByName[tmp.Name.ToString()] = new List<Connection>(); // Add new list for Name
}
_AllByPrimaryKey[tmp.DBID.ToString()].Add(tmp); // Add to Primary Key list
_AllByName[tmp.Name.ToString()].Add(tmp); // Unique Index
_CacheByPrimaryKey[tmp.DBID.ToString()].Add(tmp); // Add to Primary Key list
_CacheByName[tmp.Name.ToString()].Add(tmp); // Unique Index
remove.Add(tmp);
}
foreach (Connection tmp in remove)
_AllList.Remove(tmp);
RemoveFromCache(tmp);
}
public static Connection GetExistingByPrimaryKey(int dbid)
protected static Connection 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;
}
public static Connection GetExistingByName(string name)
protected static Connection GetCachedByName(string name)
{
ConvertListToDictionary();
string key = name.ToString();
if (_AllByName.ContainsKey(key)) return _AllByName[key][0];
if (_CacheByName.ContainsKey(key)) return _CacheByName[key][0];
return null;
}
#endregion
@@ -469,28 +477,41 @@ namespace VEPROMS.CSLA.Library
public int CurrentEditLevel
{ get { return EditLevel; } }
private static int _ConnectionUnique = 0;
private int _MyConnectionUnique;
protected static int ConnectionUnique
{ get { return ++_ConnectionUnique; } }
private int _MyConnectionUnique = ConnectionUnique;
public int MyConnectionUnique
{
get { return _MyConnectionUnique; }
}
{ get { return _MyConnectionUnique; } }
protected Connection()
{/* require use of factory methods */
_MyConnectionUnique = ++_ConnectionUnique;
_AllList.Add(this);
AddToCache(this);
}
public void Dispose()
{
_AllList.Remove(this);
if (!_AllByPrimaryKey.ContainsKey(DBID.ToString())) return;
List<Connection> listConnection = _AllByPrimaryKey[DBID.ToString()]; // Get the list of items
listConnection.Remove(this); // Remove the item from the list
if (listConnection.Count == 0) //If there are no items left in the list
_AllByPrimaryKey.Remove(DBID.ToString()); // remove the list
listConnection = _AllByName[DBID.ToString()]; // Get the list of items
listConnection.Remove(this); // Remove the item from the list
if (listConnection.Count == 0) //If there are no items left in the list
_AllByName.Remove(DBID.ToString()); // remove the list
RemoveFromDictionaries();
}
private void RemoveFromDictionaries()
{
RemoveFromCache(this);
if (_CacheByPrimaryKey.ContainsKey(DBID.ToString()))
{
List<Connection> listConnection = _CacheByPrimaryKey[DBID.ToString()]; // Get the list of items
while (listConnection.Contains(this)) listConnection.Remove(this); // Remove the item from the list
if (listConnection.Count == 0) //If there are no items left in the list
_CacheByPrimaryKey.Remove(DBID.ToString()); // remove the list
}
string myKey;
myKey = null;
foreach (string key in _CacheByName.Keys)
if (_CacheByName[key].Contains(this))
myKey = key;
if (myKey != null)
{
List<Connection> listConnection = _CacheByName[myKey]; // Get the list of items
listConnection.Remove(this); // Remove the item from the list
if (listConnection.Count == 0) //If there are no items left in the list
_CacheByName.Remove(myKey); // remove the list
}
}
public static Connection New()
{
@@ -521,7 +542,11 @@ namespace VEPROMS.CSLA.Library
{
Connection tmp = Connection.New(name, title, connectionString, serverType, config, dts, usrID);
if (tmp.IsSavable)
tmp = tmp.Save();
{
Connection tmp2 = tmp;
tmp = tmp2.Save();
tmp2.Dispose();
}
else
{
Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules();
@@ -546,7 +571,11 @@ namespace VEPROMS.CSLA.Library
{
Connection tmp = Connection.New(name, title, connectionString, config);
if (tmp.IsSavable)
tmp = tmp.Save();
{
Connection tmp2 = tmp;
tmp = tmp2.Save();
tmp2.Dispose();
}
else
{
Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules();
@@ -564,13 +593,17 @@ namespace VEPROMS.CSLA.Library
throw new System.Security.SecurityException("User not authorized to view a Connection");
try
{
Connection tmp = GetExistingByPrimaryKey(dbid);
Connection tmp = GetCachedByPrimaryKey(dbid);
if (tmp == null)
{
tmp = DataPortal.Fetch<Connection>(new PKCriteria(dbid));
if (!_AllList.Contains(tmp)) _AllList.Add(tmp);
AddToCache(tmp);
}
if (tmp.ErrorMessage == "No Record Found")
{
tmp.Dispose(); // Clean-up Connection
tmp = null;
}
if (tmp.ErrorMessage == "No Record Found") tmp = null;
return tmp;
}
catch (Exception ex)
@@ -578,19 +611,23 @@ namespace VEPROMS.CSLA.Library
throw new DbCslaException("Error on Connection.Get", ex);
}
}
private static Connection GetByName(string name)
public static Connection GetByName(string name)
{
if (!CanGetObject())
throw new System.Security.SecurityException("User not authorized to view a Connection");
try
{
Connection tmp = GetExistingByName(name);
Connection tmp = GetCachedByName(name);
if (tmp == null)
{
tmp = DataPortal.Fetch<Connection>(new NameCriteria(name));
if (!_AllList.Contains(tmp)) _AllList.Add(tmp);
AddToCache(tmp);
}
if (tmp.ErrorMessage == "No Record Found")
{
tmp.Dispose(); // Clean-up Connection
tmp = null;
}
if (tmp.ErrorMessage == "No Record Found") tmp = null;
return tmp;
}
catch (Exception ex)
@@ -632,7 +669,8 @@ namespace VEPROMS.CSLA.Library
{
BuildRefreshList();
Connection connection = base.Save();
_AllList.Add(connection);//Refresh the item in AllList
RemoveFromDictionaries(); // if save is successful remove the previous Folder from the cache
AddToCache(connection);//Refresh the item in AllList
ProcessRefreshList();
return connection;
}