다음을 통해 공유


Example Step 2: Create the VirtualGiftCertificateCollection Class

For the latest version of Commerce Server 2007 Help, see the Microsoft Web site.

This topic describes how to create a new class that represents a collection of virtual gift certificates.

To create the VirtualGiftCertificateCollection class

  1. In Visual Studio, open the VirtualGiftCertificates project that you created by following the instructions in the topic Example Step 1: Create the VirtualGiftCertificate Class.

  2. Define a new class named VirtualGiftCertificateCollection in the VirtualGiftCertificates namespace. Add the Serializable attribute to the class definition, and indicate that the class implements the ICollection and ISerializable interfaces.

  3. Define the following private member variables:

    1. virtualGiftCertificates: to store the collection of virtual gift certificates.

    2. parentOrderForm: to notify the OrderForm object of any changes to the virtual gift certificate collection.

    3. Any additional member variables that you need in order to store the state of the object.

  4. Define a constructor for the VirtualGiftCertificateCollection class. Make the constructor initialize the member variables.

  5. Define a deserialization constructor that creates an instance of the VirtualGiftCertificateCollection class from serialization information. Within the deserialization constructor, retrieve the values of the member variables of the object.

  6. Define the GetObjectData method, and add the SerializationFormatter security demand. Within the GetObjectData method, serialize the value of each member variable.

  7. Implement the ICollection interface by defining the Count, IsSynchronized, and SyncRoot properties and the GetEnumerator method.

  8. Define an integer-based indexer that returns the specified element of the collection.

  9. Define an Add method that adds a new VirtualGiftCertificate object to the collection. Within the Add method, set the ParentOrderForm property of the VirtualGiftCertificate object, and call the SetChildDirty method of the OrderForm object.

  10. Define a Remove method that removes a VirtualGiftCertificate object from the collection and reverses the other actions that the Add method performed.

  11. Define a Clear method that initializes a VirtualGiftCertificateCollection object and then calls the SetChildDirty method of the OrderForm object.

  12. Define a CopyTo method.

Example

The following example defines the VirtualGiftCertificateCollection class and the methods that the class requires to be associated with a Commerce Server order.

You can find the source code for the complete sample in the %COMMERCE_SERVER_ROOT%/SDK/Samples/OrdersExtensibility folder.

[Serializable]
public class VirtualGiftCertificateCollection : ICollection, ISerializable
{
    private ArrayList virtualGiftCertificates;
    private OrderForm parentOrderForm;

    public VirtualGiftCertificateCollection(OrderForm parentOrderForm)
    {
        if (parentOrderForm == null)
        { throw new ArgumentNullException("parentOrderForm"); }
        this.virtualGiftCertificates = new ArrayList();
        this.parentOrderForm = parentOrderForm;
    }

    public VirtualGiftCertificateCollection(SerializationInfo info, StreamingContext context)
    {
        this.parentOrderForm = (OrderForm)info.GetValue("ParentOrderForm", typeof(OrderForm));
        this.virtualGiftCertificates = (ArrayList)info.GetValue("VirtualGiftCertificates", typeof(ArrayList));
    }

    [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("ParentOrderForm", this.parentOrderForm);
        info.AddValue("VirtualGiftCertificates", this.virtualGiftCertificates);
    }

    public int Count
    {
        get
        {
            return this.virtualGiftCertificates.Count;
            }
    }

    public bool IsSynchronized
    {
        get
        {
            return false;
        }
    }

    public Object SyncRoot
    {
        get
        {
            return this;
        }
    }

    public IEnumerator GetEnumerator()
    {
        return this.virtualGiftCertificates.GetEnumerator();
    }

    public VirtualGiftCertificate this[int index]
    {
        get
        {
            return (VirtualGiftCertificate)this.virtualGiftCertificates[index];
        }
    }

    public void Add(VirtualGiftCertificate vgc)
    {
        if (vgc.ParentOrderForm != null)
        {
            throw new ArgumentException ("Virtual Gift Certificate is already a member of a collection.");
        }
        vgc.SetParent(this.parentOrderForm);
        this.virtualGiftCertificates.Add(vgc);
        this.parentOrderForm.SetChildDirty();
    }

    public void Remove(VirtualGiftCertificate vgc)
    {
        if (vgc == null)
        {
            throw new ArgumentNullException ("Virtual Gift Certificate");
        }
        this.virtualGiftCertificates.Remove(vgc);
        vgc.SetParent(null);
        this.parentOrderForm.SetChildDirty();
    }

    public void Clear()
    {
        foreach (VirtualGiftCertificate vgc in virtualGiftCertificates)
        {
            vgc.ParentOrderForm = null;
        }
        this.virtualGiftCertificates.Clear();
        this.parentOrderForm.SetChildDirty();
    }

    void ICollection.CopyTo(Array array, int index)
    {
        this.virtualGiftCertificates.CopyTo(array, index);
    }
}

See Also

Other Resources

Orders Example: Adding Virtual Gift Certificates