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

@@ -64,28 +64,36 @@ namespace VEPROMS.CSLA.Library
}
#endregion
#region Collection
protected static List<User> _AllList = new List<User>();
private static Dictionary<string, List<User>> _AllByPrimaryKey = new Dictionary<string, List<User>>();
private static List<User> _CacheList = new List<User>();
protected static void AddToCache(User user)
{
if (!_CacheList.Contains(user)) _CacheList.Add(user); // In AddToCache
}
protected static void RemoveFromCache(User user)
{
while (_CacheList.Contains(user)) _CacheList.Remove(user); // In RemoveFromCache
}
private static Dictionary<string, List<User>> _CacheByPrimaryKey = new Dictionary<string, List<User>>();
private static void ConvertListToDictionary()
{
List<User> remove = new List<User>();
foreach (User tmp in _AllList)
foreach (User tmp in _CacheList)
{
if (!_AllByPrimaryKey.ContainsKey(tmp.UID.ToString()))
if (!_CacheByPrimaryKey.ContainsKey(tmp.UID.ToString()))
{
_AllByPrimaryKey[tmp.UID.ToString()] = new List<User>(); // Add new list for PrimaryKey
_CacheByPrimaryKey[tmp.UID.ToString()] = new List<User>(); // 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 (User tmp in remove)
_AllList.Remove(tmp);
RemoveFromCache(tmp);
}
public static User GetExistingByPrimaryKey(int uid)
protected static User 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
@@ -618,24 +626,29 @@ namespace VEPROMS.CSLA.Library
public int CurrentEditLevel
{ get { return EditLevel; } }
private static int _UserUnique = 0;
private int _MyUserUnique;
protected static int UserUnique
{ get { return ++_UserUnique; } }
private int _MyUserUnique = UserUnique;
public int MyUserUnique
{
get { return _MyUserUnique; }
}
{ get { return _MyUserUnique; } }
protected User()
{/* require use of factory methods */
_MyUserUnique = ++_UserUnique;
_AllList.Add(this);
AddToCache(this);
}
public void Dispose()
{
_AllList.Remove(this);
if (!_AllByPrimaryKey.ContainsKey(UID.ToString())) return;
List<User> listUser = _AllByPrimaryKey[UID.ToString()]; // Get the list of items
listUser.Remove(this); // Remove the item from the list
if (listUser.Count == 0) //If there are no items left in the list
_AllByPrimaryKey.Remove(UID.ToString()); // remove the list
RemoveFromDictionaries();
}
private void RemoveFromDictionaries()
{
RemoveFromCache(this);
if (_CacheByPrimaryKey.ContainsKey(UID.ToString()))
{
List<User> listUser = _CacheByPrimaryKey[UID.ToString()]; // Get the list of items
while (listUser.Contains(this)) listUser.Remove(this); // Remove the item from the list
if (listUser.Count == 0) //If there are no items left in the list
_CacheByPrimaryKey.Remove(UID.ToString()); // remove the list
}
}
public static User New()
{
@@ -672,7 +685,11 @@ namespace VEPROMS.CSLA.Library
{
User tmp = User.New(userID, firstName, middleName, lastName, suffix, courtesyTitle, phoneNumber, cFGName, userLogin, userName, config, dts, usrID);
if (tmp.IsSavable)
tmp = tmp.Save();
{
User tmp2 = tmp;
tmp = tmp2.Save();
tmp2.Dispose();
}
else
{
Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules();
@@ -703,7 +720,11 @@ namespace VEPROMS.CSLA.Library
{
User tmp = User.New(firstName, middleName, lastName, suffix, courtesyTitle, phoneNumber, cFGName, userLogin, userName, config);
if (tmp.IsSavable)
tmp = tmp.Save();
{
User tmp2 = tmp;
tmp = tmp2.Save();
tmp2.Dispose();
}
else
{
Csla.Validation.BrokenRulesCollection brc = tmp.ValidationRules.GetBrokenRules();
@@ -721,13 +742,17 @@ namespace VEPROMS.CSLA.Library
throw new System.Security.SecurityException("User not authorized to view a User");
try
{
User tmp = GetExistingByPrimaryKey(uid);
User tmp = GetCachedByPrimaryKey(uid);
if (tmp == null)
{
tmp = DataPortal.Fetch<User>(new PKCriteria(uid));
if (!_AllList.Contains(tmp)) _AllList.Add(tmp);
AddToCache(tmp);
}
if (tmp.ErrorMessage == "No Record Found")
{
tmp.Dispose(); // Clean-up User
tmp = null;
}
if (tmp.ErrorMessage == "No Record Found") tmp = null;
return tmp;
}
catch (Exception ex)
@@ -769,7 +794,8 @@ namespace VEPROMS.CSLA.Library
{
BuildRefreshList();
User user = base.Save();
_AllList.Add(user);//Refresh the item in AllList
RemoveFromDictionaries(); // if save is successful remove the previous Folder from the cache
AddToCache(user);//Refresh the item in AllList
ProcessRefreshList();
return user;
}