CSLA - M through P

This commit is contained in:
2026-09-11 09:50:40 -04:00
parent e41a4ea65d
commit d9f8f6cb37
20 changed files with 1196 additions and 2757 deletions
+106 -231
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;
@@ -37,10 +35,7 @@ namespace VEPROMS.CSLA.Library
if (IsDirty)
refreshParts.Add(this);
}
private void ClearRefreshList()
{
_RefreshParts = new List<Part>();
}
private void ClearRefreshList() => _RefreshParts = new List<Part>();
private void BuildRefreshList()
{
ClearRefreshList();
@@ -58,6 +53,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<Part> _CacheList = new List<Part>();
protected static void AddToCache(Part part)
{
@@ -67,6 +63,7 @@ namespace VEPROMS.CSLA.Library
{
while (_CacheList.Contains(part)) _CacheList.Remove(part); // In RemoveFromCache
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "Keeping Collections Not ReadOnly")]
private static Dictionary<string, List<Part>> _CacheByPrimaryKey = new Dictionary<string, List<Part>>();
private static void ConvertListToDictionary()
{
@@ -92,10 +89,7 @@ namespace VEPROMS.CSLA.Library
#endregion
#region Business Methods
private string _ErrorMessage = string.Empty;
public string ErrorMessage
{
get { return _ErrorMessage; }
}
public string ErrorMessage => _ErrorMessage;
private int _ContentID;
[System.ComponentModel.DataObjectField(true, true)]
public int ContentID
@@ -210,37 +204,22 @@ namespace VEPROMS.CSLA.Library
if (base.IsDirty || list.Contains(this))
return base.IsDirty;
list.Add(this);
return base.IsDirty || (_MyContent == null ? false : _MyContent.IsDirtyList(list)) || (_MyItem == null ? false : _MyItem.IsDirtyList(list));
}
public override bool IsValid
{
get { return IsValidList(new List<object>()); }
return base.IsDirty || (_MyContent != null && _MyContent.IsDirtyList(list)) || (_MyItem != null && _MyItem.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) && (_MyContent == null ? true : _MyContent.IsValidList(list)) && (_MyItem == null ? true : _MyItem.IsValidList(list));
return ((IsNew && !IsDirty) || base.IsValid) && (_MyContent == null || _MyContent.IsValidList(list)) && (_MyItem == null || _MyItem.IsValidList(list));
}
// CSLATODO: Replace base Part.ToString function as necessary
/// <summary>
/// Overrides Base ToString
/// </summary>
/// <returns>A string representation of current Part</returns>
//public override string ToString()
//{
// return base.ToString();
//}
// CSLATODO: Check Part.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 Part</returns>
protected override object GetIdValue()
{
return MyPartUnique; // Absolutely Unique ID
}
protected override object GetIdValue() => MyPartUnique; // Absolutely Unique ID
#endregion
#region ValidationRules
[NonSerialized]
@@ -269,8 +248,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()
@@ -299,30 +278,11 @@ namespace VEPROMS.CSLA.Library
}
return true;
}
// 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(ContentID, "<Role(s)>");
//AuthorizationRules.AllowRead(FromType, "<Role(s)>");
//AuthorizationRules.AllowRead(ItemID, "<Role(s)>");
//AuthorizationRules.AllowRead(DTS, "<Role(s)>");
//AuthorizationRules.AllowRead(UserID, "<Role(s)>");
//AuthorizationRules.AllowWrite(ItemID, "<Role(s)>");
//AuthorizationRules.AllowWrite(DTS, "<Role(s)>");
//AuthorizationRules.AllowWrite(UserID, "<Role(s)>");
_PartExtension.AddAuthorizationRules(AuthorizationRules);
}
protected override void AddInstanceAuthorizationRules()
@@ -330,42 +290,14 @@ namespace VEPROMS.CSLA.Library
//CSLATODO: Who can read/write which fields
_PartExtension.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;
}
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 _PartUnique = 0;
protected static int PartUnique
{ get { return ++_PartUnique; } }
private int _MyPartUnique = PartUnique;
public int MyPartUnique // Absolutely Unique ID - Editable
{ get { return _MyPartUnique; } }
protected static int PartUnique => ++_PartUnique;
private readonly int _MyPartUnique = PartUnique;
// Absolutely Unique ID - Editable
public int MyPartUnique => _MyPartUnique;
protected Part()
{/* require use of factory methods */
AddToCache(this);
@@ -374,15 +306,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;
~Part()
{
_CountFinalized++;
@@ -407,8 +335,6 @@ namespace VEPROMS.CSLA.Library
}
public static Part New()
{
if (!CanAddObject())
throw new System.Security.SecurityException("User not authorized to add a Part");
try
{
return DataPortal.Create<Part>();
@@ -470,8 +396,6 @@ namespace VEPROMS.CSLA.Library
}
public static Part Get(int contentID, int fromType)
{
if (!CanGetObject())
throw new System.Security.SecurityException("User not authorized to view a Part");
try
{
Part tmp = GetCachedByPrimaryKey(contentID, fromType);
@@ -497,14 +421,9 @@ namespace VEPROMS.CSLA.Library
if (dr.Read()) return new Part(dr);
return null;
}
internal Part(SafeDataReader dr)
{
ReadData(dr);
}
internal Part(SafeDataReader dr) => ReadData(dr);
public static void Delete(int contentID, int fromType)
{
if (!CanDeleteObject())
throw new System.Security.SecurityException("User not authorized to remove a Part");
try
{
DataPortal.Delete(new PKCriteria(contentID, fromType));
@@ -516,12 +435,6 @@ namespace VEPROMS.CSLA.Library
}
public override Part Save()
{
if (IsDeleted && !CanDeleteObject())
throw new System.Security.SecurityException("User not authorized to remove a Part");
else if (IsNew && !CanAddObject())
throw new System.Security.SecurityException("User not authorized to add a Part");
else if (!CanEditObject())
throw new System.Security.SecurityException("User not authorized to update a Part");
try
{
BuildRefreshList();
@@ -541,12 +454,10 @@ namespace VEPROMS.CSLA.Library
[Serializable()]
protected class PKCriteria
{
private int _ContentID;
public int ContentID
{ get { return _ContentID; } }
private int _FromType;
public int FromType
{ get { return _FromType; } }
private readonly int _ContentID;
public int ContentID => _ContentID;
private readonly int _FromType;
public int FromType => _FromType;
public PKCriteria(int contentID, int fromType)
{
_ContentID = contentID;
@@ -649,32 +560,37 @@ namespace VEPROMS.CSLA.Library
[Transactional(TransactionalTypes.TransactionScope)]
internal void SQLInsert()
{
if (!this.IsDirty) return;
if (!IsDirty) return;
try
{
if (_MyContent != null) _MyContent.Update();
if (_MyItem != null) _MyItem.Update();
SqlConnection cn = (SqlConnection)ApplicationContext.LocalContext["cn"];
using (SqlCommand cm = cn.CreateCommand())
_MyContent?.Update();
_MyItem?.Update();
using (SqlConnection cn = (SqlConnection)ApplicationContext.LocalContext["cn"])
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandTimeout = Database.SQLTimeout;
cm.CommandText = "addPart";
// Input All Fields - Except Calculated Columns
cm.Parameters.AddWithValue("@ContentID", ContentID);
cm.Parameters.AddWithValue("@FromType", _FromType);
cm.Parameters.AddWithValue("@ItemID", ItemID);
if (_DTS.Year >= 1753 && _DTS.Year <= 9999) cm.Parameters.AddWithValue("@DTS", _DTS);
cm.Parameters.AddWithValue("@UserID", _UserID);
// 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 = "addPart";
// Input All Fields - Except Calculated Columns
cm.Parameters.AddWithValue("@ContentID", ContentID);
cm.Parameters.AddWithValue("@FromType", _FromType);
cm.Parameters.AddWithValue("@ItemID", ItemID);
if (_DTS.Year >= 1753 && _DTS.Year <= 9999) cm.Parameters.AddWithValue("@DTS", _DTS);
cm.Parameters.AddWithValue("@UserID", _UserID);
// 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();
// update child objects
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] Part.SQLInsert", GetHashCode());
@@ -704,8 +620,10 @@ namespace VEPROMS.CSLA.Library
if (dts.Year >= 1753 && dts.Year <= 9999) cm.Parameters.AddWithValue("@DTS", dts);
cm.Parameters.AddWithValue("@UserID", userID);
// 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();
@@ -749,33 +667,38 @@ namespace VEPROMS.CSLA.Library
if (_MyLog.IsDebugEnabled) _MyLog.DebugFormat("[{0}] Part.SQLUpdate", GetHashCode());
try
{
if (_MyContent != null) _MyContent.Update();
if (_MyItem != null) _MyItem.Update();
SqlConnection cn = (SqlConnection)ApplicationContext.LocalContext["cn"];
if (base.IsDirty)
_MyContent?.Update();
_MyItem?.Update();
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 = "updatePart";
// All Fields including Calculated Fields
cm.Parameters.AddWithValue("@ContentID", ContentID);
cm.Parameters.AddWithValue("@FromType", _FromType);
cm.Parameters.AddWithValue("@ItemID", ItemID);
if (_DTS.Year >= 1753 && _DTS.Year <= 9999) cm.Parameters.AddWithValue("@DTS", _DTS);
cm.Parameters.AddWithValue("@UserID", _UserID);
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 = "updatePart";
// All Fields including Calculated Fields
cm.Parameters.AddWithValue("@ContentID", ContentID);
cm.Parameters.AddWithValue("@FromType", _FromType);
cm.Parameters.AddWithValue("@ItemID", ItemID);
if (_DTS.Year >= 1753 && _DTS.Year <= 9999) cm.Parameters.AddWithValue("@DTS", _DTS);
cm.Parameters.AddWithValue("@UserID", _UserID);
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
}
@@ -788,14 +711,17 @@ 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 = Part.Add(cn, _MyContent, _FromType, _MyItem, _DTS, _UserID);
else
_LastChanged = Part.Update(cn, _ContentID, _FromType, _ItemID, _DTS, _UserID, ref _LastChanged);
using (SqlConnection cn = (SqlConnection)ApplicationContext.LocalContext["cn"])
{
if (IsNew)
_LastChanged = Part.Add(cn, _MyContent, _FromType, _MyItem, _DTS, _UserID);
else
_LastChanged = Part.Update(cn, _ContentID, _FromType, _ItemID, _DTS, _UserID, ref _LastChanged);
}
MarkOld();
}
}
@@ -818,8 +744,10 @@ namespace VEPROMS.CSLA.Library
cm.Parameters.AddWithValue("@UserID", userID);
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();
@@ -906,13 +834,10 @@ namespace VEPROMS.CSLA.Library
[Serializable()]
private class ExistsCommand : CommandBase
{
private int _ContentID;
private int _FromType;
private readonly int _ContentID;
private readonly int _FromType;
private bool _exists;
public bool Exists
{
get { return _exists; }
}
public bool Exists => _exists;
public ExistsCommand(int contentID, int fromType)
{
_ContentID = contentID;
@@ -948,7 +873,7 @@ namespace VEPROMS.CSLA.Library
#endregion
// Standard Default Code
#region extension
PartExtension _PartExtension = new PartExtension();
readonly PartExtension _PartExtension = new PartExtension();
[Serializable()]
partial class PartExtension : extensionBase
{
@@ -957,14 +882,8 @@ namespace VEPROMS.CSLA.Library
class extensionBase
{
// Default Values
public virtual DateTime DefaultDTS
{
get { return DateTime.Now; }
}
public virtual string DefaultUserID
{
get { return Volian.Base.Library.VlnSettings.UserID; }
}
public virtual DateTime DefaultDTS => DateTime.Now;
public virtual string DefaultUserID => Volian.Base.Library.VlnSettings.UserID;
// Authorization Rules
public virtual void AddAuthorizationRules(Csla.Security.AuthorizationRules rules)
{
@@ -993,57 +912,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 Part)
if (destType == typeof(string) && value is Part part)
{
// Return the ToString value
return ((Part)value).ToString();
return part.ToString();
}
return base.ConvertTo(context, culture, value, destType);
}
}
#endregion
} // Namespace
//// The following is a sample Extension File. You can use it to create PartExt.cs
//using System;
//using System.Collections.Generic;
//using System.Text;
//using Csla;
//namespace VEPROMS.CSLA.Library
//{
// public partial class Part
// {
// partial class PartExtension : extensionBase
// {
// // CSLATODO: Override automatic defaults
// public virtual DateTime DefaultDTS
// {
// get { return DateTime.Now; }
// }
// public virtual string DefaultUserID
// {
// 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 */);
// }
// }
// }
//}