0 out of 3 rated this helpful - Rate this topic

IEditableObject Interface

Provides functionality to commit or rollback changes to an object that is used as a data source.

Namespace:  System.ComponentModel
Assembly:  System (in System.dll)
public interface IEditableObject

The IEditableObject type exposes the following members.

  Name Description
Public method Supported by the XNA Framework Supported by Portable Class Library BeginEdit Begins an edit on an object.
Public method Supported by the XNA Framework Supported by Portable Class Library CancelEdit Discards changes since the last BeginEdit call.
Public method Supported by the XNA Framework Supported by Portable Class Library EndEdit Pushes changes since the last BeginEdit or IBindingList.AddNew call into the underlying object.
Top

This interface is typically used to capture the BeginEdit, EndEdit, and CancelEdit semantics of a DataRowView.

The following sample provides a simple implementation of the IEditableObject interface. The Customer class stores customer information and can be used as a collection for a customer database. This sample assumes that you have used the CustomerList class that can be found in sample in the IBindingList class.


	public class Customer : IEditableObject 
	{

		struct CustomerData 
		{
			internal string id ;
			internal string firstName ;
			internal string lastName ;
		}

		private CustomersList parent;
		private CustomerData custData; 
		private CustomerData backupData; 
		private bool inTxn = false;

		// Implements IEditableObject
		void IEditableObject.BeginEdit() 
		{
			Console.WriteLine("Start BeginEdit");
			if (!inTxn) 
			{
				this.backupData = custData;
				inTxn = true;
				Console.WriteLine("BeginEdit - " + this.backupData.lastName);
			}
			Console.WriteLine("End BeginEdit");
		}

		void IEditableObject.CancelEdit() 
		{
			Console.WriteLine("Start CancelEdit");
			if (inTxn) 
			{
				this.custData = backupData;
				inTxn = false;
				Console.WriteLine("CancelEdit - " + this.custData.lastName);
			}
			Console.WriteLine("End CancelEdit");
		}

		void IEditableObject.EndEdit() 
		{
			Console.WriteLine("Start EndEdit" + this.custData.id + this.custData.lastName);
			if (inTxn) 
			{
				backupData = new CustomerData();
				inTxn = false;
				Console.WriteLine("Done EndEdit - " + this.custData.id + this.custData.lastName);
			}
			Console.WriteLine("End EndEdit");
		}

		public Customer(string ID) : base() 
		{
			this.custData = new CustomerData();
			this.custData.id = ID;
			this.custData.firstName = "";
			this.custData.lastName = "";
		}

		public string ID 
		{
			get 
			{
				return this.custData.id;
			}
		}

		public string FirstName 
		{
			get 
			{
				return this.custData.firstName;
			}
			set 
			{
				this.custData.firstName = value;
                this.OnCustomerChanged();
			}
		}

		public string LastName 
		{
			get 
			{
				return this.custData.lastName;
			}
			set 
			{
				this.custData.lastName = value;
                this.OnCustomerChanged();
			}
		}

		internal CustomersList Parent 
		{
			get 
			{
				return parent;
			}
			set 
			{
				parent = value ;
			}
		}

		private void OnCustomerChanged() 
		{
			if (!inTxn && Parent != null) 
			{
				Parent.CustomerChanged(this);
			}
		}
		
		public override string ToString() 
		{
			StringWriter sb = new StringWriter();
			sb.Write(this.FirstName);
			sb.Write(" ");
			sb.Write(this.LastName);
			return sb.ToString();
		}   
	}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
inTxn / comments
Adding some comments to the source would be helpful.  For example  inTxn  is probably used to say that the customer record is in a transaction and so should not be edited; but that's only apparent from reading through the code.   $0$0 $0I'm also unclear on whether that variable prevents updates, or if it just prevents their persistence (i.e. if an object receives two updates in quick succession, the first takes the isTxn "lock"; does the second fail, or is it somehow queued & completed later?  If it fails, what would a user see - there's no exceptions being thrown, so are they alerted, or does the system just ignore the second request?$0 $0
Editable example
Imports System.ComponentModel
Imports Publisher
Public Class EditController
    Implements INotifyPropertyChanged
    Private _author As BL.Author
    Public Property Author() As BL.Author
        Get
            Return _author
        End Get
        Set(ByVal value As BL.Author)
            _author = value
            _author.BeginEdit()
            OnPropertyChanged("Author")
        End Set
    End Property
    Public Sub save()
        Author.EndEdit()
        'database update
        'BL.Author.save(_author)
    End Sub
    Public Sub cancel()
        Author.CancelEdit()
    End Sub
    Protected Overridable Sub OnPropertyChanged(ByVal propertyname As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyname))
    End Sub
    Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class


Imports System.ComponentModel
Public Class Author
Implements INotifyPropertyChanged
Implements IEditableObject
Private _firstname As String
Public Property Firstname() As String
Get
Return _firstname
End Get
Set(ByVal value As String)
_firstname = value
OnPropertyChanged("Firstname")
End Set
End Property
 
Private _lastname As String
Public Property Lastname() As String
Get
Return _lastname
End Get
Set(ByVal value As String)
_lastname = value
OnPropertyChanged("Lastname")
End Set
End Property
Public Sub New(ByVal fname As String, ByVal lname As String)
_firstname = fname
_lastname = lname
End Sub
 
Private _auID As String
Public Property auID() As String
Get
Return _auID
End Get
Set(ByVal value As String)
_auID = value
End Set
End Property
Protected Overridable Sub OnPropertyChanged(ByVal propertyname As String)
If _raisePropertyChanged Then
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyname))
End If
End Sub
Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
Private _raisePropertyChanged As Boolean = True
Private _oldFirstname As String
Private _oldLastname As String
Private _oldAddress As String
Private _oldState As String
Private _oldZip As String
Private _oldPhone As String
Private _oldContract As String
 
Public Sub BeginEdit() Implements System.ComponentModel.IEditableObject.BeginEdit
If _raisePropertyChanged Then
_raisePropertyChanged = False
_oldFirstname = Firstname
_oldLastname = Lastname
End If
End Sub
Public Sub CancelEdit() Implements System.ComponentModel.IEditableObject.CancelEdit
If _raisePropertyChanged.Equals(False) Then
_firstname = _oldFirstname
_lastname = _oldLastname
End If
End Sub
Public Sub EndEdit() Implements System.ComponentModel.IEditableObject.EndEdit
If Not _raisePropertyChanged Then
_raisePropertyChanged = True
_oldAddress = Nothing
_oldContract = Nothing
_oldFirstname = Nothing
_oldLastname = Nothing
_oldPhone = Nothing
_oldState = Nothing
_oldZip = Nothing
OnPropertyChanged("Firstname")
OnPropertyChanged("Lastname")
End If
End Sub
End Class
Good exmaple
I am not entirely sure what the whole parent thing is doing nor the collection that they use.  I was able to get a simple class operational.  Might not be a bad idea to change the verbage on the two private declarations of the structure from custData to workingCopy and backupData to backupCopy or something.  Not a big deal, but it helps not to see the cust prefix keyword when you are learning this stuff.