KGy SOFT

EditableObjectBase Class

KGy SOFT Core Libraries Help
Represents an object with editing capabilities by adding ICanEdit implementation to the PersistableObjectBase class. Starting an edit session saves a snapshot of the stored properties, which can be either applied or reverted. Saving and restoring properties works for properties set through the IPersistableObject implementation and the ObservableObjectBase.Set method.
See the Remarks section for details.
Inheritance Hierarchy

SystemObject
  KGySoft.ComponentModelObservableObjectBase
    KGySoft.ComponentModelPersistableObjectBase
      KGySoft.ComponentModelEditableObjectBase

Namespace:  KGySoft.ComponentModel
Assembly:  KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 5.0.0-rc.1
Syntax

public abstract class EditableObjectBase : PersistableObjectBase, 
	ICanEdit, IEditableObject

The EditableObjectBase type exposes the following members.

Constructors

  NameDescription
Protected methodEditableObjectBase
Initializes a new instance of the EditableObjectBase class
Top
Properties

  NameDescription
Protected propertyEditableObjectBehavior
Gets how the object should behave if treated as an IEditableObject.
The base implementation returns DisableNesting.
Public propertyEditLevel
Gets the editing level. That is, the number of BeginNewEdit calls without corresponding CommitLastEdit or RevertLastEdit calls.
Public propertyIsModified
Gets whether this instance has been modified. Modified state can be set by the SetModified method.
(Inherited from ObservableObjectBase.)
Top
Methods

  NameDescription
Protected methodAffectsModifiedState
Gets whether the change of the specified propertyName affects the IsModified property.
The EditableObjectBase implementation excludes the IsModified and EditLevel properties.
(Overrides ObservableObjectBaseAffectsModifiedState(String).)
Public methodBeginNewEdit
Begins a new level of committable/revertible editing session on the object.
Protected methodCanGetProperty
Gets whether the specified property can be get.
The base implementation allows to get the actual instance properties in this instance.
(Inherited from ObservableObjectBase.)
Protected methodCanSetProperty
Gets whether the specified property can be set.
The base implementation allows to set the actual instance properties in this instance if the specified value is compatible with the property type.
(Inherited from ObservableObjectBase.)
Public methodClone
Creates a new object that is a copy of the current instance.
The base implementation clones the internal property storage, the IsModified property and if clonePropertyChanged is , then also the subscribers of the PropertyChanged event.
(Inherited from ObservableObjectBase.)
Public methodCommitLastEdit
Commits all changes since the last BeginNewEdit call.
Public methodDispose
Releases the resources held by this instance.
(Inherited from ObservableObjectBase.)
Protected methodDispose(Boolean)
Releases the resources held by this instance.
The base implementation removes the subscribers of the PropertyChanged event.
(Inherited from ObservableObjectBase.)
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Protected methodGetT(FuncT, String)
Gets the value of a property, or - if it was not set before -, then creates its initial value. The created initial value will be stored in the internal property storage without triggering the PropertyChanged event. For constant or simple expressions, or to return a default value for a non-existing property without storing it internally use the other Get overload.
For an example, see the Remarks section of the ObservableObjectBase class.
(Inherited from ObservableObjectBase.)
Protected methodGetT(T, String)
Gets the value of a property or defaultValue if no value is stored for it. No new value will be stored if the property does not exist. If the default initial value is too complex and should not be evaluated every time when the property is get, or to throw an exception for an uninitialized property use the other Get overload.
For an example, see the Remarks section of the ObservableObjectBase class.
(Inherited from ObservableObjectBase.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Protected methodOnPropertyChanged
Raises the PropertyChanged event.
(Inherited from ObservableObjectBase.)
Protected methodResetProperty
Resets the property of the specified name, meaning, it will be removed from the underlying storage so the getter methods will return the default value again.
(Inherited from ObservableObjectBase.)
Protected methodResumeChangedEvent
Resumes the raising of the PropertyChanged event suspended by the SuspendChangeEvents method.
(Inherited from ObservableObjectBase.)
Public methodRevertLastEdit
Discards all changes since the last BeginNewEdit call.
Protected methodSet
Sets the value of a property.
For an example, see the Remarks section of the ObservableObjectBase class.
(Inherited from ObservableObjectBase.)
Public methodSetModified
Sets the modified state of this ObservableObjectBase instance represented by the IsModified property.
(Inherited from ObservableObjectBase.)
Protected methodSuspendChangedEvent
Suspends the raising of the PropertyChanged event until ResumeChangeEvents method is called. Supports nested calls.
(Inherited from ObservableObjectBase.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Public methodTryCommitAllEdits
Commits all changes of all editing levels.
Public methodTryRevertAllEdits
Reverts all changes of all editing levels.
Top
Events

  NameDescription
Public eventPropertyChanged
Occurs when a property value changed. The actual type of the event argument is PropertyChangedExtendedEventArgs.
(Inherited from ObservableObjectBase.)
Top
Extension Methods

  NameDescription
Public Extension MethodConvert(Type, CultureInfo)Overloaded.
Converts an Object specified in the obj parameter to the desired targetType.
See the Examples section of the generic ConvertTTarget(Object, CultureInfo) overload for an example.
(Defined by ObjectExtensions.)
Public Extension MethodCode exampleConvertTTarget(CultureInfo)Overloaded.
Converts an Object specified in the obj parameter to the desired TTarget.
(Defined by ObjectExtensions.)
Public Extension MethodIn (Defined by ObjectExtensions.)
Public Extension MethodTryConvert(Type, Object)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired targetType.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvert(Type, CultureInfo, Object)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired targetType.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvertTTarget(TTarget)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvertTTarget(CultureInfo, TTarget)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions.)
Top
Remarks

An object derived from EditableObjectBase is able to save a snapshot of its properties (the ones, which are set through the IPersistableObject implementation or the ObservableObjectBase.Set method).

A new snapshot can be saved by the BeginNewEdit method. This can be called by a UI before starting the editing.

Call the CommitLastEdit method to apply the changes since the last BeginNewEdit call.

Call the RevertLastEdit method to discard the changes since the last BeginNewEdit call.

The editing sessions can be nested by calling BeginNewEdit method multiple times. The number of the BeginNewEdit calls without a corresponding CommitLastEdit or RevertLastEdit call is indicated by the EditLevel property.

By calling the TryCommitAllEdits and TryRevertAllEdits methods all of the previous BeginNewEdit calls can be applied or discarded, respectively. These methods return a bool value indicating whether any action occurred.

Note Note
When it is needed to be determined whether a type has editing capabilities use the ICanEdit interface instead the EditableObjectBase type because other editable types, such as the ModelBase class are not necessarily derived from the EditableObjectBase class. See also the class diagram of the business object base classes of the KGySoft.CoreLibraries assembly:
Class diagram of business object base classes

Differences from UndoableObjectBase:
Both UndoableObjectBase and EditableObjectBase can revert changes; however, the aspects of these classes are different.

  • An undoable class (which implements ICanUndo or ICanUndoRedo interfaces such as UndoableObjectBase) is able to undo (or redo) any changes made so far either step-by-step or in a single step.
  • On the other hand, an editable class (which implements ICanEdit such as EditableObjectBase) is able to start editing sessions by saving a snapshot of its current state, which states are committable and revertible.
  • Undo and editing features are independent from each other and a class is allowed to implement both (like the ModelBase class).

IEditableObject support:
EditableObjectBase implements also the System.ComponentModel.IEditableObject interface, which is the standard way in .NET to support editing. Several controls of different UI frameworks automatically call its members if an object, which implements it, is bound to a grid control, for example. However, some frameworks (such as Windows Forms) do not always call the begin/end operations the same times, which can cause problems. To handle this, you can override the EditableObjectBehavior property in your class to adjust the editing behavior via the IEditableObject interface. If not overridden, the EditableObjectBehavior returns DisableNesting, which works in most cases.
This is how the IEditableObject members are mapped in the EditableObjectBase class:

Note Notes to Implementers
For an example see the Remarks section of the ObservableObjectBase class. The same applies also for the EditableObjectBase class in terms of implementation.
See Also

Reference