📄 businessbase.cs
字号:
if(this.IsChild)
throw new NotSupportedException(Strings.GetResourceString("ChildDeleteException"));
MarkDeleted();
}
// allow the parent object to delete us
// (internal scope)
internal void DeleteChild()
{
if(!this.IsChild)
throw new NotSupportedException(Strings.GetResourceString("NoDeleteRootException"));
MarkDeleted();
}
#endregion
#region Edit Level Tracking (child only)
// we need to keep track of the edit
// level when we were added so if the user
// cancels below that level we can be destroyed
int _editLevelAdded;
// allow the collection object to use the
// edit level as needed (internal scope)
internal int EditLevelAdded
{
get
{
return _editLevelAdded;
}
set
{
_editLevelAdded = value;
}
}
#endregion
#region ICloneable
/// <summary>
/// Creates a clone of the object.
/// </summary>
/// <returns>A new object containing the exact data of the original object.</returns>
public object Clone()
{
MemoryStream buffer = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
Serialization.SerializationNotification.OnSerializing(this);
formatter.Serialize(buffer, this);
Serialization.SerializationNotification.OnSerialized(this);
buffer.Position = 0;
object temp = formatter.Deserialize(buffer);
Serialization.SerializationNotification.OnDeserialized(temp);
return temp;
}
#endregion
#region BrokenRules, IsValid
// keep a list of broken rules
BrokenRules _brokenRules = new BrokenRules();
/// <summary>
/// Override this method in your business class to
/// be notified when you need to set up business
/// rules.
/// </summary>
/// <remarks>
/// You should call AddBusinessRules from your object's
/// constructor methods so the rules are set up when
/// your object is created. This method will be automatically
/// called, if needed, when your object is serialized by
/// the DataPortal or by the Clone method.
/// </remarks>
protected internal virtual void AddBusinessRules()
{
}
/// <summary>
/// Returns True if the object is currently valid, False if the
/// object has broken rules or is otherwise invalid.
/// </summary>
/// <remarks>
/// <para>
/// By default this property relies on the underling <see cref="T:CSLA.BrokenRules" />
/// object to track whether any business rules are currently broken for this object.
/// </para><para>
/// You can override this property to provide more sophisticated
/// implementations of the behavior. For instance, you should always override
/// this method if your object has child objects, since the validity of this object
/// is affected by the validity of all child objects.
/// </para>
/// </remarks>
/// <returns>A value indicating if the object is currently valid.</returns>
virtual public bool IsValid
{
get
{
return _brokenRules.IsValid;
}
}
/// <summary>
/// Provides access to the readonly collection of broken business rules
/// for this object.
/// </summary>
/// <returns>A <see cref="T:CSLA.BrokenRules.RulesCollection" /> object.</returns>
public virtual BrokenRules.RulesCollection GetBrokenRulesCollection()
{
return _brokenRules.BrokenRulesCollection;
}
/// <summary>
/// Provides access to the readonly collection of broken business rules
/// for this object.
/// </summary>
/// <returns>A <see cref="T:CSLA.BrokenRules.RulesCollection" /> object.</returns>
public BrokenRules.RulesCollection BrokenRulesCollection
{
get
{
return _brokenRules.BrokenRulesCollection;
}
}
/// <summary>
/// Provides access to a text representation of all the descriptions of
/// the currently broken business rules for this object.
/// </summary>
/// <returns>Text containing the descriptions of the broken business rules.</returns>
public virtual string GetBrokenRulesString()
{
return _brokenRules.ToString();
}
/// <summary>
/// Provides access to a text representation of all the descriptions of
/// the currently broken business rules for this object.
/// </summary>
/// <returns>Text containing the descriptions of the broken business rules.</returns>
public string BrokenRulesString
{
get
{
return _brokenRules.ToString();
}
}
/// <summary>
/// Provides access to the broken rules functionality.
/// </summary>
/// <remarks>
/// This property is used within your business logic so you can
/// easily call the
/// <see cref="M:CSLA.BrokenRules.Assert(System.String,System.String,System.Boolean)" />
/// method to mark rules as broken and unbroken.
/// </remarks>
protected BrokenRules BrokenRules
{
get
{
return _brokenRules;
}
}
#endregion
#region Data Access
/// <summary>
/// Saves the object to the database.
/// </summary>
/// <remarks>
/// <para>
/// Calling this method starts the save operation, causing the object
/// to be inserted, updated or deleted within the database based on the
/// object's current state.
/// </para><para>
/// If <see cref="P:CSLA.BusinessBase.IsDeleted" /> is True the object
/// will be deleted. Otherwise, if <see cref="P:CSLA.BusinessBase.IsNew" />
/// is True the object will be inserted. Otherwise the object's data will
/// be updated in the database.
/// </para><para>
/// All this is contingent on <see cref="P:CSLA.BusinessBase.IsDirty" />. If
/// this value is False, no data operation occurs. It is also contingent on
/// <see cref="P:CSLA.BusinessBase.IsValid" />. If this value is False an
/// exception will be thrown to indicate that the UI attempted to save an
/// invalid object.
/// </para><para>
/// It is important to note that this method returns a new version of the
/// business object that contains any data updated during the save operation.
/// You MUST update all object references to use this new version of the
/// business object in order to have access to the correct object data.
/// </para><para>
/// You can override this method to add your own custom behaviors to the save
/// operation. For instance, you may add some security checks to make sure
/// the user can save the object. If all security checks pass, you would then
/// invoke the base Save method via <c>MyBase.Save()</c>.
/// </para>
/// </remarks>
/// <returns>A new object containing the saved values.</returns>
virtual public BusinessBase Save()
{
if(IsChild)
throw new NotSupportedException(Strings.GetResourceString("NoSaveChildException"));
if(EditLevel > 0)
throw new ApplicationException(Strings.GetResourceString("NoSaveEditingException"));
if(!IsValid)
throw new ValidationException(Strings.GetResourceString("NoSaveInvalidException"));
if(IsDirty)
return (BusinessBase)DataPortal.Update(this);
else
return this;
}
/// <summary>
/// Override this method to load a new business object with default
/// values from the database.
/// </summary>
/// <param name="criteria">An object containing criteria values.</param>
virtual protected void DataPortal_Create(object criteria)
{
throw new NotSupportedException(Strings.GetResourceString("CreateNotSupportedException"));
}
/// <summary>
/// Override this method to allow retrieval of an existing business
/// object based on data in the database.
/// </summary>
/// <param name="criteria">
/// An object containing criteria values to identify the object.</param>
virtual protected void DataPortal_Fetch(object criteria)
{
throw new NotSupportedException(Strings.GetResourceString("FetchNotSupportedException"));
}
/// <summary>
/// Override this method to allow insert, update or deletion of a business
/// object.
/// </summary>
virtual protected void DataPortal_Update()
{
throw new NotSupportedException(Strings.GetResourceString("UpdateNotSupportedException"));
}
/// <summary>
/// Override this method to allow immediate deletion of a business object.
/// </summary>
/// <param name="criteria">
/// An object containing criteria values to identify the object.</param>
virtual protected void DataPortal_Delete(object criteria)
{
throw new NotSupportedException(Strings.GetResourceString("DeleteNotSupportedException"));
}
/// <summary>
/// Called by the server-side DataPortal prior to calling the
/// requested DataPortal_xyz method.
/// </summary>
/// <param name="e">The DataPortalContext object passed to the DataPortal.</param>
protected virtual void DataPortal_OnDataPortalInvoke(DataPortalEventArgs e)
{
}
/// <summary>
/// Called by the server-side DataPortal after calling the
/// requested DataPortal_xyz method.
/// </summary>
/// <param name="e">The DataPortalContext object passed to the DataPortal.</param>
protected virtual void DataPortal_OnDataPortalInvokeComplete(DataPortalEventArgs e)
{
}
/// <summary>
/// Returns the specified database connection string from the application
/// configuration file.
/// </summary>
/// <remarks>
/// The database connection string must be in the <c>appSettings</c> section
/// of the application configuration file. The database name should be
/// prefixed with 'DB:'. For instance, <c>DB:mydatabase</c>.
/// </remarks>
/// <param name="databaseName">Name of the database.</param>
/// <returns>A database connection string.</returns>
protected string DB(string databaseName)
{
return ConfigurationSettings.AppSettings["DB:" + databaseName];
}
#endregion
#region IDataErrorInfo
string IDataErrorInfo.Error
{
get
{
if(!IsValid)
{
if(BrokenRules.BrokenRulesCollection.Count == 1)
return BrokenRules.BrokenRulesCollection[0].Description;
else
return BrokenRules.ToString();
}
else
return string.Empty;
}
}
string IDataErrorInfo.this [string columnName]
{
get
{
if(!IsValid)
return BrokenRules.BrokenRulesCollection.RuleForProperty(columnName).Description;
else
return string.Empty;
}
}
#endregion
#region ISerializationNotification
void Serialization.ISerializationNotification.Deserialized()
{
Deserialized();
}
/// <summary>
/// This method is called on a newly deserialized object
/// after deserialization is complete.
/// </summary>
protected virtual void Deserialized()
{
AddBusinessRules();
// now cascade the call to all child objects/collections
FieldInfo [] fields;
// get the list of fields in this type
fields = this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
foreach(FieldInfo field in fields)
if(!field.FieldType.IsValueType && !Attribute.IsDefined(field, typeof(NotUndoableAttribute)))
{
// it's a ref type, so check for ISerializationNotification
object value = field.GetValue(this);
if(value is Serialization.ISerializationNotification)
((Serialization.ISerializationNotification)value).Deserialized();
}
}
void Serialization.ISerializationNotification.Serialized()
{
Serialized();
}
/// <summary>
/// This method is called on the original instance of the
/// object after it has been serialized.
/// </summary>
protected virtual void Serialized()
{
// cascade the call to all child objects/collections
FieldInfo [] fields;
// get the list of fields in this type
fields = this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
foreach(FieldInfo field in fields)
if(!field.FieldType.IsValueType && !Attribute.IsDefined(field, typeof(NotUndoableAttribute)))
{
// it's a ref type, so check for ISerializationNotification
object value = field.GetValue(this);
if(value is Serialization.ISerializationNotification)
((Serialization.ISerializationNotification)value).Serialized();
}
}
void Serialization.ISerializationNotification.Serializing()
{
Serializing();
}
/// <summary>
/// This method is called before an object is serialized.
/// </summary>
protected virtual void Serializing()
{
// cascade the call to all child objects/collections
FieldInfo [] fields;
// get the list of fields in this type
fields = this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
foreach(FieldInfo field in fields)
if(!field.FieldType.IsValueType && !Attribute.IsDefined(field, typeof(NotUndoableAttribute)))
{
// it's a ref type, so check for ISerializationNotification
object value = field.GetValue(this);
if(value is Serialization.ISerializationNotification)
((Serialization.ISerializationNotification)value).Serializing();
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -