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<UserInfo> _AllList = new List<UserInfo>();
private static Dictionary<string, List<UserInfo>> _AllByPrimaryKey = new Dictionary<string, List<UserInfo>>();
private static List<UserInfo> _CacheList = new List<UserInfo>();
protected static void AddToCache(UserInfo userInfo)
{
if (!_CacheList.Contains(userInfo)) _CacheList.Add(userInfo); // In AddToCache
}
protected static void RemoveFromCache(UserInfo userInfo)
{
while (_CacheList.Contains(userInfo)) _CacheList.Remove(userInfo); // In RemoveFromCache
}
private static Dictionary<string, List<UserInfo>> _CacheByPrimaryKey = new Dictionary<string, List<UserInfo>>();
private static void ConvertListToDictionary()
{
List<UserInfo> remove = new List<UserInfo>();
foreach (UserInfo tmp in _AllList)
foreach (UserInfo tmp in _CacheList)
{
if (!_AllByPrimaryKey.ContainsKey(tmp.UID.ToString()))
if (!_CacheByPrimaryKey.ContainsKey(tmp.UID.ToString()))
{
_AllByPrimaryKey[tmp.UID.ToString()] = new List<UserInfo>(); // Add new list for PrimaryKey
_CacheByPrimaryKey[tmp.UID.ToString()] = new List<UserInfo>(); // Add new list for PrimaryKey
}
_AllByPrimaryKey[tmp.UID.ToString()].Add(tmp); // Add to Primary Key list
_CacheByPrimaryKey[tmp.UID.ToString()].Add(tmp); // Add to Primary Key list
remove.Add(tmp);
}
foreach (UserInfo tmp in remove)
_AllList.Remove(tmp);
RemoveFromCache(tmp);
}
internal static void AddList(UserInfoList lst)
{
foreach (UserInfo item in lst) _AllList.Add(item);
foreach (UserInfo item in lst) AddToCache(item);
}
public static UserInfo GetExistingByPrimaryKey(int uid)
protected static UserInfo GetCachedByPrimaryKey(int uid)
{
ConvertListToDictionary();
string key = uid.ToString();
if (_AllByPrimaryKey.ContainsKey(key)) return _AllByPrimaryKey[key][0];
if (_CacheByPrimaryKey.ContainsKey(key)) return _CacheByPrimaryKey[key][0];
return null;
}
#endregion
@@ -251,11 +259,11 @@ namespace VEPROMS.CSLA.Library
return _UserMemberships;
}
}
internal void RefreshUserMemberships()
public void RefreshUserMemberships()
{
ConvertListToDictionary();
if (_AllByPrimaryKey.ContainsKey(_UID.ToString()))
foreach (UserInfo tmp in _AllByPrimaryKey[_UID.ToString()])
if (_CacheByPrimaryKey.ContainsKey(_UID.ToString()))
foreach (UserInfo tmp in _CacheByPrimaryKey[_UID.ToString()])
tmp._UserMembershipCount = -1; // This will cause the data to be requeried
}
// TODO: Replace base UserInfo.ToString function as necessary
@@ -279,24 +287,23 @@ namespace VEPROMS.CSLA.Library
#endregion
#region Factory Methods
private static int _UserInfoUnique = 0;
private int _MyUserInfoUnique;
private static int UserInfoUnique
{ get { return ++_UserInfoUnique; } }
private int _MyUserInfoUnique = UserInfoUnique;
public int MyUserInfoUnique
{
get { return _MyUserInfoUnique; }
}
private UserInfo()
{ get { return _MyUserInfoUnique; } }
protected UserInfo()
{/* require use of factory methods */
_MyUserInfoUnique = ++_UserInfoUnique;
_AllList.Add(this);
AddToCache(this);
}
public void Dispose()
{
_AllList.Remove(this);
if (!_AllByPrimaryKey.ContainsKey(UID.ToString())) return;
List<UserInfo> listUserInfo = _AllByPrimaryKey[UID.ToString()]; // Get the list of items
listUserInfo.Remove(this); // Remove the item from the list
RemoveFromCache(this);
if (!_CacheByPrimaryKey.ContainsKey(UID.ToString())) return;
List<UserInfo> listUserInfo = _CacheByPrimaryKey[UID.ToString()]; // Get the list of items
while (listUserInfo.Contains(this)) listUserInfo.Remove(this); // Remove the item from the list
if (listUserInfo.Count == 0) // If there are no items left in the list
_AllByPrimaryKey.Remove(UID.ToString()); // remove the list
_CacheByPrimaryKey.Remove(UID.ToString()); // remove the list
}
public virtual User Get()
{
@@ -306,11 +313,11 @@ namespace VEPROMS.CSLA.Library
{
string key = tmp.UID.ToString();
ConvertListToDictionary();
if (_AllByPrimaryKey.ContainsKey(key))
foreach (UserInfo tmpInfo in _AllByPrimaryKey[key])
if (_CacheByPrimaryKey.ContainsKey(key))
foreach (UserInfo tmpInfo in _CacheByPrimaryKey[key])
tmpInfo.RefreshFields(tmp);
}
private void RefreshFields(User tmp)
protected virtual void RefreshFields(User tmp)
{
_UserID = tmp.UserID;
_FirstName = tmp.FirstName;
@@ -334,13 +341,17 @@ namespace VEPROMS.CSLA.Library
// throw new System.Security.SecurityException("User not authorized to view a User");
try
{
UserInfo tmp = GetExistingByPrimaryKey(uid);
UserInfo tmp = GetCachedByPrimaryKey(uid);
if (tmp == null)
{
tmp = DataPortal.Fetch<UserInfo>(new PKCriteria(uid));
if (!_AllList.Contains(tmp)) _AllList.Add(tmp);
AddToCache(tmp);
}
if (tmp.ErrorMessage == "No Record Found")
{
tmp.Dispose(); // Clean-up UserInfo
tmp = null;
}
if (tmp.ErrorMessage == "No Record Found") tmp = null;
return tmp;
}
catch (Exception ex)
@@ -352,7 +363,6 @@ namespace VEPROMS.CSLA.Library
#region Data Access Portal
internal UserInfo(SafeDataReader dr)
{
_MyUserInfoUnique = ++_UserInfoUnique;
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] UserInfo.Constructor", GetHashCode());
try
{