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

@@ -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<GroupInfo> _AllList = new List<GroupInfo>();
private static Dictionary<string, List<GroupInfo>> _AllByPrimaryKey = new Dictionary<string, List<GroupInfo>>();
private static List<GroupInfo> _CacheList = new List<GroupInfo>();
protected static void AddToCache(GroupInfo groupInfo)
{
if (!_CacheList.Contains(groupInfo)) _CacheList.Add(groupInfo); // In AddToCache
}
protected static void RemoveFromCache(GroupInfo groupInfo)
{
while (_CacheList.Contains(groupInfo)) _CacheList.Remove(groupInfo); // In RemoveFromCache
}
private static Dictionary<string, List<GroupInfo>> _CacheByPrimaryKey = new Dictionary<string, List<GroupInfo>>();
private static void ConvertListToDictionary()
{
List<GroupInfo> remove = new List<GroupInfo>();
foreach (GroupInfo tmp in _AllList)
foreach (GroupInfo tmp in _CacheList)
{
if (!_AllByPrimaryKey.ContainsKey(tmp.GID.ToString()))
if (!_CacheByPrimaryKey.ContainsKey(tmp.GID.ToString()))
{
_AllByPrimaryKey[tmp.GID.ToString()] = new List<GroupInfo>(); // Add new list for PrimaryKey
_CacheByPrimaryKey[tmp.GID.ToString()] = new List<GroupInfo>(); // Add new list for PrimaryKey
}
_AllByPrimaryKey[tmp.GID.ToString()].Add(tmp); // Add to Primary Key list
_CacheByPrimaryKey[tmp.GID.ToString()].Add(tmp); // Add to Primary Key list
remove.Add(tmp);
}
foreach (GroupInfo tmp in remove)
_AllList.Remove(tmp);
RemoveFromCache(tmp);
}
internal static void AddList(GroupInfoList lst)
{
foreach (GroupInfo item in lst) _AllList.Add(item);
foreach (GroupInfo item in lst) AddToCache(item);
}
public static GroupInfo GetExistingByPrimaryKey(int gid)
protected static GroupInfo GetCachedByPrimaryKey(int gid)
{
ConvertListToDictionary();
string key = gid.ToString();
if (_AllByPrimaryKey.ContainsKey(key)) return _AllByPrimaryKey[key][0];
if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
return null;
}
#endregion
@@ -171,11 +179,11 @@ namespace VEPROMS.CSLA.Library
return _GroupAssignments;
}
}
internal void RefreshGroupAssignments()
public void RefreshGroupAssignments()
{
ConvertListToDictionary();
if (_AllByPrimaryKey.ContainsKey(_GID.ToString()))
foreach (GroupInfo tmp in _AllByPrimaryKey[_GID.ToString()])
if (_CacheByPrimaryKey.ContainsKey(_GID.ToString()))
foreach (GroupInfo tmp in _CacheByPrimaryKey[_GID.ToString()])
tmp._GroupAssignmentCount = -1; // This will cause the data to be requeried
}
private int _GroupMembershipCount = 0;
@@ -206,11 +214,11 @@ namespace VEPROMS.CSLA.Library
return _GroupMemberships;
}
}
internal void RefreshGroupMemberships()
public void RefreshGroupMemberships()
{
ConvertListToDictionary();
if (_AllByPrimaryKey.ContainsKey(_GID.ToString()))
foreach (GroupInfo tmp in _AllByPrimaryKey[_GID.ToString()])
if (_CacheByPrimaryKey.ContainsKey(_GID.ToString()))
foreach (GroupInfo tmp in _CacheByPrimaryKey[_GID.ToString()])
tmp._GroupMembershipCount = -1; // This will cause the data to be requeried
}
// TODO: Replace base GroupInfo.ToString function as necessary
@@ -234,24 +242,23 @@ namespace VEPROMS.CSLA.Library
#endregion
#region Factory Methods
private static int _GroupInfoUnique = 0;
private int _MyGroupInfoUnique;
private static int GroupInfoUnique
{ get { return ++_GroupInfoUnique; } }
private int _MyGroupInfoUnique = GroupInfoUnique;
public int MyGroupInfoUnique
{
get { return _MyGroupInfoUnique; }
}
private GroupInfo()
{ get { return _MyGroupInfoUnique; } }
protected GroupInfo()
{/* require use of factory methods */
_MyGroupInfoUnique = ++_GroupInfoUnique;
_AllList.Add(this);
AddToCache(this);
}
public void Dispose()
{
_AllList.Remove(this);
if (!_AllByPrimaryKey.ContainsKey(GID.ToString())) return;
List<GroupInfo> listGroupInfo = _AllByPrimaryKey[GID.ToString()]; // Get the list of items
listGroupInfo.Remove(this); // Remove the item from the list
RemoveFromCache(this);
if (!_CacheByPrimaryKey.ContainsKey(GID.ToString())) return;
List<GroupInfo> listGroupInfo = _CacheByPrimaryKey[GID.ToString()]; // Get the list of items
while (listGroupInfo.Contains(this)) listGroupInfo.Remove(this); // Remove the item from the list
if (listGroupInfo.Count == 0) // If there are no items left in the list
_AllByPrimaryKey.Remove(GID.ToString()); // remove the list
_CacheByPrimaryKey.Remove(GID.ToString()); // remove the list
}
public virtual Group Get()
{
@@ -261,11 +268,11 @@ namespace VEPROMS.CSLA.Library
{
string key = tmp.GID.ToString();
ConvertListToDictionary();
if (_AllByPrimaryKey.ContainsKey(key))
foreach (GroupInfo tmpInfo in _AllByPrimaryKey[key])
if (_CacheByPrimaryKey.ContainsKey(key))
foreach (GroupInfo tmpInfo in _CacheByPrimaryKey[key])
tmpInfo.RefreshFields(tmp);
}
private void RefreshFields(Group tmp)
protected virtual void RefreshFields(Group tmp)
{
_GroupName = tmp.GroupName;
_GroupType = tmp.GroupType;
@@ -281,13 +288,17 @@ namespace VEPROMS.CSLA.Library
// throw new System.Security.SecurityException("User not authorized to view a Group");
try
{
GroupInfo tmp = GetExistingByPrimaryKey(gid);
GroupInfo tmp = GetCachedByPrimaryKey(gid);
if (tmp == null)
{
tmp = DataPortal.Fetch<GroupInfo>(new PKCriteria(gid));
if (!_AllList.Contains(tmp)) _AllList.Add(tmp);
AddToCache(tmp);
}
if (tmp.ErrorMessage == "No Record Found")
{
tmp.Dispose(); // Clean-up GroupInfo
tmp = null;
}
if (tmp.ErrorMessage == "No Record Found") tmp = null;
return tmp;
}
catch (Exception ex)
@@ -299,7 +310,6 @@ namespace VEPROMS.CSLA.Library
#region Data Access Portal
internal GroupInfo(SafeDataReader dr)
{
_MyGroupInfoUnique = ++_GroupInfoUnique;
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] GroupInfo.Constructor", GetHashCode());
try
{