This commit is contained in:
2008-08-12 11:27:04 +00:00
parent 50e163dae0
commit dfafac5a3a
9 changed files with 352 additions and 202 deletions

View File

@@ -124,28 +124,36 @@ namespace VEPROMS.CSLA.Library
}
#endregion
#region Collection
protected static List<Item> _AllList = new List<Item>();
private static Dictionary<string, List<Item>> _AllByPrimaryKey = new Dictionary<string, List<Item>>();
private static List<Item> _CacheList = new List<Item>();
protected static void AddToCache(Item item)
{
if (!_CacheList.Contains(item)) _CacheList.Add(item); // In AddToCache
}
protected static void RemoveFromCache(Item item)
{
while (_CacheList.Contains(item)) _CacheList.Remove(item); // In RemoveFromCache
}
private static Dictionary<string, List<Item>> _CacheByPrimaryKey = new Dictionary<string, List<Item>>();
private static void ConvertListToDictionary()
{
List<Item> remove = new List<Item>();
foreach (Item tmp in _AllList)
foreach (Item tmp in _CacheList)
{
if (!_AllByPrimaryKey.ContainsKey(tmp.ItemID.ToString()))
if (!_CacheByPrimaryKey.ContainsKey(tmp.ItemID.ToString()))
{
_AllByPrimaryKey[tmp.ItemID.ToString()] = new List<Item>(); // Add new list for PrimaryKey
_CacheByPrimaryKey[tmp.ItemID.ToString()] = new List<Item>(); // Add new list for PrimaryKey
}
_AllByPrimaryKey[tmp.ItemID.ToString()].Add(tmp); // Add to Primary Key list
_CacheByPrimaryKey[tmp.ItemID.ToString()].Add(tmp); // Add to Primary Key list
remove.Add(tmp);
}
foreach (Item tmp in remove)
_AllList.Remove(tmp);
RemoveFromCache(tmp);
}
public static Item GetExistingByPrimaryKey(int itemID)
protected static Item GetCachedByPrimaryKey(int itemID)
{
ConvertListToDictionary();
string key = itemID.ToString();
if (_AllByPrimaryKey.ContainsKey(key)) return _AllByPrimaryKey[key][0];
if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
return null;
}
#endregion
@@ -196,7 +204,7 @@ namespace VEPROMS.CSLA.Library
set
{
CanWriteProperty("MyPrevious", true);
if (_MyPrevious != value)
if ((_MyPrevious == null ? _PreviousID : (int?)_MyPrevious.ItemID) != (value == null ? null : (int?)value.ItemID))
{
_MyPrevious = value;
_PreviousID = (value == null ? null : (int?)value.ItemID);
@@ -635,24 +643,29 @@ namespace VEPROMS.CSLA.Library
public int CurrentEditLevel
{ get { return EditLevel; } }
private static int _ItemUnique = 0;
private int _MyItemUnique;
protected static int ItemUnique
{ get { return ++_ItemUnique; } }
private int _MyItemUnique = ItemUnique;
public int MyItemUnique
{
get { return _MyItemUnique; }
}
{ get { return _MyItemUnique; } }
protected Item()
{/* require use of factory methods */
_MyItemUnique = ++_ItemUnique;
_AllList.Add(this);
AddToCache(this);
}
public void Dispose()
{
_AllList.Remove(this);
if (!_AllByPrimaryKey.ContainsKey(ItemID.ToString())) return;
List<Item> listItem = _AllByPrimaryKey[ItemID.ToString()]; // Get the list of items
listItem.Remove(this); // Remove the item from the list
if (listItem.Count == 0) //If there are no items left in the list
_AllByPrimaryKey.Remove(ItemID.ToString()); // remove the list
RemoveFromDictionaries();
}
private void RemoveFromDictionaries()
{
RemoveFromCache(this);
if (_CacheByPrimaryKey.ContainsKey(ItemID.ToString()))
{
List<Item> listItem = _CacheByPrimaryKey[ItemID.ToString()]; // Get the list of items
while (listItem.Contains(this)) listItem.Remove(this); // Remove the item from the list
if (listItem.Count == 0) //If there are no items left in the list
_CacheByPrimaryKey.Remove(ItemID.ToString()); // remove the list
}
}
public static Item New()
{
@@ -686,7 +699,11 @@ namespace VEPROMS.CSLA.Library
{
Item tmp = Item.New(myPrevious, myContent, dts, userID);
if (tmp.IsSavable)
tmp = tmp.Save();
{
Item tmp2 = tmp;
tmp = tmp2.Save();
tmp2.Dispose();
}
else
{
Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules();
@@ -709,7 +726,11 @@ namespace VEPROMS.CSLA.Library
{
Item tmp = Item.New(myPrevious, myContent);
if (tmp.IsSavable)
tmp = tmp.Save();
{
Item tmp2 = tmp;
tmp = tmp2.Save();
tmp2.Dispose();
}
else
{
Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules();
@@ -727,13 +748,17 @@ namespace VEPROMS.CSLA.Library
throw new System.Security.SecurityException("User not authorized to view a Item");
try
{
Item tmp = GetExistingByPrimaryKey(itemID);
Item tmp = GetCachedByPrimaryKey(itemID);
if (tmp == null)
{
tmp = DataPortal.Fetch<Item>(new PKCriteria(itemID));
if (!_AllList.Contains(tmp)) _AllList.Add(tmp);
AddToCache(tmp);
}
if (tmp.ErrorMessage == "No Record Found")
{
tmp.Dispose(); // Clean-up Item
tmp = null;
}
if (tmp.ErrorMessage == "No Record Found") tmp = null;
return tmp;
}
catch (Exception ex)
@@ -785,7 +810,8 @@ namespace VEPROMS.CSLA.Library
{
BuildRefreshList();
Item item = base.Save();
_AllList.Add(item);//Refresh the item in AllList
RemoveFromDictionaries(); // if save is successful remove the previous Folder from the cache
AddToCache(item);//Refresh the item in AllList
ProcessRefreshList();
return item;
}