다음을 통해 공유


IEditableObject 인터페이스

정의

데이터 소스로 사용되는 개체에 대한 변경 내용을 커밋하거나 롤백하는 기능을 제공합니다.

public interface class IEditableObject
public interface IEditableObject
type IEditableObject = interface
Public Interface IEditableObject
파생

예제

다음 샘플에서는 인터페이스의 간단한 구현을 IEditableObject 제공합니다. 클래스는 Customer 고객 정보를 저장하고 고객 데이터베이스의 컬렉션으로 사용할 수 있습니다. 이 샘플에서는 클래스의 CustomerList 샘플에서 찾을 수 있는 클래스를 IBindingList 사용했다고 가정합니다.

public ref class Customer: public IEditableObject
{
private:
   ref struct CustomerData
   {
   internal:
      String^ id;
      String^ firstName;
      String^ lastName;
   };

internal:
   CustomersList^ parent;
   CustomerData^ custData;
   CustomerData^ backupData;
   bool inTxn;

   // Implements IEditableObject
public:
   virtual void BeginEdit()
   {
      Console::WriteLine( "Start BeginEdit" );
      if (  !inTxn )
      {
         this->backupData = custData;
         inTxn = true;
         Console::WriteLine( "BeginEdit - {0}", this->backupData->lastName );
      }

      Console::WriteLine( "End BeginEdit" );
   }

   virtual void CancelEdit()
   {
      Console::WriteLine( "Start CancelEdit" );
      if ( inTxn )
      {
         this->custData = backupData;
         inTxn = false;
         Console::WriteLine( "CancelEdit - {0}", this->custData->lastName );
      }

      Console::WriteLine( "End CancelEdit" );
   }

   virtual void EndEdit()
   {
      Console::WriteLine( "Start EndEdit{0}{1}", this->custData->id, this->custData->lastName );
      if ( inTxn )
      {
         backupData = gcnew CustomerData;
         inTxn = false;
         Console::WriteLine( "Done EndEdit - {0}{1}", this->custData->id, this->custData->lastName );
      }

      Console::WriteLine( "End EndEdit" );
   }


public:

   Customer( String^ ID )
   {
      this->custData = gcnew CustomerData;
      this->custData->id = ID;
      this->custData->firstName = "";
      this->custData->lastName = "";
      inTxn = false;
   }

   property String^ ID 
   {
      String^ get()
      {
         return this->custData->id;
      }

   }

   property String^ FirstName 
   {
      String^ get()
      {
         return this->custData->firstName;
      }

      void set( String^ value )
      {
         this->custData->firstName = value;
         this->OnCustomerChanged();
      }

   }

   property String^ LastName 
   {
      String^ get()
      {
         return this->custData->lastName;
      }

      void set( String^ value )
      {
         this->custData->lastName = value;
         this->OnCustomerChanged();
      }

   }

internal:

   property CustomersList^ Parent 
   {
      CustomersList^ get()
      {
         return parent;
      }

      void set( CustomersList^ value )
      {
         parent = value;
      }

   }

   void OnCustomerChanged()
   {
      if (!inTxn && Parent != nullptr)
      {
          Parent->CustomerChanged(this);
      }
   }

public:
   virtual String^ ToString() override
   {
      StringWriter^ sb = gcnew StringWriter;
      sb->Write( this->FirstName );
      sb->Write( " " );
      sb->Write( this->LastName );
      return sb->ToString();
   }
};
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();
    }
}
Public Class Customer
    Implements IEditableObject

    Structure CustomerData
        Friend id As String
        Friend firstName As String
        Friend lastName As String
    End Structure 

    Public parent As CustomersList
    Private custData As CustomerData
    Private backupData As CustomerData
    Private inTxn As Boolean = False


    ' Implements IEditableObject
    Sub BeginEdit() Implements IEditableObject.BeginEdit
        Console.WriteLine("Start BeginEdit")
        If Not inTxn Then
            Me.backupData = custData
            inTxn = True
            Console.WriteLine(("BeginEdit - " + Me.backupData.lastName))
        End If
        Console.WriteLine("End BeginEdit")
    End Sub 


    Sub CancelEdit() Implements IEditableObject.CancelEdit
        Console.WriteLine("Start CancelEdit")
        If inTxn Then
            Me.custData = backupData
            inTxn = False
            Console.WriteLine(("CancelEdit - " + Me.custData.lastName))
        End If
        Console.WriteLine("End CancelEdit")
    End Sub 


    Sub EndEdit() Implements IEditableObject.EndEdit
        Console.WriteLine(("Start EndEdit" + Me.custData.id + Me.custData.lastName))
        If inTxn Then
            backupData = New CustomerData()
            inTxn = False
            Console.WriteLine(("Done EndEdit - " + Me.custData.id + Me.custData.lastName))
        End If
        Console.WriteLine("End EndEdit")
    End Sub 


    Public Sub New(ByVal ID As String)
        Me.custData = New CustomerData()
        Me.custData.id = ID
        Me.custData.firstName = ""
        Me.custData.lastName = ""
    End Sub 


    Public ReadOnly Property ID() As String
        Get
            Return Me.custData.id
        End Get
    End Property


    Public Property FirstName() As String
        Get
            Return Me.custData.firstName
        End Get
        Set(ByVal Value As String)
            Me.custData.firstName = Value
            Me.OnCustomerChanged()
        End Set
    End Property


    Public Property LastName() As String
        Get
            Return Me.custData.lastName
        End Get
        Set(ByVal Value As String)
            Me.custData.lastName = Value
            Me.OnCustomerChanged()
        End Set
    End Property


    Friend Property Parents() As CustomersList
        Get
            Return Parent
        End Get
        Set(ByVal Value As CustomersList)
            parent = Value
        End Set
    End Property


    Private Sub OnCustomerChanged()
        If Not inTxn And (Parent IsNot Nothing) Then
            Parent.CustomerChanged(Me)
        End If
    End Sub 

    
    Public Overrides Function ToString() As String
        Dim sb As New StringWriter()
        sb.Write(Me.FirstName)
        sb.Write(" ")
        sb.Write(Me.LastName)
        Return sb.ToString()
    End Function 
End Class

설명

이 인터페이스는 일반적으로 의 DataRowView, EndEditCancelEdit 의미 체계를 캡처BeginEdit하는 데 사용됩니다.

메서드

BeginEdit()

개체에 대한 편집을 시작합니다.

CancelEdit()

마지막으로 BeginEdit()가 호출된 이후 변경된 내용을 취소합니다.

EndEdit()

마지막으로 BeginEdit() 또는 AddNew()가 호출된 이후 변경된 내용을 내부 개체에 적용합니다.

적용 대상