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<MembershipInfo> _AllList = new List<MembershipInfo>();
private static Dictionary<string, List<MembershipInfo>> _AllByPrimaryKey = new Dictionary<string, List<MembershipInfo>>();
private static List<MembershipInfo> _CacheList = new List<MembershipInfo>();
protected static void AddToCache(MembershipInfo membershipInfo)
{
if (!_CacheList.Contains(membershipInfo)) _CacheList.Add(membershipInfo); // In AddToCache
}
protected static void RemoveFromCache(MembershipInfo membershipInfo)
{
while (_CacheList.Contains(membershipInfo)) _CacheList.Remove(membershipInfo); // In RemoveFromCache
}
private static Dictionary<string, List<MembershipInfo>> _CacheByPrimaryKey = new Dictionary<string, List<MembershipInfo>>();
private static void ConvertListToDictionary()
{
List<MembershipInfo> remove = new List<MembershipInfo>();
foreach (MembershipInfo tmp in _AllList)
foreach (MembershipInfo tmp in _CacheList)
{
if (!_AllByPrimaryKey.ContainsKey(tmp.UGID.ToString()))
if (!_CacheByPrimaryKey.ContainsKey(tmp.UGID.ToString()))
{
_AllByPrimaryKey[tmp.UGID.ToString()] = new List<MembershipInfo>(); // Add new list for PrimaryKey
_CacheByPrimaryKey[tmp.UGID.ToString()] = new List<MembershipInfo>(); // Add new list for PrimaryKey
}
_AllByPrimaryKey[tmp.UGID.ToString()].Add(tmp); // Add to Primary Key list
_CacheByPrimaryKey[tmp.UGID.ToString()].Add(tmp); // Add to Primary Key list
remove.Add(tmp);
}
foreach (MembershipInfo tmp in remove)
_AllList.Remove(tmp);
RemoveFromCache(tmp);
}
internal static void AddList(MembershipInfoList lst)
{
foreach (MembershipInfo item in lst) _AllList.Add(item);
foreach (MembershipInfo item in lst) AddToCache(item);
}
public static MembershipInfo GetExistingByPrimaryKey(int ugid)
protected static MembershipInfo GetCachedByPrimaryKey(int ugid)
{
ConvertListToDictionary();
string key = ugid.ToString();
if (_AllByPrimaryKey.ContainsKey(key)) return _AllByPrimaryKey[key][0];
if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
return null;
}
#endregion
@@ -208,24 +216,23 @@ namespace VEPROMS.CSLA.Library
#endregion
#region Factory Methods
private static int _MembershipInfoUnique = 0;
private int _MyMembershipInfoUnique;
private static int MembershipInfoUnique
{ get { return ++_MembershipInfoUnique; } }
private int _MyMembershipInfoUnique = MembershipInfoUnique;
public int MyMembershipInfoUnique
{
get { return _MyMembershipInfoUnique; }
}
private MembershipInfo()
{ get { return _MyMembershipInfoUnique; } }
protected MembershipInfo()
{/* require use of factory methods */
_MyMembershipInfoUnique = ++_MembershipInfoUnique;
_AllList.Add(this);
AddToCache(this);
}
public void Dispose()
{
_AllList.Remove(this);
if (!_AllByPrimaryKey.ContainsKey(UGID.ToString())) return;
List<MembershipInfo> listMembershipInfo = _AllByPrimaryKey[UGID.ToString()]; // Get the list of items
listMembershipInfo.Remove(this); // Remove the item from the list
RemoveFromCache(this);
if (!_CacheByPrimaryKey.ContainsKey(UGID.ToString())) return;
List<MembershipInfo> listMembershipInfo = _CacheByPrimaryKey[UGID.ToString()]; // Get the list of items
while (listMembershipInfo.Contains(this)) listMembershipInfo.Remove(this); // Remove the item from the list
if (listMembershipInfo.Count == 0) // If there are no items left in the list
_AllByPrimaryKey.Remove(UGID.ToString()); // remove the list
_CacheByPrimaryKey.Remove(UGID.ToString()); // remove the list
}
public virtual Membership Get()
{
@@ -235,22 +242,22 @@ namespace VEPROMS.CSLA.Library
{
string key = tmp.UGID.ToString();
ConvertListToDictionary();
if (_AllByPrimaryKey.ContainsKey(key))
foreach (MembershipInfo tmpInfo in _AllByPrimaryKey[key])
if (_CacheByPrimaryKey.ContainsKey(key))
foreach (MembershipInfo tmpInfo in _CacheByPrimaryKey[key])
tmpInfo.RefreshFields(tmp);
}
private void RefreshFields(Membership tmp)
protected virtual void RefreshFields(Membership tmp)
{
if (_UID != tmp.UID)
{
MyUser.RefreshUserMemberships(); // Update List for old value
if (MyUser != null) MyUser.RefreshUserMemberships(); // Update List for old value
_UID = tmp.UID; // Update the value
}
_MyUser = null; // Reset list so that the next line gets a new list
if (MyUser != null) MyUser.RefreshUserMemberships(); // Update List for new value
if (_GID != tmp.GID)
{
MyGroup.RefreshGroupMemberships(); // Update List for old value
if (MyGroup != null) MyGroup.RefreshGroupMemberships(); // Update List for old value
_GID = tmp.GID; // Update the value
}
_MyGroup = null; // Reset list so that the next line gets a new list
@@ -269,15 +276,15 @@ namespace VEPROMS.CSLA.Library
{
string key = tmp.UGID.ToString();
ConvertListToDictionary();
if (_AllByPrimaryKey.ContainsKey(key))
foreach (MembershipInfo tmpInfo in _AllByPrimaryKey[key])
if (_CacheByPrimaryKey.ContainsKey(key))
foreach (MembershipInfo tmpInfo in _CacheByPrimaryKey[key])
tmpInfo.RefreshFields(tmp);
}
private void RefreshFields(GroupMembership tmp)
protected virtual void RefreshFields(GroupMembership tmp)
{
if (_UID != tmp.UID)
{
MyUser.RefreshUserMemberships(); // Update List for old value
if (MyUser != null) MyUser.RefreshUserMemberships(); // Update List for old value
_UID = tmp.UID; // Update the value
}
_MyUser = null; // Reset list so that the next line gets a new list
@@ -296,15 +303,15 @@ namespace VEPROMS.CSLA.Library
{
string key = tmp.UGID.ToString();
ConvertListToDictionary();
if (_AllByPrimaryKey.ContainsKey(key))
foreach (MembershipInfo tmpInfo in _AllByPrimaryKey[key])
if (_CacheByPrimaryKey.ContainsKey(key))
foreach (MembershipInfo tmpInfo in _CacheByPrimaryKey[key])
tmpInfo.RefreshFields(tmp);
}
private void RefreshFields(UserMembership tmp)
protected virtual void RefreshFields(UserMembership tmp)
{
if (_GID != tmp.GID)
{
MyGroup.RefreshGroupMemberships(); // Update List for old value
if (MyGroup != null) MyGroup.RefreshGroupMemberships(); // Update List for old value
_GID = tmp.GID; // Update the value
}
_MyGroup = null; // Reset list so that the next line gets a new list
@@ -325,13 +332,17 @@ namespace VEPROMS.CSLA.Library
// throw new System.Security.SecurityException("User not authorized to view a Membership");
try
{
MembershipInfo tmp = GetExistingByPrimaryKey(ugid);
MembershipInfo tmp = GetCachedByPrimaryKey(ugid);
if (tmp == null)
{
tmp = DataPortal.Fetch<MembershipInfo>(new PKCriteria(ugid));
if (!_AllList.Contains(tmp)) _AllList.Add(tmp);
AddToCache(tmp);
}
if (tmp.ErrorMessage == "No Record Found")
{
tmp.Dispose(); // Clean-up MembershipInfo
tmp = null;
}
if (tmp.ErrorMessage == "No Record Found") tmp = null;
return tmp;
}
catch (Exception ex)
@@ -343,7 +354,6 @@ namespace VEPROMS.CSLA.Library
#region Data Access Portal
internal MembershipInfo(SafeDataReader dr)
{
_MyMembershipInfoUnique = ++_MembershipInfoUnique;
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] MembershipInfo.Constructor", GetHashCode());
try
{