This commit is contained in:
2008-08-12 11:42:50 +00:00
parent dfafac5a3a
commit d81b4d8af0
19 changed files with 941 additions and 569 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<PartInfo> _AllList = new List<PartInfo>();
private static Dictionary<string, List<PartInfo>> _AllByPrimaryKey = new Dictionary<string, List<PartInfo>>();
private static List<PartInfo> _CacheList = new List<PartInfo>();
protected static void AddToCache(PartInfo partInfo)
{
if (!_CacheList.Contains(partInfo)) _CacheList.Add(partInfo); // In AddToCache
}
protected static void RemoveFromCache(PartInfo partInfo)
{
while (_CacheList.Contains(partInfo)) _CacheList.Remove(partInfo); // In RemoveFromCache
}
private static Dictionary<string, List<PartInfo>> _CacheByPrimaryKey = new Dictionary<string, List<PartInfo>>();
private static void ConvertListToDictionary()
{
List<PartInfo> remove = new List<PartInfo>();
foreach (PartInfo tmp in _AllList)
foreach (PartInfo tmp in _CacheList)
{
if (!_AllByPrimaryKey.ContainsKey(tmp.ContentID.ToString() + "_" + tmp.FromType.ToString()))
if (!_CacheByPrimaryKey.ContainsKey(tmp.ContentID.ToString() + "_" + tmp.FromType.ToString()))
{
_AllByPrimaryKey[tmp.ContentID.ToString() + "_" + tmp.FromType.ToString()] = new List<PartInfo>(); // Add new list for PrimaryKey
_CacheByPrimaryKey[tmp.ContentID.ToString() + "_" + tmp.FromType.ToString()] = new List<PartInfo>(); // Add new list for PrimaryKey
}
_AllByPrimaryKey[tmp.ContentID.ToString() + "_" + tmp.FromType.ToString()].Add(tmp); // Add to Primary Key list
_CacheByPrimaryKey[tmp.ContentID.ToString() + "_" + tmp.FromType.ToString()].Add(tmp); // Add to Primary Key list
remove.Add(tmp);
}
foreach (PartInfo tmp in remove)
_AllList.Remove(tmp);
RemoveFromCache(tmp);
}
internal static void AddList(PartInfoList lst)
{
foreach (PartInfo item in lst) _AllList.Add(item);
foreach (PartInfo item in lst) AddToCache(item);
}
public static PartInfo GetExistingByPrimaryKey(int contentID, int fromType)
protected static PartInfo GetCachedByPrimaryKey(int contentID, int fromType)
{
ConvertListToDictionary();
string key = contentID.ToString() + "_" + fromType.ToString();
if (_AllByPrimaryKey.ContainsKey(key)) return _AllByPrimaryKey[key][0];
if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
return null;
}
#endregion
@@ -180,24 +188,23 @@ namespace VEPROMS.CSLA.Library
#endregion
#region Factory Methods
private static int _PartInfoUnique = 0;
private int _MyPartInfoUnique;
private static int PartInfoUnique
{ get { return ++_PartInfoUnique; } }
private int _MyPartInfoUnique = PartInfoUnique;
public int MyPartInfoUnique
{
get { return _MyPartInfoUnique; }
}
private PartInfo()
{ get { return _MyPartInfoUnique; } }
protected PartInfo()
{/* require use of factory methods */
_MyPartInfoUnique = ++_PartInfoUnique;
_AllList.Add(this);
AddToCache(this);
}
public void Dispose()
{
_AllList.Remove(this);
if (!_AllByPrimaryKey.ContainsKey(ContentID.ToString() + "_" + FromType.ToString())) return;
List<PartInfo> listPartInfo = _AllByPrimaryKey[ContentID.ToString() + "_" + FromType.ToString()]; // Get the list of items
listPartInfo.Remove(this); // Remove the item from the list
RemoveFromCache(this);
if (!_CacheByPrimaryKey.ContainsKey(ContentID.ToString() + "_" + FromType.ToString())) return;
List<PartInfo> listPartInfo = _CacheByPrimaryKey[ContentID.ToString() + "_" + FromType.ToString()]; // Get the list of items
while (listPartInfo.Contains(this)) listPartInfo.Remove(this); // Remove the item from the list
if (listPartInfo.Count == 0) // If there are no items left in the list
_AllByPrimaryKey.Remove(ContentID.ToString() + "_" + FromType.ToString()); // remove the list
_CacheByPrimaryKey.Remove(ContentID.ToString() + "_" + FromType.ToString()); // remove the list
}
public virtual Part Get()
{
@@ -207,15 +214,15 @@ namespace VEPROMS.CSLA.Library
{
string key = tmp.ContentID.ToString() + "_" + tmp.FromType.ToString();
ConvertListToDictionary();
if (_AllByPrimaryKey.ContainsKey(key))
foreach (PartInfo tmpInfo in _AllByPrimaryKey[key])
if (_CacheByPrimaryKey.ContainsKey(key))
foreach (PartInfo tmpInfo in _CacheByPrimaryKey[key])
tmpInfo.RefreshFields(tmp);
}
private void RefreshFields(Part tmp)
protected virtual void RefreshFields(Part tmp)
{
if (_ItemID != tmp.ItemID)
{
MyItem.RefreshItemParts(); // Update List for old value
if (MyItem != null) MyItem.RefreshItemParts(); // Update List for old value
_ItemID = tmp.ItemID; // Update the value
}
_MyItem = null; // Reset list so that the next line gets a new list
@@ -231,15 +238,15 @@ namespace VEPROMS.CSLA.Library
{
string key = myContent.ContentID.ToString() + "_" + tmp.FromType.ToString();
ConvertListToDictionary();
if (_AllByPrimaryKey.ContainsKey(key))
foreach (PartInfo tmpInfo in _AllByPrimaryKey[key])
if (_CacheByPrimaryKey.ContainsKey(key))
foreach (PartInfo tmpInfo in _CacheByPrimaryKey[key])
tmpInfo.RefreshFields(tmp);
}
private void RefreshFields(ContentPart tmp)
protected virtual void RefreshFields(ContentPart tmp)
{
if (_ItemID != tmp.ItemID)
{
MyItem.RefreshItemParts(); // Update List for old value
if (MyItem != null) MyItem.RefreshItemParts(); // Update List for old value
_ItemID = tmp.ItemID; // Update the value
}
_MyItem = null; // Reset list so that the next line gets a new list
@@ -255,11 +262,11 @@ namespace VEPROMS.CSLA.Library
{
string key = tmp.ContentID.ToString() + "_" + tmp.FromType.ToString();
ConvertListToDictionary();
if (_AllByPrimaryKey.ContainsKey(key))
foreach (PartInfo tmpInfo in _AllByPrimaryKey[key])
if (_CacheByPrimaryKey.ContainsKey(key))
foreach (PartInfo tmpInfo in _CacheByPrimaryKey[key])
tmpInfo.RefreshFields(tmp);
}
private void RefreshFields(ItemPart tmp)
protected virtual void RefreshFields(ItemPart tmp)
{
_DTS = tmp.DTS;
_UserID = tmp.UserID;
@@ -274,13 +281,17 @@ namespace VEPROMS.CSLA.Library
// throw new System.Security.SecurityException("User not authorized to view a Part");
try
{
PartInfo tmp = GetExistingByPrimaryKey(contentID, fromType);
PartInfo tmp = GetCachedByPrimaryKey(contentID, fromType);
if (tmp == null)
{
tmp = DataPortal.Fetch<PartInfo>(new PKCriteria(contentID, fromType));
if (!_AllList.Contains(tmp)) _AllList.Add(tmp);
AddToCache(tmp);
}
if (tmp.ErrorMessage == "No Record Found")
{
tmp.Dispose(); // Clean-up PartInfo
tmp = null;
}
if (tmp.ErrorMessage == "No Record Found") tmp = null;
return tmp;
}
catch (Exception ex)
@@ -292,7 +303,6 @@ namespace VEPROMS.CSLA.Library
#region Data Access Portal
internal PartInfo(SafeDataReader dr)
{
_MyPartInfoUnique = ++_PartInfoUnique;
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] PartInfo.Constructor", GetHashCode());
try
{