This commit is contained in:
Kathy Ruffing 2008-08-12 11:23:45 +00:00
parent f42f176ae9
commit 25458190e6
4 changed files with 191 additions and 119 deletions

View File

@ -52,28 +52,36 @@ namespace VEPROMS.CSLA.Library
} }
#endregion #endregion
#region Collection #region Collection
protected static List<Detail> _AllList = new List<Detail>(); private static List<Detail> _CacheList = new List<Detail>();
private static Dictionary<string, List<Detail>> _AllByPrimaryKey = new Dictionary<string, List<Detail>>(); protected static void AddToCache(Detail detail)
{
if (!_CacheList.Contains(detail)) _CacheList.Add(detail); // In AddToCache
}
protected static void RemoveFromCache(Detail detail)
{
while (_CacheList.Contains(detail)) _CacheList.Remove(detail); // In RemoveFromCache
}
private static Dictionary<string, List<Detail>> _CacheByPrimaryKey = new Dictionary<string, List<Detail>>();
private static void ConvertListToDictionary() private static void ConvertListToDictionary()
{ {
List<Detail> remove = new List<Detail>(); List<Detail> remove = new List<Detail>();
foreach (Detail tmp in _AllList) foreach (Detail tmp in _CacheList)
{ {
if (!_AllByPrimaryKey.ContainsKey(tmp.DetailID.ToString())) if (!_CacheByPrimaryKey.ContainsKey(tmp.DetailID.ToString()))
{ {
_AllByPrimaryKey[tmp.DetailID.ToString()] = new List<Detail>(); // Add new list for PrimaryKey _CacheByPrimaryKey[tmp.DetailID.ToString()] = new List<Detail>(); // Add new list for PrimaryKey
} }
_AllByPrimaryKey[tmp.DetailID.ToString()].Add(tmp); // Add to Primary Key list _CacheByPrimaryKey[tmp.DetailID.ToString()].Add(tmp); // Add to Primary Key list
remove.Add(tmp); remove.Add(tmp);
} }
foreach (Detail tmp in remove) foreach (Detail tmp in remove)
_AllList.Remove(tmp); RemoveFromCache(tmp);
} }
public static Detail GetExistingByPrimaryKey(int detailID) protected static Detail GetCachedByPrimaryKey(int detailID)
{ {
ConvertListToDictionary(); ConvertListToDictionary();
string key = detailID.ToString(); string key = detailID.ToString();
if (_AllByPrimaryKey.ContainsKey(key)) return _AllByPrimaryKey[key][0]; if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
return null; return null;
} }
#endregion #endregion
@ -394,24 +402,29 @@ namespace VEPROMS.CSLA.Library
public int CurrentEditLevel public int CurrentEditLevel
{ get { return EditLevel; } } { get { return EditLevel; } }
private static int _DetailUnique = 0; private static int _DetailUnique = 0;
private int _MyDetailUnique; protected static int DetailUnique
{ get { return ++_DetailUnique; } }
private int _MyDetailUnique = DetailUnique;
public int MyDetailUnique public int MyDetailUnique
{ { get { return _MyDetailUnique; } }
get { return _MyDetailUnique; }
}
protected Detail() protected Detail()
{/* require use of factory methods */ {/* require use of factory methods */
_MyDetailUnique = ++_DetailUnique; AddToCache(this);
_AllList.Add(this);
} }
public void Dispose() public void Dispose()
{ {
_AllList.Remove(this); RemoveFromDictionaries();
if (!_AllByPrimaryKey.ContainsKey(DetailID.ToString())) return; }
List<Detail> listDetail = _AllByPrimaryKey[DetailID.ToString()]; // Get the list of items private void RemoveFromDictionaries()
listDetail.Remove(this); // Remove the item from the list {
if (listDetail.Count == 0) //If there are no items left in the list RemoveFromCache(this);
_AllByPrimaryKey.Remove(DetailID.ToString()); // remove the list if (_CacheByPrimaryKey.ContainsKey(DetailID.ToString()))
{
List<Detail> listDetail = _CacheByPrimaryKey[DetailID.ToString()]; // Get the list of items
while (listDetail.Contains(this)) listDetail.Remove(this); // Remove the item from the list
if (listDetail.Count == 0) //If there are no items left in the list
_CacheByPrimaryKey.Remove(DetailID.ToString()); // remove the list
}
} }
public static Detail New() public static Detail New()
{ {
@ -449,7 +462,11 @@ namespace VEPROMS.CSLA.Library
{ {
Detail tmp = Detail.New(myContent, itemType, text, config, dts, userID); Detail tmp = Detail.New(myContent, itemType, text, config, dts, userID);
if (tmp.IsSavable) if (tmp.IsSavable)
tmp = tmp.Save(); {
Detail tmp2 = tmp;
tmp = tmp2.Save();
tmp2.Dispose();
}
else else
{ {
Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules(); Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules();
@ -474,7 +491,11 @@ namespace VEPROMS.CSLA.Library
{ {
Detail tmp = Detail.New(myContent, itemType, text, config); Detail tmp = Detail.New(myContent, itemType, text, config);
if (tmp.IsSavable) if (tmp.IsSavable)
tmp = tmp.Save(); {
Detail tmp2 = tmp;
tmp = tmp2.Save();
tmp2.Dispose();
}
else else
{ {
Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules(); Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules();
@ -492,13 +513,17 @@ namespace VEPROMS.CSLA.Library
throw new System.Security.SecurityException("User not authorized to view a Detail"); throw new System.Security.SecurityException("User not authorized to view a Detail");
try try
{ {
Detail tmp = GetExistingByPrimaryKey(detailID); Detail tmp = GetCachedByPrimaryKey(detailID);
if (tmp == null) if (tmp == null)
{ {
tmp = DataPortal.Fetch<Detail>(new PKCriteria(detailID)); tmp = DataPortal.Fetch<Detail>(new PKCriteria(detailID));
if (!_AllList.Contains(tmp)) _AllList.Add(tmp); AddToCache(tmp);
}
if (tmp.ErrorMessage == "No Record Found")
{
tmp.Dispose(); // Clean-up Detail
tmp = null;
} }
if (tmp.ErrorMessage == "No Record Found") tmp = null;
return tmp; return tmp;
} }
catch (Exception ex) catch (Exception ex)
@ -540,7 +565,8 @@ namespace VEPROMS.CSLA.Library
{ {
BuildRefreshList(); BuildRefreshList();
Detail detail = base.Save(); Detail detail = base.Save();
_AllList.Add(detail);//Refresh the item in AllList RemoveFromDictionaries(); // if save is successful remove the previous Folder from the cache
AddToCache(detail);//Refresh the item in AllList
ProcessRefreshList(); ProcessRefreshList();
return detail; return detail;
} }

View File

@ -36,32 +36,40 @@ namespace VEPROMS.CSLA.Library
private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#endregion #endregion
#region Collection #region Collection
protected static List<DetailInfo> _AllList = new List<DetailInfo>(); private static List<DetailInfo> _CacheList = new List<DetailInfo>();
private static Dictionary<string, List<DetailInfo>> _AllByPrimaryKey = new Dictionary<string, List<DetailInfo>>(); protected static void AddToCache(DetailInfo detailInfo)
{
if (!_CacheList.Contains(detailInfo)) _CacheList.Add(detailInfo); // In AddToCache
}
protected static void RemoveFromCache(DetailInfo detailInfo)
{
while (_CacheList.Contains(detailInfo)) _CacheList.Remove(detailInfo); // In RemoveFromCache
}
private static Dictionary<string, List<DetailInfo>> _CacheByPrimaryKey = new Dictionary<string, List<DetailInfo>>();
private static void ConvertListToDictionary() private static void ConvertListToDictionary()
{ {
List<DetailInfo> remove = new List<DetailInfo>(); List<DetailInfo> remove = new List<DetailInfo>();
foreach (DetailInfo tmp in _AllList) foreach (DetailInfo tmp in _CacheList)
{ {
if (!_AllByPrimaryKey.ContainsKey(tmp.DetailID.ToString())) if (!_CacheByPrimaryKey.ContainsKey(tmp.DetailID.ToString()))
{ {
_AllByPrimaryKey[tmp.DetailID.ToString()] = new List<DetailInfo>(); // Add new list for PrimaryKey _CacheByPrimaryKey[tmp.DetailID.ToString()] = new List<DetailInfo>(); // Add new list for PrimaryKey
} }
_AllByPrimaryKey[tmp.DetailID.ToString()].Add(tmp); // Add to Primary Key list _CacheByPrimaryKey[tmp.DetailID.ToString()].Add(tmp); // Add to Primary Key list
remove.Add(tmp); remove.Add(tmp);
} }
foreach (DetailInfo tmp in remove) foreach (DetailInfo tmp in remove)
_AllList.Remove(tmp); RemoveFromCache(tmp);
} }
internal static void AddList(DetailInfoList lst) internal static void AddList(DetailInfoList lst)
{ {
foreach (DetailInfo item in lst) _AllList.Add(item); foreach (DetailInfo item in lst) AddToCache(item);
} }
public static DetailInfo GetExistingByPrimaryKey(int detailID) protected static DetailInfo GetCachedByPrimaryKey(int detailID)
{ {
ConvertListToDictionary(); ConvertListToDictionary();
string key = detailID.ToString(); string key = detailID.ToString();
if (_AllByPrimaryKey.ContainsKey(key)) return _AllByPrimaryKey[key][0]; if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
return null; return null;
} }
#endregion #endregion
@ -186,24 +194,23 @@ namespace VEPROMS.CSLA.Library
#endregion #endregion
#region Factory Methods #region Factory Methods
private static int _DetailInfoUnique = 0; private static int _DetailInfoUnique = 0;
private int _MyDetailInfoUnique; private static int DetailInfoUnique
{ get { return ++_DetailInfoUnique; } }
private int _MyDetailInfoUnique = DetailInfoUnique;
public int MyDetailInfoUnique public int MyDetailInfoUnique
{ { get { return _MyDetailInfoUnique; } }
get { return _MyDetailInfoUnique; } protected DetailInfo()
}
private DetailInfo()
{/* require use of factory methods */ {/* require use of factory methods */
_MyDetailInfoUnique = ++_DetailInfoUnique; AddToCache(this);
_AllList.Add(this);
} }
public void Dispose() public void Dispose()
{ {
_AllList.Remove(this); RemoveFromCache(this);
if (!_AllByPrimaryKey.ContainsKey(DetailID.ToString())) return; if (!_CacheByPrimaryKey.ContainsKey(DetailID.ToString())) return;
List<DetailInfo> listDetailInfo = _AllByPrimaryKey[DetailID.ToString()]; // Get the list of items List<DetailInfo> listDetailInfo = _CacheByPrimaryKey[DetailID.ToString()]; // Get the list of items
listDetailInfo.Remove(this); // Remove the item from the list while (listDetailInfo.Contains(this)) listDetailInfo.Remove(this); // Remove the item from the list
if (listDetailInfo.Count == 0) // If there are no items left in the list if (listDetailInfo.Count == 0) // If there are no items left in the list
_AllByPrimaryKey.Remove(DetailID.ToString()); // remove the list _CacheByPrimaryKey.Remove(DetailID.ToString()); // remove the list
} }
public virtual Detail Get() public virtual Detail Get()
{ {
@ -213,15 +220,15 @@ namespace VEPROMS.CSLA.Library
{ {
string key = tmp.DetailID.ToString(); string key = tmp.DetailID.ToString();
ConvertListToDictionary(); ConvertListToDictionary();
if (_AllByPrimaryKey.ContainsKey(key)) if (_CacheByPrimaryKey.ContainsKey(key))
foreach (DetailInfo tmpInfo in _AllByPrimaryKey[key]) foreach (DetailInfo tmpInfo in _CacheByPrimaryKey[key])
tmpInfo.RefreshFields(tmp); tmpInfo.RefreshFields(tmp);
} }
private void RefreshFields(Detail tmp) protected virtual void RefreshFields(Detail tmp)
{ {
if (_ContentID != tmp.ContentID) if (_ContentID != tmp.ContentID)
{ {
MyContent.RefreshContentDetails(); // Update List for old value if (MyContent != null) MyContent.RefreshContentDetails(); // Update List for old value
_ContentID = tmp.ContentID; // Update the value _ContentID = tmp.ContentID; // Update the value
} }
_MyContent = null; // Reset list so that the next line gets a new list _MyContent = null; // Reset list so that the next line gets a new list
@ -239,11 +246,11 @@ namespace VEPROMS.CSLA.Library
{ {
string key = tmp.DetailID.ToString(); string key = tmp.DetailID.ToString();
ConvertListToDictionary(); ConvertListToDictionary();
if (_AllByPrimaryKey.ContainsKey(key)) if (_CacheByPrimaryKey.ContainsKey(key))
foreach (DetailInfo tmpInfo in _AllByPrimaryKey[key]) foreach (DetailInfo tmpInfo in _CacheByPrimaryKey[key])
tmpInfo.RefreshFields(tmp); tmpInfo.RefreshFields(tmp);
} }
private void RefreshFields(ContentDetail tmp) protected virtual void RefreshFields(ContentDetail tmp)
{ {
_ItemType = tmp.ItemType; _ItemType = tmp.ItemType;
_Text = tmp.Text; _Text = tmp.Text;
@ -260,13 +267,17 @@ namespace VEPROMS.CSLA.Library
// throw new System.Security.SecurityException("User not authorized to view a Detail"); // throw new System.Security.SecurityException("User not authorized to view a Detail");
try try
{ {
DetailInfo tmp = GetExistingByPrimaryKey(detailID); DetailInfo tmp = GetCachedByPrimaryKey(detailID);
if (tmp == null) if (tmp == null)
{ {
tmp = DataPortal.Fetch<DetailInfo>(new PKCriteria(detailID)); tmp = DataPortal.Fetch<DetailInfo>(new PKCriteria(detailID));
if (!_AllList.Contains(tmp)) _AllList.Add(tmp); AddToCache(tmp);
}
if (tmp.ErrorMessage == "No Record Found")
{
tmp.Dispose(); // Clean-up DetailInfo
tmp = null;
} }
if (tmp.ErrorMessage == "No Record Found") tmp = null;
return tmp; return tmp;
} }
catch (Exception ex) catch (Exception ex)
@ -278,7 +289,6 @@ namespace VEPROMS.CSLA.Library
#region Data Access Portal #region Data Access Portal
internal DetailInfo(SafeDataReader dr) internal DetailInfo(SafeDataReader dr)
{ {
_MyDetailInfoUnique = ++_DetailInfoUnique;
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] DetailInfo.Constructor", GetHashCode()); if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] DetailInfo.Constructor", GetHashCode());
try try
{ {

View File

@ -64,28 +64,36 @@ namespace VEPROMS.CSLA.Library
} }
#endregion #endregion
#region Collection #region Collection
protected static List<Document> _AllList = new List<Document>(); private static List<Document> _CacheList = new List<Document>();
private static Dictionary<string, List<Document>> _AllByPrimaryKey = new Dictionary<string, List<Document>>(); protected static void AddToCache(Document document)
{
if (!_CacheList.Contains(document)) _CacheList.Add(document); // In AddToCache
}
protected static void RemoveFromCache(Document document)
{
while (_CacheList.Contains(document)) _CacheList.Remove(document); // In RemoveFromCache
}
private static Dictionary<string, List<Document>> _CacheByPrimaryKey = new Dictionary<string, List<Document>>();
private static void ConvertListToDictionary() private static void ConvertListToDictionary()
{ {
List<Document> remove = new List<Document>(); List<Document> remove = new List<Document>();
foreach (Document tmp in _AllList) foreach (Document tmp in _CacheList)
{ {
if (!_AllByPrimaryKey.ContainsKey(tmp.DocID.ToString())) if (!_CacheByPrimaryKey.ContainsKey(tmp.DocID.ToString()))
{ {
_AllByPrimaryKey[tmp.DocID.ToString()] = new List<Document>(); // Add new list for PrimaryKey _CacheByPrimaryKey[tmp.DocID.ToString()] = new List<Document>(); // Add new list for PrimaryKey
} }
_AllByPrimaryKey[tmp.DocID.ToString()].Add(tmp); // Add to Primary Key list _CacheByPrimaryKey[tmp.DocID.ToString()].Add(tmp); // Add to Primary Key list
remove.Add(tmp); remove.Add(tmp);
} }
foreach (Document tmp in remove) foreach (Document tmp in remove)
_AllList.Remove(tmp); RemoveFromCache(tmp);
} }
public static Document GetExistingByPrimaryKey(int docID) protected static Document GetCachedByPrimaryKey(int docID)
{ {
ConvertListToDictionary(); ConvertListToDictionary();
string key = docID.ToString(); string key = docID.ToString();
if (_AllByPrimaryKey.ContainsKey(key)) return _AllByPrimaryKey[key][0]; if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
return null; return null;
} }
#endregion #endregion
@ -436,24 +444,29 @@ namespace VEPROMS.CSLA.Library
public int CurrentEditLevel public int CurrentEditLevel
{ get { return EditLevel; } } { get { return EditLevel; } }
private static int _DocumentUnique = 0; private static int _DocumentUnique = 0;
private int _MyDocumentUnique; protected static int DocumentUnique
{ get { return ++_DocumentUnique; } }
private int _MyDocumentUnique = DocumentUnique;
public int MyDocumentUnique public int MyDocumentUnique
{ { get { return _MyDocumentUnique; } }
get { return _MyDocumentUnique; }
}
protected Document() protected Document()
{/* require use of factory methods */ {/* require use of factory methods */
_MyDocumentUnique = ++_DocumentUnique; AddToCache(this);
_AllList.Add(this);
} }
public void Dispose() public void Dispose()
{ {
_AllList.Remove(this); RemoveFromDictionaries();
if (!_AllByPrimaryKey.ContainsKey(DocID.ToString())) return; }
List<Document> listDocument = _AllByPrimaryKey[DocID.ToString()]; // Get the list of items private void RemoveFromDictionaries()
listDocument.Remove(this); // Remove the item from the list {
if (listDocument.Count == 0) //If there are no items left in the list RemoveFromCache(this);
_AllByPrimaryKey.Remove(DocID.ToString()); // remove the list if (_CacheByPrimaryKey.ContainsKey(DocID.ToString()))
{
List<Document> listDocument = _CacheByPrimaryKey[DocID.ToString()]; // Get the list of items
while (listDocument.Contains(this)) listDocument.Remove(this); // Remove the item from the list
if (listDocument.Count == 0) //If there are no items left in the list
_CacheByPrimaryKey.Remove(DocID.ToString()); // remove the list
}
} }
public static Document New() public static Document New()
{ {
@ -483,7 +496,11 @@ namespace VEPROMS.CSLA.Library
{ {
Document tmp = Document.New(libTitle, docContent, docAscii, config, dts, userID); Document tmp = Document.New(libTitle, docContent, docAscii, config, dts, userID);
if (tmp.IsSavable) if (tmp.IsSavable)
tmp = tmp.Save(); {
Document tmp2 = tmp;
tmp = tmp2.Save();
tmp2.Dispose();
}
else else
{ {
Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules(); Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules();
@ -508,7 +525,11 @@ namespace VEPROMS.CSLA.Library
{ {
Document tmp = Document.New(libTitle, docContent, docAscii, config); Document tmp = Document.New(libTitle, docContent, docAscii, config);
if (tmp.IsSavable) if (tmp.IsSavable)
tmp = tmp.Save(); {
Document tmp2 = tmp;
tmp = tmp2.Save();
tmp2.Dispose();
}
else else
{ {
Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules(); Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules();
@ -526,13 +547,17 @@ namespace VEPROMS.CSLA.Library
throw new System.Security.SecurityException("User not authorized to view a Document"); throw new System.Security.SecurityException("User not authorized to view a Document");
try try
{ {
Document tmp = GetExistingByPrimaryKey(docID); Document tmp = GetCachedByPrimaryKey(docID);
if (tmp == null) if (tmp == null)
{ {
tmp = DataPortal.Fetch<Document>(new PKCriteria(docID)); tmp = DataPortal.Fetch<Document>(new PKCriteria(docID));
if (!_AllList.Contains(tmp)) _AllList.Add(tmp); AddToCache(tmp);
}
if (tmp.ErrorMessage == "No Record Found")
{
tmp.Dispose(); // Clean-up Document
tmp = null;
} }
if (tmp.ErrorMessage == "No Record Found") tmp = null;
return tmp; return tmp;
} }
catch (Exception ex) catch (Exception ex)
@ -574,7 +599,8 @@ namespace VEPROMS.CSLA.Library
{ {
BuildRefreshList(); BuildRefreshList();
Document document = base.Save(); Document document = base.Save();
_AllList.Add(document);//Refresh the item in AllList RemoveFromDictionaries(); // if save is successful remove the previous Folder from the cache
AddToCache(document);//Refresh the item in AllList
ProcessRefreshList(); ProcessRefreshList();
return document; return document;
} }

View File

@ -36,32 +36,40 @@ namespace VEPROMS.CSLA.Library
private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#endregion #endregion
#region Collection #region Collection
protected static List<DocumentInfo> _AllList = new List<DocumentInfo>(); private static List<DocumentInfo> _CacheList = new List<DocumentInfo>();
private static Dictionary<string, List<DocumentInfo>> _AllByPrimaryKey = new Dictionary<string, List<DocumentInfo>>(); protected static void AddToCache(DocumentInfo documentInfo)
{
if (!_CacheList.Contains(documentInfo)) _CacheList.Add(documentInfo); // In AddToCache
}
protected static void RemoveFromCache(DocumentInfo documentInfo)
{
while (_CacheList.Contains(documentInfo)) _CacheList.Remove(documentInfo); // In RemoveFromCache
}
private static Dictionary<string, List<DocumentInfo>> _CacheByPrimaryKey = new Dictionary<string, List<DocumentInfo>>();
private static void ConvertListToDictionary() private static void ConvertListToDictionary()
{ {
List<DocumentInfo> remove = new List<DocumentInfo>(); List<DocumentInfo> remove = new List<DocumentInfo>();
foreach (DocumentInfo tmp in _AllList) foreach (DocumentInfo tmp in _CacheList)
{ {
if (!_AllByPrimaryKey.ContainsKey(tmp.DocID.ToString())) if (!_CacheByPrimaryKey.ContainsKey(tmp.DocID.ToString()))
{ {
_AllByPrimaryKey[tmp.DocID.ToString()] = new List<DocumentInfo>(); // Add new list for PrimaryKey _CacheByPrimaryKey[tmp.DocID.ToString()] = new List<DocumentInfo>(); // Add new list for PrimaryKey
} }
_AllByPrimaryKey[tmp.DocID.ToString()].Add(tmp); // Add to Primary Key list _CacheByPrimaryKey[tmp.DocID.ToString()].Add(tmp); // Add to Primary Key list
remove.Add(tmp); remove.Add(tmp);
} }
foreach (DocumentInfo tmp in remove) foreach (DocumentInfo tmp in remove)
_AllList.Remove(tmp); RemoveFromCache(tmp);
} }
internal static void AddList(DocumentInfoList lst) internal static void AddList(DocumentInfoList lst)
{ {
foreach (DocumentInfo item in lst) _AllList.Add(item); foreach (DocumentInfo item in lst) AddToCache(item);
} }
public static DocumentInfo GetExistingByPrimaryKey(int docID) protected static DocumentInfo GetCachedByPrimaryKey(int docID)
{ {
ConvertListToDictionary(); ConvertListToDictionary();
string key = docID.ToString(); string key = docID.ToString();
if (_AllByPrimaryKey.ContainsKey(key)) return _AllByPrimaryKey[key][0]; if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
return null; return null;
} }
#endregion #endregion
@ -187,11 +195,11 @@ namespace VEPROMS.CSLA.Library
return _DocumentEntries; return _DocumentEntries;
} }
} }
internal void RefreshDocumentEntries() public void RefreshDocumentEntries()
{ {
ConvertListToDictionary(); ConvertListToDictionary();
if (_AllByPrimaryKey.ContainsKey(_DocID.ToString())) if (_CacheByPrimaryKey.ContainsKey(_DocID.ToString()))
foreach (DocumentInfo tmp in _AllByPrimaryKey[_DocID.ToString()]) foreach (DocumentInfo tmp in _CacheByPrimaryKey[_DocID.ToString()])
tmp._DocumentEntryCount = -1; // This will cause the data to be requeried tmp._DocumentEntryCount = -1; // This will cause the data to be requeried
} }
// TODO: Replace base DocumentInfo.ToString function as necessary // TODO: Replace base DocumentInfo.ToString function as necessary
@ -215,24 +223,23 @@ namespace VEPROMS.CSLA.Library
#endregion #endregion
#region Factory Methods #region Factory Methods
private static int _DocumentInfoUnique = 0; private static int _DocumentInfoUnique = 0;
private int _MyDocumentInfoUnique; private static int DocumentInfoUnique
{ get { return ++_DocumentInfoUnique; } }
private int _MyDocumentInfoUnique = DocumentInfoUnique;
public int MyDocumentInfoUnique public int MyDocumentInfoUnique
{ { get { return _MyDocumentInfoUnique; } }
get { return _MyDocumentInfoUnique; } protected DocumentInfo()
}
private DocumentInfo()
{/* require use of factory methods */ {/* require use of factory methods */
_MyDocumentInfoUnique = ++_DocumentInfoUnique; AddToCache(this);
_AllList.Add(this);
} }
public void Dispose() public void Dispose()
{ {
_AllList.Remove(this); RemoveFromCache(this);
if (!_AllByPrimaryKey.ContainsKey(DocID.ToString())) return; if (!_CacheByPrimaryKey.ContainsKey(DocID.ToString())) return;
List<DocumentInfo> listDocumentInfo = _AllByPrimaryKey[DocID.ToString()]; // Get the list of items List<DocumentInfo> listDocumentInfo = _CacheByPrimaryKey[DocID.ToString()]; // Get the list of items
listDocumentInfo.Remove(this); // Remove the item from the list while (listDocumentInfo.Contains(this)) listDocumentInfo.Remove(this); // Remove the item from the list
if (listDocumentInfo.Count == 0) // If there are no items left in the list if (listDocumentInfo.Count == 0) // If there are no items left in the list
_AllByPrimaryKey.Remove(DocID.ToString()); // remove the list _CacheByPrimaryKey.Remove(DocID.ToString()); // remove the list
} }
public virtual Document Get() public virtual Document Get()
{ {
@ -242,11 +249,11 @@ namespace VEPROMS.CSLA.Library
{ {
string key = tmp.DocID.ToString(); string key = tmp.DocID.ToString();
ConvertListToDictionary(); ConvertListToDictionary();
if (_AllByPrimaryKey.ContainsKey(key)) if (_CacheByPrimaryKey.ContainsKey(key))
foreach (DocumentInfo tmpInfo in _AllByPrimaryKey[key]) foreach (DocumentInfo tmpInfo in _CacheByPrimaryKey[key])
tmpInfo.RefreshFields(tmp); tmpInfo.RefreshFields(tmp);
} }
private void RefreshFields(Document tmp) protected virtual void RefreshFields(Document tmp)
{ {
_LibTitle = tmp.LibTitle; _LibTitle = tmp.LibTitle;
_DocContent = tmp.DocContent; _DocContent = tmp.DocContent;
@ -263,13 +270,17 @@ namespace VEPROMS.CSLA.Library
// throw new System.Security.SecurityException("User not authorized to view a Document"); // throw new System.Security.SecurityException("User not authorized to view a Document");
try try
{ {
DocumentInfo tmp = GetExistingByPrimaryKey(docID); DocumentInfo tmp = GetCachedByPrimaryKey(docID);
if (tmp == null) if (tmp == null)
{ {
tmp = DataPortal.Fetch<DocumentInfo>(new PKCriteria(docID)); tmp = DataPortal.Fetch<DocumentInfo>(new PKCriteria(docID));
if (!_AllList.Contains(tmp)) _AllList.Add(tmp); AddToCache(tmp);
}
if (tmp.ErrorMessage == "No Record Found")
{
tmp.Dispose(); // Clean-up DocumentInfo
tmp = null;
} }
if (tmp.ErrorMessage == "No Record Found") tmp = null;
return tmp; return tmp;
} }
catch (Exception ex) catch (Exception ex)
@ -281,7 +292,6 @@ namespace VEPROMS.CSLA.Library
#region Data Access Portal #region Data Access Portal
internal DocumentInfo(SafeDataReader dr) internal DocumentInfo(SafeDataReader dr)
{ {
_MyDocumentInfoUnique = ++_DocumentInfoUnique;
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] DocumentInfo.Constructor", GetHashCode()); if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] DocumentInfo.Constructor", GetHashCode());
try try
{ {