📄 businesscollectionbase.cs
字号:
/// Defines a strongly-typed collection to store all
/// child objects marked for deletion.
/// </summary>
[Serializable()]
protected class DeletedCollection : CollectionBase
{
/// <summary>
/// Adds a child object to the collection.
/// </summary>
/// <param name="Child">The child object to be added.</param>
public void Add(BusinessBase child)
{
List.Add(child);
}
/// <summary>
/// Removes a child object from the collection.
/// </summary>
/// <param name="Child">The child object to be removed.</param>
public void Remove(BusinessBase child)
{
List.Remove(child);
}
/// <summary>
/// Returns a reference to a child object in the collection.
/// </summary>
/// <param name="index">The positional index of the item in the collection.</param>
/// <returns>The specified child object.</returns>
public BusinessBase this [int index]
{
get
{
return (BusinessBase)List[index];
}
}
}
#endregion
#region Insert, Remove, Clear
/// <summary>
/// This method is called by a child object when it
/// wants to be removed from the collection.
/// </summary>
/// <param name="child">The child object to remove.</param>
internal void RemoveChild(BusinessBase child)
{
List.Remove(child);
}
/// <summary>
/// Sets the edit level of the child object as it is added.
/// </summary>
protected override void OnInsert(int index, object val)
{
if (!ActivelySorting)
{
// when an object is inserted we assume it is
// a new object and so the edit level when it was
// added must be set
((BusinessBase)val).EditLevelAdded = _editLevel;
((BusinessBase)val).SetParent(this);
base.OnInsert(index, val);
}
}
/// <summary>
/// Marks the child object for deletion and moves it to
/// the collection of deleted objects.
/// </summary>
protected override void OnRemove(int index, object val)
{
if (!ActivelySorting)
{
// when an object is 'removed' it is really
// being deleted, so do the deletion work
DeleteChild((BusinessBase)val);
base.OnRemove(index, val);
}
}
/// <summary>
/// Marks all child objects for deletion and moves them
/// to the collection of deleted objects.
/// </summary>
protected override void OnClear()
{
if (!ActivelySorting)
{
// remove all the items from the list
while(List.Count > 0)
List.RemoveAt(List.Count - 1);
base.OnClear();
}
}
#endregion
#region Edit level tracking
// keep track of how many edit levels we have
int _editLevel;
#endregion
#region IsChild
[NotUndoable()]
bool _IsChild = false;
/// <summary>
/// Indicates whether this collection object is a child object.
/// </summary>
/// <returns>True if this is a child object.</returns>
protected bool IsChild
{
get
{
return _IsChild;
}
}
/// <summary>
/// Marks the object as being a child object.
/// </summary>
/// <remarks>
/// <para>
/// By default all business objects are 'parent' objects. This means
/// that they can be directly retrieved and updated into the database.
/// </para><para>
/// We often also need child objects. These are objects which are contained
/// within other objects. For instance, a parent Invoice object will contain
/// child LineItem objects.
/// </para><para>
/// To create a child object, the MarkAsChild method must be called as the
/// object is created. Please see Chapter 7 for details on the use of the
/// MarkAsChild method.
/// </para>
/// </remarks>
protected void MarkAsChild()
{
_IsChild = true;
}
#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 Data Access
/// <summary>
/// Saves the object to the database.
/// </summary>
/// <remarks>
/// <para>
/// Calling this method starts the save operation, causing the all child
/// objects to be inserted, updated or deleted within the database based on the
/// each object's current state.
/// </para><para>
/// All this is contingent on <see cref="P:CSLA.BusinessCollectionBase.IsDirty" />. If
/// this value is False, no data operation occurs. It is also contingent on
/// <see cref="P:CSLA.BusinessCollectionBase.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 collection that contains any data updated during the save operation.
/// You MUST update all object references to use this new version of the
/// business collection 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 BusinessCollectionBase Save()
{
if(this.IsChild)
throw new NotSupportedException(Strings.GetResourceString("NoSaveChildException"));
if(_editLevel > 0)
throw new Exception(Strings.GetResourceString("NoSaveEditingException"));
if(!IsValid)
throw new Exception(Strings.GetResourceString("NoSaveInvalidException"));
if(IsDirty)
return (BusinessCollectionBase)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 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()
{
foreach(Serialization.ISerializationNotification child in List)
child.Deserialized();
foreach(Serialization.ISerializationNotification child in deletedList)
child.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()
{
foreach(Serialization.ISerializationNotification child in List)
child.Serialized();
foreach(Serialization.ISerializationNotification child in deletedList)
child.Serialized();
}
void Serialization.ISerializationNotification.Serializing()
{
Serializing();
}
/// <summary>
/// This method is called before an object is serialized.
/// </summary>
protected virtual void Serializing()
{
foreach(Serialization.ISerializationNotification child in List)
child.Serializing();
foreach(Serialization.ISerializationNotification child in deletedList)
child.Serializing();
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -