Jaa


Example Step 3: Derive a New OrderForm Class

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

This topic describes how to extend the OrderForm class to add a collection of virtual gift certificates to an order.

To extend the OrderForm 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. Add a new class named MyOrderForm that derives from the OrderForm class, and add the Serializable attribute to the class definition.

  3. Define a private property named giftCertificates to store the collection of virtual gift certificates.

  4. Within the constructor for the MyOrderForm class:

    1. Call the constructor for the base class.

    2. Initialize the value of the giftCertificates property.

  5. Define a deserialization constructor that creates an instance of the MyOrderForm class from serialization information. Within the deserialization constructor, retrieve the of the giftCertificates property.

  6. Override the GetObjectData method, and add the SerializationFormatter security demand. Within the GetObjectData method, serialize the value of the giftCertificates property.

  7. Define a public property named GiftCertificates to expose the private giftCertificates property. Within the get accessor, return the value of the private property. Define a set accessor, but leave the body of the set accessor empty.

  8. Override the SetParent method of the OrderForm object. Call the base method, and then call the SetParent method of each VirtualGiftCertificate object in the virtual gift certificate collection.

  9. Build the VirtualGiftCertificates assembly, and copy the VirtualGiftCertificates.dll file to the /bin directory of your Commerce Server Web site.

Example

The following example extends the OrderForm class by creating a new class named MyOrderForm that contains a VirtualGiftCertificateCollection object.

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

[Serializable]
public class MyOrderForm : OrderForm
{
    private VirtualGiftCertificateCollection giftCertificates;

    public MyOrderForm() : base(null)
    {
        giftCertificates = new VirtualGiftCertificateCollection(this);
    }

    public MyOrderForm(SerializationInfo info, StreamingContext context) : base(info, context)
    {
        this.giftCertificates = (VirtualGiftCertificateCollection)info.GetValue("GiftCertificates", typeof(VirtualGiftCertificateCollection));
    }

    [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
    public override void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        base.GetObjectData(info, context);
        info.AddValue("GiftCertificates", this.giftCertificates);
    }

    public VirtualGiftCertificateCollection GiftCertificates
    {
        get
        {
            return giftCertificates;
        }
        set
        {
        }
    }

    public override void SetParent(object parent)
    {
        base.SetParent(parent);
        foreach (VirtualGiftCertificate vgc in this.giftCertificates)
        {
            vgc.SetParent(this);
        }
    }
}

See Also

Other Resources

Orders Example: Adding Virtual Gift Certificates