CSLA - R - Group 1

This commit is contained in:
2026-09-11 16:03:51 -04:00
parent d9f8f6cb37
commit d238d6aed2
33 changed files with 2101 additions and 4829 deletions
+122 -274
View File
@@ -13,8 +13,6 @@ using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using System.Configuration;
using System.IO;
using System.ComponentModel;
using System.Collections.Generic;
using Csla.Validation;
@@ -82,6 +80,7 @@ namespace VEPROMS.CSLA.Library
}
#endregion
#region Collection
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Keeping Collections Not ReadOnly")]
private static List<Role> _CacheList = new List<Role>();
protected static void AddToCache(Role role)
{
@@ -91,7 +90,9 @@ namespace VEPROMS.CSLA.Library
{
while (_CacheList.Contains(role)) _CacheList.Remove(role); // In RemoveFromCache
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Keeping Collections Not ReadOnly")]
private static Dictionary<string, List<Role>> _CacheByPrimaryKey = new Dictionary<string, List<Role>>();
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Keeping Collections Not ReadOnly")]
private static Dictionary<string, List<Role>> _CacheByName = new Dictionary<string, List<Role>>();
private static void ConvertListToDictionary()
{
@@ -126,15 +127,9 @@ namespace VEPROMS.CSLA.Library
#endregion
#region Business Methods
private string _ErrorMessage = string.Empty;
public string ErrorMessage
{
get { return _ErrorMessage; }
}
public string ErrorMessage => _ErrorMessage;
private static int _nextRID = -1;
public static int NextRID
{
get { return _nextRID--; }
}
public static int NextRID => _nextRID--;
private int _RID;
[System.ComponentModel.DataObjectField(true, true)]
public int RID
@@ -252,10 +247,7 @@ namespace VEPROMS.CSLA.Library
return _RoleAssignments;
}
}
public void Reset_RoleAssignments()
{
_RoleAssignmentCount = -1;
}
public void Reset_RoleAssignments() => _RoleAssignmentCount = -1;
private int _RolePermissionCount = 0;
/// <summary>
/// Count of RolePermissions for this Role
@@ -287,10 +279,7 @@ namespace VEPROMS.CSLA.Library
return _RolePermissions;
}
}
public void Reset_RolePermissions()
{
_RolePermissionCount = -1;
}
public void Reset_RolePermissions() => _RolePermissionCount = -1;
public override bool IsDirty
{
get
@@ -305,37 +294,22 @@ namespace VEPROMS.CSLA.Library
if (base.IsDirty || list.Contains(this))
return base.IsDirty;
list.Add(this);
return base.IsDirty || (_RoleAssignments == null ? false : _RoleAssignments.IsDirtyList(list)) || (_RolePermissions == null ? false : _RolePermissions.IsDirtyList(list));
}
public override bool IsValid
{
get { return IsValidList(new List<object>()); }
return base.IsDirty || (_RoleAssignments != null && _RoleAssignments.IsDirtyList(list)) || (_RolePermissions != null && _RolePermissions.IsDirtyList(list));
}
public override bool IsValid => IsValidList(new List<object>());
public bool IsValidList(List<object> list)
{
if (list.Contains(this))
return (IsNew && !IsDirty) ? true : base.IsValid;
return (IsNew && !IsDirty) || base.IsValid;
list.Add(this);
return ((IsNew && !IsDirty) ? true : base.IsValid) && (_RoleAssignments == null ? true : _RoleAssignments.IsValidList(list)) && (_RolePermissions == null ? true : _RolePermissions.IsValidList(list));
return ((IsNew && !IsDirty) || base.IsValid) && (_RoleAssignments == null || _RoleAssignments.IsValidList(list)) && (_RolePermissions == null || _RolePermissions.IsValidList(list));
}
// CSLATODO: Replace base Role.ToString function as necessary
/// <summary>
/// Overrides Base ToString
/// </summary>
/// <returns>A string representation of current Role</returns>
//public override string ToString()
//{
// return base.ToString();
//}
// CSLATODO: Check Role.GetIdValue to assure that the ID returned is unique
/// <summary>
/// Overrides Base GetIdValue - Used internally by CSLA to determine equality
/// </summary>
/// <returns>A Unique ID for the current Role</returns>
protected override object GetIdValue()
{
return MyRoleUnique; // Absolutely Unique ID
}
protected override object GetIdValue() => MyRoleUnique; // Absolutely Unique ID
#endregion
#region ValidationRules
[NonSerialized]
@@ -365,8 +339,8 @@ namespace VEPROMS.CSLA.Library
get
{
IVEHasBrokenRules hasBrokenRules = HasBrokenRules;
if (this.Equals(hasBrokenRules)) return BrokenRulesCollection;
return (hasBrokenRules != null ? hasBrokenRules.BrokenRules : null);
if (Equals(hasBrokenRules)) return BrokenRulesCollection;
return hasBrokenRules?.BrokenRules;
}
}
protected override void AddBusinessRules()
@@ -395,31 +369,11 @@ namespace VEPROMS.CSLA.Library
_RoleExtension.AddInstanceValidationRules(ValidationRules);
// CSLATODO: Add other validation rules
}
// Sample data comparison validation rule
//private bool StartDateGTEndDate(object target, Csla.Validation.RuleArgs e)
//{
// if (_started > _ended)
// {
// e.Description = "Start date can't be after end date";
// return false;
// }
// else
// return true;
//}
#endregion
#region Authorization Rules
protected override void AddAuthorizationRules()
{
//CSLATODO: Who can read/write which fields
//AuthorizationRules.AllowRead(RID, "<Role(s)>");
//AuthorizationRules.AllowRead(Name, "<Role(s)>");
//AuthorizationRules.AllowRead(Title, "<Role(s)>");
//AuthorizationRules.AllowRead(DTS, "<Role(s)>");
//AuthorizationRules.AllowRead(UsrID, "<Role(s)>");
//AuthorizationRules.AllowWrite(Name, "<Role(s)>");
//AuthorizationRules.AllowWrite(Title, "<Role(s)>");
//AuthorizationRules.AllowWrite(DTS, "<Role(s)>");
//AuthorizationRules.AllowWrite(UsrID, "<Role(s)>");
_RoleExtension.AddAuthorizationRules(AuthorizationRules);
}
protected override void AddInstanceAuthorizationRules()
@@ -427,56 +381,14 @@ namespace VEPROMS.CSLA.Library
//CSLATODO: Who can read/write which fields
_RoleExtension.AddInstanceAuthorizationRules(AuthorizationRules);
}
public static bool CanAddObject()
{
// CSLATODO: Can Add Authorization
//return Csla.ApplicationContext.User.IsInRole("ProjectManager");
return true;
}
public static bool CanGetObject()
{
// CSLATODO: CanGet Authorization
return true;
}
public static bool CanDeleteObject()
{
// CSLATODO: CanDelete Authorization
//bool result = false;
//if (Csla.ApplicationContext.User.IsInRole("ProjectManager"))result = true;
//if (Csla.ApplicationContext.User.IsInRole("Administrator"))result = true;
//return result;
return true;
}
/// <summary>
/// determines if related records (Foreign Keys) will keep this Item from being deleted
/// </summary>
public bool CanDelete
{
get
{
// Check to make sure that there are not any related records
int usedByCount = 0;
usedByCount += _RoleAssignmentCount;
usedByCount += _RolePermissionCount;
return (usedByCount == 0);
}
}
public static bool CanEditObject()
{
// CSLATODO: CanEdit Authorization
//return Csla.ApplicationContext.User.IsInRole("ProjectManager");
return true;
}
#endregion
#region Factory Methods
public int CurrentEditLevel
{ get { return EditLevel; } }
public int CurrentEditLevel => EditLevel;
private static int _RoleUnique = 0;
protected static int RoleUnique
{ get { return ++_RoleUnique; } }
private int _MyRoleUnique = RoleUnique;
public int MyRoleUnique // Absolutely Unique ID - Editable
{ get { return _MyRoleUnique; } }
protected static int RoleUnique => ++_RoleUnique;
private readonly int _MyRoleUnique = RoleUnique;
// Absolutely Unique ID - Editable
public int MyRoleUnique => _MyRoleUnique;
protected Role()
{/* require use of factory methods */
AddToCache(this);
@@ -485,15 +397,11 @@ namespace VEPROMS.CSLA.Library
private static int _CountCreated = 0;
private static int _CountDisposed = 0;
private static int _CountFinalized = 0;
private static int IncrementCountCreated
{ get { return ++_CountCreated; } }
private int _CountWhenCreated = IncrementCountCreated;
public static int CountCreated
{ get { return _CountCreated; } }
public static int CountNotDisposed
{ get { return _CountCreated - _CountDisposed; } }
public static int CountNotFinalized
{ get { return _CountCreated - _CountFinalized; } }
private static int IncrementCountCreated => ++_CountCreated;
private readonly int _CountWhenCreated = IncrementCountCreated;
public static int CountCreated => _CountCreated;
public static int CountNotDisposed => _CountCreated - _CountDisposed;
public static int CountNotFinalized => _CountCreated - _CountFinalized;
~Role()
{
_CountFinalized++;
@@ -530,8 +438,6 @@ namespace VEPROMS.CSLA.Library
}
public static Role New()
{
if (!CanAddObject())
throw new System.Security.SecurityException("User not authorized to add a Role");
try
{
return DataPortal.Create<Role>();
@@ -591,8 +497,6 @@ namespace VEPROMS.CSLA.Library
}
public static Role Get(int rid)
{
if (!CanGetObject())
throw new System.Security.SecurityException("User not authorized to view a Role");
try
{
Role tmp = GetCachedByPrimaryKey(rid);
@@ -615,8 +519,6 @@ namespace VEPROMS.CSLA.Library
}
public static Role GetByName(string name)
{
if (!CanGetObject())
throw new System.Security.SecurityException("User not authorized to view a Role");
try
{
Role tmp = GetCachedByName(name);
@@ -642,14 +544,9 @@ namespace VEPROMS.CSLA.Library
if (dr.Read()) return new Role(dr);
return null;
}
internal Role(SafeDataReader dr)
{
ReadData(dr);
}
internal Role(SafeDataReader dr) => ReadData(dr);
public static void Delete(int rid)
{
if (!CanDeleteObject())
throw new System.Security.SecurityException("User not authorized to remove a Role");
try
{
DataPortal.Delete(new PKCriteria(rid));
@@ -661,12 +558,6 @@ namespace VEPROMS.CSLA.Library
}
public override Role Save()
{
if (IsDeleted && !CanDeleteObject())
throw new System.Security.SecurityException("User not authorized to remove a Role");
else if (IsNew && !CanAddObject())
throw new System.Security.SecurityException("User not authorized to add a Role");
else if (!CanEditObject())
throw new System.Security.SecurityException("User not authorized to update a Role");
try
{
BuildRefreshList();
@@ -686,24 +577,16 @@ namespace VEPROMS.CSLA.Library
[Serializable()]
protected class PKCriteria
{
private int _RID;
public int RID
{ get { return _RID; } }
public PKCriteria(int rid)
{
_RID = rid;
}
private readonly int _RID;
public int RID => _RID;
public PKCriteria(int rid) => _RID = rid;
}
[Serializable()]
private class NameCriteria
{
private string _Name;
public string Name
{ get { return _Name; } }
public NameCriteria(string name)
{
_Name = name;
}
private readonly string _Name;
public string Name => _Name;
public NameCriteria(string name) => _Name = name;
}
// CSLATODO: If Create needs to access DB - It should not be marked RunLocal
[RunLocal()]
@@ -844,37 +727,44 @@ namespace VEPROMS.CSLA.Library
[Transactional(TransactionalTypes.TransactionScope)]
internal void SQLInsert()
{
if (!this.IsDirty) return;
if (!IsDirty) return;
try
{
SqlConnection cn = (SqlConnection)ApplicationContext.LocalContext["cn"];
using (SqlCommand cm = cn.CreateCommand())
using (SqlConnection cn = (SqlConnection)ApplicationContext.LocalContext["cn"])
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandTimeout = Database.SQLTimeout;
cm.CommandText = "addRole";
// Input All Fields - Except Calculated Columns
cm.Parameters.AddWithValue("@Name", _Name);
cm.Parameters.AddWithValue("@Title", _Title);
if (_DTS.Year >= 1753 && _DTS.Year <= 9999) cm.Parameters.AddWithValue("@DTS", _DTS);
cm.Parameters.AddWithValue("@UsrID", _UsrID);
// Output Calculated Columns
SqlParameter param_RID = new SqlParameter("@newRID", SqlDbType.Int);
param_RID.Direction = ParameterDirection.Output;
cm.Parameters.Add(param_RID);
SqlParameter param_LastChanged = new SqlParameter("@newLastChanged", SqlDbType.Timestamp);
param_LastChanged.Direction = ParameterDirection.Output;
cm.Parameters.Add(param_LastChanged);
// CSLATODO: Define any additional output parameters
cm.ExecuteNonQuery();
// Save all values being returned from the Procedure
_RID = (int)cm.Parameters["@newRID"].Value;
_LastChanged = (byte[])cm.Parameters["@newLastChanged"].Value;
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandTimeout = Database.SQLTimeout;
cm.CommandText = "addRole";
// Input All Fields - Except Calculated Columns
cm.Parameters.AddWithValue("@Name", _Name);
cm.Parameters.AddWithValue("@Title", _Title);
if (_DTS.Year >= 1753 && _DTS.Year <= 9999) cm.Parameters.AddWithValue("@DTS", _DTS);
cm.Parameters.AddWithValue("@UsrID", _UsrID);
// Output Calculated Columns
SqlParameter param_RID = new SqlParameter("@newRID", SqlDbType.Int)
{
Direction = ParameterDirection.Output
};
cm.Parameters.Add(param_RID);
SqlParameter param_LastChanged = new SqlParameter("@newLastChanged", SqlDbType.Timestamp)
{
Direction = ParameterDirection.Output
};
cm.Parameters.Add(param_LastChanged);
// CSLATODO: Define any additional output parameters
cm.ExecuteNonQuery();
// Save all values being returned from the Procedure
_RID = (int)cm.Parameters["@newRID"].Value;
_LastChanged = (byte[])cm.Parameters["@newLastChanged"].Value;
}
}
MarkOld();
// update child objects
if (_RoleAssignments != null) _RoleAssignments.Update(this);
if (_RolePermissions != null) _RolePermissions.Update(this);
_RoleAssignments?.Update(this);
_RolePermissions?.Update(this);
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] Role.SQLInsert", GetHashCode());
}
catch (Exception ex)
@@ -901,11 +791,15 @@ namespace VEPROMS.CSLA.Library
if (dts.Year >= 1753 && dts.Year <= 9999) cm.Parameters.AddWithValue("@DTS", dts);
cm.Parameters.AddWithValue("@UsrID", usrID);
// Output Calculated Columns
SqlParameter param_RID = new SqlParameter("@newRID", SqlDbType.Int);
param_RID.Direction = ParameterDirection.Output;
SqlParameter param_RID = new SqlParameter("@newRID", SqlDbType.Int)
{
Direction = ParameterDirection.Output
};
cm.Parameters.Add(param_RID);
SqlParameter param_LastChanged = new SqlParameter("@newLastChanged", SqlDbType.Timestamp);
param_LastChanged.Direction = ParameterDirection.Output;
SqlParameter param_LastChanged = new SqlParameter("@newLastChanged", SqlDbType.Timestamp)
{
Direction = ParameterDirection.Output
};
cm.Parameters.Add(param_LastChanged);
// CSLATODO: Define any additional output parameters
cm.ExecuteNonQuery();
@@ -950,35 +844,40 @@ namespace VEPROMS.CSLA.Library
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] Role.SQLUpdate", GetHashCode());
try
{
SqlConnection cn = (SqlConnection)ApplicationContext.LocalContext["cn"];
if (base.IsDirty)
using (SqlConnection cn = (SqlConnection)ApplicationContext.LocalContext["cn"])
{
using (SqlCommand cm = cn.CreateCommand())
if (base.IsDirty)
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandTimeout = Database.SQLTimeout;
cm.CommandText = "updateRole";
// All Fields including Calculated Fields
cm.Parameters.AddWithValue("@RID", _RID);
cm.Parameters.AddWithValue("@Name", _Name);
cm.Parameters.AddWithValue("@Title", _Title);
if (_DTS.Year >= 1753 && _DTS.Year <= 9999) cm.Parameters.AddWithValue("@DTS", _DTS);
cm.Parameters.AddWithValue("@UsrID", _UsrID);
cm.Parameters.AddWithValue("@LastChanged", _LastChanged);
// Output Calculated Columns
SqlParameter param_LastChanged = new SqlParameter("@newLastChanged", SqlDbType.Timestamp);
param_LastChanged.Direction = ParameterDirection.Output;
cm.Parameters.Add(param_LastChanged);
// CSLATODO: Define any additional output parameters
cm.ExecuteNonQuery();
// Save all values being returned from the Procedure
_LastChanged = (byte[])cm.Parameters["@newLastChanged"].Value;
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandTimeout = Database.SQLTimeout;
cm.CommandText = "updateRole";
// All Fields including Calculated Fields
cm.Parameters.AddWithValue("@RID", _RID);
cm.Parameters.AddWithValue("@Name", _Name);
cm.Parameters.AddWithValue("@Title", _Title);
if (_DTS.Year >= 1753 && _DTS.Year <= 9999) cm.Parameters.AddWithValue("@DTS", _DTS);
cm.Parameters.AddWithValue("@UsrID", _UsrID);
cm.Parameters.AddWithValue("@LastChanged", _LastChanged);
// Output Calculated Columns
SqlParameter param_LastChanged = new SqlParameter("@newLastChanged", SqlDbType.Timestamp)
{
Direction = ParameterDirection.Output
};
cm.Parameters.Add(param_LastChanged);
// CSLATODO: Define any additional output parameters
cm.ExecuteNonQuery();
// Save all values being returned from the Procedure
_LastChanged = (byte[])cm.Parameters["@newLastChanged"].Value;
}
}
}
MarkOld();
// use the open connection to update child objects
if (_RoleAssignments != null) _RoleAssignments.Update(this);
if (_RolePermissions != null) _RolePermissions.Update(this);
_RoleAssignments?.Update(this);
_RolePermissions?.Update(this);
}
catch (Exception ex)
{
@@ -989,18 +888,21 @@ namespace VEPROMS.CSLA.Library
}
internal void Update()
{
if (!this.IsDirty) return;
if (!IsDirty) return;
if (base.IsDirty)
{
SqlConnection cn = (SqlConnection)ApplicationContext.LocalContext["cn"];
if (IsNew)
_LastChanged = Role.Add(cn, ref _RID, _Name, _Title, _DTS, _UsrID);
else
_LastChanged = Role.Update(cn, ref _RID, _Name, _Title, _DTS, _UsrID, ref _LastChanged);
using (SqlConnection cn = (SqlConnection)ApplicationContext.LocalContext["cn"])
{
if (IsNew)
_LastChanged = Role.Add(cn, ref _RID, _Name, _Title, _DTS, _UsrID);
else
_LastChanged = Role.Update(cn, ref _RID, _Name, _Title, _DTS, _UsrID, ref _LastChanged);
}
MarkOld();
}
if (_RoleAssignments != null) _RoleAssignments.Update(this);
if (_RolePermissions != null) _RolePermissions.Update(this);
_RoleAssignments?.Update(this);
_RolePermissions?.Update(this);
}
[Transactional(TransactionalTypes.TransactionScope)]
public static byte[] Update(SqlConnection cn, ref int rid, string name, string title, DateTime dts, string usrID, ref byte[] lastChanged)
@@ -1021,8 +923,10 @@ namespace VEPROMS.CSLA.Library
cm.Parameters.AddWithValue("@UsrID", usrID);
cm.Parameters.AddWithValue("@LastChanged", lastChanged);
// Output Calculated Columns
SqlParameter param_LastChanged = new SqlParameter("@newLastChanged", SqlDbType.Timestamp);
param_LastChanged.Direction = ParameterDirection.Output;
SqlParameter param_LastChanged = new SqlParameter("@newLastChanged", SqlDbType.Timestamp)
{
Direction = ParameterDirection.Output
};
cm.Parameters.Add(param_LastChanged);
// CSLATODO: Define any additional output parameters
cm.ExecuteNonQuery();
@@ -1107,16 +1011,10 @@ namespace VEPROMS.CSLA.Library
[Serializable()]
private class ExistsCommand : CommandBase
{
private int _RID;
private readonly int _RID;
private bool _exists;
public bool Exists
{
get { return _exists; }
}
public ExistsCommand(int rid)
{
_RID = rid;
}
public bool Exists => _exists;
public ExistsCommand(int rid) => _RID = rid;
protected override void DataPortal_Execute()
{
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] Role.DataPortal_Execute", GetHashCode());
@@ -1146,7 +1044,7 @@ namespace VEPROMS.CSLA.Library
#endregion
// Standard Default Code
#region extension
RoleExtension _RoleExtension = new RoleExtension();
readonly RoleExtension _RoleExtension = new RoleExtension();
[Serializable()]
partial class RoleExtension : extensionBase
{
@@ -1155,14 +1053,8 @@ namespace VEPROMS.CSLA.Library
class extensionBase
{
// Default Values
public virtual DateTime DefaultDTS
{
get { return DateTime.Now; }
}
public virtual string DefaultUsrID
{
get { return Volian.Base.Library.VlnSettings.UserID; }
}
public virtual DateTime DefaultDTS => DateTime.Now;
public virtual string DefaultUsrID => Volian.Base.Library.VlnSettings.UserID;
// Authorization Rules
public virtual void AddAuthorizationRules(Csla.Security.AuthorizationRules rules)
{
@@ -1191,57 +1083,13 @@ namespace VEPROMS.CSLA.Library
{
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destType)
{
if (destType == typeof(string) && value is Role)
if (destType == typeof(string) && value is Role role)
{
// Return the ToString value
return ((Role)value).ToString();
return role.ToString();
}
return base.ConvertTo(context, culture, value, destType);
}
}
#endregion
} // Namespace
//// The following is a sample Extension File. You can use it to create RoleExt.cs
//using System;
//using System.Collections.Generic;
//using System.Text;
//using Csla;
//namespace VEPROMS.CSLA.Library
//{
// public partial class Role
// {
// partial class RoleExtension : extensionBase
// {
// // CSLATODO: Override automatic defaults
// public virtual DateTime DefaultDTS
// {
// get { return DateTime.Now; }
// }
// public virtual string DefaultUsrID
// {
// get { return Environment.UserName.ToUpper(); }
// }
// public new void AddAuthorizationRules(Csla.Security.AuthorizationRules rules)
// {
// //rules.AllowRead(Dbid, "<Role(s)>");
// }
// public new void AddInstanceAuthorizationRules(Csla.Security.AuthorizationRules rules)
// {
// //rules.AllowInstanceRead(Dbid, "<Role(s)>");
// }
// public new void AddValidationRules(Csla.Validation.ValidationRules rules)
// {
// rules.AddRule(
// Csla.Validation.CommonRules.StringMaxLength,
// new Csla.Validation.CommonRules.MaxLengthRuleArgs("Name", 100));
// }
// public new void AddInstanceValidationRules(Csla.Validation.ValidationRules rules)
// {
// rules.AddInstanceRule(/* Instance Validation Rule */);
// }
// }
// }
//}