This commit is contained in:
2008-08-12 11:21:19 +00:00
parent 610f408c6d
commit d0dba2aad4
14 changed files with 937 additions and 240 deletions

View File

@@ -54,28 +54,36 @@ namespace VEPROMS.CSLA.Library
}
#endregion
#region Collection
protected static List<Assignment> _AllList = new List<Assignment>();
private static Dictionary<string, List<Assignment>> _AllByPrimaryKey = new Dictionary<string, List<Assignment>>();
private static List<Assignment> _CacheList = new List<Assignment>();
protected static void AddToCache(Assignment assignment)
{
if (!_CacheList.Contains(assignment)) _CacheList.Add(assignment); // In AddToCache
}
protected static void RemoveFromCache(Assignment assignment)
{
while (_CacheList.Contains(assignment)) _CacheList.Remove(assignment); // In RemoveFromCache
}
private static Dictionary<string, List<Assignment>> _CacheByPrimaryKey = new Dictionary<string, List<Assignment>>();
private static void ConvertListToDictionary()
{
List<Assignment> remove = new List<Assignment>();
foreach (Assignment tmp in _AllList)
foreach (Assignment tmp in _CacheList)
{
if (!_AllByPrimaryKey.ContainsKey(tmp.AID.ToString()))
if (!_CacheByPrimaryKey.ContainsKey(tmp.AID.ToString()))
{
_AllByPrimaryKey[tmp.AID.ToString()] = new List<Assignment>(); // Add new list for PrimaryKey
_CacheByPrimaryKey[tmp.AID.ToString()] = new List<Assignment>(); // Add new list for PrimaryKey
}
_AllByPrimaryKey[tmp.AID.ToString()].Add(tmp); // Add to Primary Key list
_CacheByPrimaryKey[tmp.AID.ToString()].Add(tmp); // Add to Primary Key list
remove.Add(tmp);
}
foreach (Assignment tmp in remove)
_AllList.Remove(tmp);
RemoveFromCache(tmp);
}
public static Assignment GetExistingByPrimaryKey(int aid)
protected static Assignment GetCachedByPrimaryKey(int aid)
{
ConvertListToDictionary();
string key = aid.ToString();
if (_AllByPrimaryKey.ContainsKey(key)) return _AllByPrimaryKey[key][0];
if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
return null;
}
#endregion
@@ -504,24 +512,29 @@ namespace VEPROMS.CSLA.Library
public int CurrentEditLevel
{ get { return EditLevel; } }
private static int _AssignmentUnique = 0;
private int _MyAssignmentUnique;
protected static int AssignmentUnique
{ get { return ++_AssignmentUnique; } }
private int _MyAssignmentUnique = AssignmentUnique;
public int MyAssignmentUnique
{
get { return _MyAssignmentUnique; }
}
{ get { return _MyAssignmentUnique; } }
protected Assignment()
{/* require use of factory methods */
_MyAssignmentUnique = ++_AssignmentUnique;
_AllList.Add(this);
AddToCache(this);
}
public void Dispose()
{
_AllList.Remove(this);
if (!_AllByPrimaryKey.ContainsKey(AID.ToString())) return;
List<Assignment> listAssignment = _AllByPrimaryKey[AID.ToString()]; // Get the list of items
listAssignment.Remove(this); // Remove the item from the list
if (listAssignment.Count == 0) //If there are no items left in the list
_AllByPrimaryKey.Remove(AID.ToString()); // remove the list
RemoveFromDictionaries();
}
private void RemoveFromDictionaries()
{
RemoveFromCache(this);
if (_CacheByPrimaryKey.ContainsKey(AID.ToString()))
{
List<Assignment> listAssignment = _CacheByPrimaryKey[AID.ToString()]; // Get the list of items
while (listAssignment.Contains(this)) listAssignment.Remove(this); // Remove the item from the list
if (listAssignment.Count == 0) //If there are no items left in the list
_CacheByPrimaryKey.Remove(AID.ToString()); // remove the list
}
}
public static Assignment New()
{
@@ -560,7 +573,11 @@ namespace VEPROMS.CSLA.Library
{
Assignment tmp = Assignment.New(myGroup, myRole, myFolder, startDate, endDate, dts, usrID);
if (tmp.IsSavable)
tmp = tmp.Save();
{
Assignment tmp2 = tmp;
tmp = tmp2.Save();
tmp2.Dispose();
}
else
{
Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules();
@@ -585,7 +602,11 @@ namespace VEPROMS.CSLA.Library
{
Assignment tmp = Assignment.New(myGroup, myRole, myFolder, endDate);
if (tmp.IsSavable)
tmp = tmp.Save();
{
Assignment tmp2 = tmp;
tmp = tmp2.Save();
tmp2.Dispose();
}
else
{
Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules();
@@ -603,13 +624,17 @@ namespace VEPROMS.CSLA.Library
throw new System.Security.SecurityException("User not authorized to view a Assignment");
try
{
Assignment tmp = GetExistingByPrimaryKey(aid);
Assignment tmp = GetCachedByPrimaryKey(aid);
if (tmp == null)
{
tmp = DataPortal.Fetch<Assignment>(new PKCriteria(aid));
if (!_AllList.Contains(tmp)) _AllList.Add(tmp);
AddToCache(tmp);
}
if (tmp.ErrorMessage == "No Record Found")
{
tmp.Dispose(); // Clean-up Assignment
tmp = null;
}
if (tmp.ErrorMessage == "No Record Found") tmp = null;
return tmp;
}
catch (Exception ex)
@@ -651,7 +676,8 @@ namespace VEPROMS.CSLA.Library
{
BuildRefreshList();
Assignment assignment = base.Save();
_AllList.Add(assignment);//Refresh the item in AllList
RemoveFromDictionaries(); // if save is successful remove the previous Folder from the cache
AddToCache(assignment);//Refresh the item in AllList
ProcessRefreshList();
return assignment;
}