This commit is contained in:
2008-08-12 11:25:17 +00:00
parent 25458190e6
commit 50e163dae0
8 changed files with 493 additions and 279 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<EntryInfo> _AllList = new List<EntryInfo>();
private static Dictionary<string, List<EntryInfo>> _AllByPrimaryKey = new Dictionary<string, List<EntryInfo>>();
private static List<EntryInfo> _CacheList = new List<EntryInfo>();
protected static void AddToCache(EntryInfo entryInfo)
{
if (!_CacheList.Contains(entryInfo)) _CacheList.Add(entryInfo); // In AddToCache
}
protected static void RemoveFromCache(EntryInfo entryInfo)
{
while (_CacheList.Contains(entryInfo)) _CacheList.Remove(entryInfo); // In RemoveFromCache
}
private static Dictionary<string, List<EntryInfo>> _CacheByPrimaryKey = new Dictionary<string, List<EntryInfo>>();
private static void ConvertListToDictionary()
{
List<EntryInfo> remove = new List<EntryInfo>();
foreach (EntryInfo tmp in _AllList)
foreach (EntryInfo tmp in _CacheList)
{
if (!_AllByPrimaryKey.ContainsKey(tmp.ContentID.ToString()))
if (!_CacheByPrimaryKey.ContainsKey(tmp.ContentID.ToString()))
{
_AllByPrimaryKey[tmp.ContentID.ToString()] = new List<EntryInfo>(); // Add new list for PrimaryKey
_CacheByPrimaryKey[tmp.ContentID.ToString()] = new List<EntryInfo>(); // Add new list for PrimaryKey
}
_AllByPrimaryKey[tmp.ContentID.ToString()].Add(tmp); // Add to Primary Key list
_CacheByPrimaryKey[tmp.ContentID.ToString()].Add(tmp); // Add to Primary Key list
remove.Add(tmp);
}
foreach (EntryInfo tmp in remove)
_AllList.Remove(tmp);
RemoveFromCache(tmp);
}
internal static void AddList(EntryInfoList lst)
{
foreach (EntryInfo item in lst) _AllList.Add(item);
foreach (EntryInfo item in lst) AddToCache(item);
}
public static EntryInfo GetExistingByPrimaryKey(int contentID)
protected static EntryInfo GetCachedByPrimaryKey(int contentID)
{
ConvertListToDictionary();
string key = contentID.ToString();
if (_AllByPrimaryKey.ContainsKey(key)) return _AllByPrimaryKey[key][0];
if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
return null;
}
#endregion
@@ -169,24 +177,23 @@ namespace VEPROMS.CSLA.Library
#endregion
#region Factory Methods
private static int _EntryInfoUnique = 0;
private int _MyEntryInfoUnique;
private static int EntryInfoUnique
{ get { return ++_EntryInfoUnique; } }
private int _MyEntryInfoUnique = EntryInfoUnique;
public int MyEntryInfoUnique
{
get { return _MyEntryInfoUnique; }
}
private EntryInfo()
{ get { return _MyEntryInfoUnique; } }
protected EntryInfo()
{/* require use of factory methods */
_MyEntryInfoUnique = ++_EntryInfoUnique;
_AllList.Add(this);
AddToCache(this);
}
public void Dispose()
{
_AllList.Remove(this);
if (!_AllByPrimaryKey.ContainsKey(ContentID.ToString())) return;
List<EntryInfo> listEntryInfo = _AllByPrimaryKey[ContentID.ToString()]; // Get the list of items
listEntryInfo.Remove(this); // Remove the item from the list
RemoveFromCache(this);
if (!_CacheByPrimaryKey.ContainsKey(ContentID.ToString())) return;
List<EntryInfo> listEntryInfo = _CacheByPrimaryKey[ContentID.ToString()]; // Get the list of items
while (listEntryInfo.Contains(this)) listEntryInfo.Remove(this); // Remove the item from the list
if (listEntryInfo.Count == 0) // If there are no items left in the list
_AllByPrimaryKey.Remove(ContentID.ToString()); // remove the list
_CacheByPrimaryKey.Remove(ContentID.ToString()); // remove the list
}
public virtual Entry Get()
{
@@ -196,15 +203,15 @@ namespace VEPROMS.CSLA.Library
{
string key = tmp.ContentID.ToString();
ConvertListToDictionary();
if (_AllByPrimaryKey.ContainsKey(key))
foreach (EntryInfo tmpInfo in _AllByPrimaryKey[key])
if (_CacheByPrimaryKey.ContainsKey(key))
foreach (EntryInfo tmpInfo in _CacheByPrimaryKey[key])
tmpInfo.RefreshFields(tmp);
}
private void RefreshFields(Entry tmp)
protected virtual void RefreshFields(Entry tmp)
{
if (_DocID != tmp.DocID)
{
MyDocument.RefreshDocumentEntries(); // Update List for old value
if (MyDocument != null) MyDocument.RefreshDocumentEntries(); // Update List for old value
_DocID = tmp.DocID; // Update the value
}
_MyDocument = null; // Reset list so that the next line gets a new list
@@ -220,11 +227,11 @@ namespace VEPROMS.CSLA.Library
{
string key = tmp.ContentID.ToString();
ConvertListToDictionary();
if (_AllByPrimaryKey.ContainsKey(key))
foreach (EntryInfo tmpInfo in _AllByPrimaryKey[key])
if (_CacheByPrimaryKey.ContainsKey(key))
foreach (EntryInfo tmpInfo in _CacheByPrimaryKey[key])
tmpInfo.RefreshFields(tmp);
}
private void RefreshFields(DocumentEntry tmp)
protected virtual void RefreshFields(DocumentEntry tmp)
{
_DTS = tmp.DTS;
_UserID = tmp.UserID;
@@ -239,15 +246,16 @@ namespace VEPROMS.CSLA.Library
// throw new System.Security.SecurityException("User not authorized to view a Entry");
try
{
EntryInfo tmp = GetExistingByPrimaryKey(contentID);
EntryInfo tmp = GetCachedByPrimaryKey(contentID);
if (tmp == null)
{
tmp = DataPortal.Fetch<EntryInfo>(new PKCriteria(contentID));
if (!_AllList.Contains(tmp)) _AllList.Add(tmp);
AddToCache(tmp);
}
if (tmp.ErrorMessage == "No Record Found")
{
tmp._ContentID = contentID;
tmp.Dispose(); // Clean-up EntryInfo
tmp = null;
}
return tmp;
}
@@ -260,7 +268,6 @@ namespace VEPROMS.CSLA.Library
#region Data Access Portal
internal EntryInfo(SafeDataReader dr)
{
_MyEntryInfoUnique = ++_EntryInfoUnique;
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] EntryInfo.Constructor", GetHashCode());
try
{