Partager via


Example Step 1: Create the VirtualGiftCertificate 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 virtual gift certificate.

To create the VirtualGiftCertificate class

  1. In Visual Studio, create a new class library project named VirtualGiftCertificates.

  2. Add a reference to the Microsoft.CommerceServer.Runtime.dll assembly.

  3. Add using directives for the following namespaces:

  4. Define a new class named VirtualGiftCertificate, and add the Serializable attribute to the class definition. Indicate that the class implements the MappedStorageBase and ISerializable interfaces.

    Note

    The VirtualGiftCertificate class supports versioning. Implementing the ISerializable interface enables you to persist the version number of a VirtualGiftCertificate object.

  5. Define the following private member variables:

    1. currentClassRevision: to store the version number.

    2. virtualGiftCertificateId: to store the unique identifier.

    3. orderGroupId: to store the OrderGroupId of the OrderGroup object that contains the virtual gift certificate.

    4. orderFormId: to store the OrderFormId of the OrderForm object that contains the virtual gift certificate.

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

    6. Any additional member variables that you need in order to store the state of the object, such as the balance remaining on the virtual gift certificate.

  6. Define a default constructor for the VirtualGiftCertificate class. Make the constructor call the base constructor after you initialize the member variables.

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

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

  9. Define a public property to expose each private member variable that you will need access to.

  10. Within the get accessor, return the value of the private property.

  11. Within the set accessor:

    1. Perform any required validation.

    2. Call the SetDirty method. You will define the SetDirty method later.

    3. Set the value of the private property.

  12. Override the SetDirty method. Within the method, call the base method, and then call the SetChildDirty method of the OrderForm object that contains the VirtualGiftCertificate object.

  13. Override both implementations of the overloaded SetChildDirty method. Within each method, call the corresponding SetChildDirty method of the OrderForm object that contains the VirtualGiftCertificate object.

  14. Override the SetParent method. Within the method, set the parentOrderForm, orderGroupId, and orderFormId member variables to the appropriate values from the OrderForm object.

Example

The following example defines the VirtualGiftCertificate class and the methods that the class requires to be associated with a Commerce Server order. This example uses the MyOrderForm class instead of the OrderForm class. You will create the MyOrderForm class in a later step of this example.

Be aware that this is not a complete implementation of the VirtualGiftCertificate class. You can find the source code for the complete sample in the %COMMERCE_SERVER_ROOT%/SDK/Samples/OrdersExtensibility folder.

using System;
using Microsoft.CommerceServer.Runtime.Orders;
using Microsoft.CommerceServer.Runtime;
using System.Collections;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace VirtualGiftCertificates
{
    [Serializable]
    public class VirtualGiftCertificate : MappedStorageBase, ISerializable
    {
        private Guid virtualGiftCertificateId;
        private const int currentClassRevision = 1;
        private Guid orderGroupId;
        private Guid orderFormId;
        private OrderForm parentOrderForm;

        public VirtualGiftCertificate() :base()
        {
            virtualGiftCertificateId = Guid.NewGuid();
            orderFormId = Guid.Empty;
            orderGroupId = Guid.Empty;
        }

        protected VirtualGiftCertificate (SerializationInfo info, StreamingContext context)
        {
            this.orderFormId = (Guid)info.GetValue("OrderFormId", typeof(Guid));
            this.orderGroupId = (Guid)info.GetValue("OrderGroupId", typeof(Guid));
            this.parentOrderForm = (OrderForm)info.GetValue("ParentOrderForm", typeof(OrderForm));
            this.virtualGiftCertificateId = (Guid)info.GetValue("VirtualGiftCertificateId", typeof(Guid));
        }

        [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter=true)]
        public void GetObjectData (SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Ver", currentClassRevision);
            info.AddValue("OrderFormId", orderFormId);
            info.AddValue("OrderGroupId", orderGroupId);
            info.AddValue("ParentOrderForm", parentOrderForm);
            info.AddValue("VirtualGiftCertificateId", virtualGiftCertificateId);
        }

        public Guid VirtualGiftCertificateId
        {
            get
            {
                return virtualGiftCertificateId;
            }
            set
            {
                SetDirty(value);
                virtualGiftCertificateId = value;
            }
        }

        public Guid OrderFormId
        {
            get
            {
                return orderFormId;
            }
            set
            {
                SetDirty(value);
                orderFormId = value;
            }
        }

        public Guid OrderGroupId
        {
            get
            {
                return orderGroupId;
            }
            set
            {
                SetDirty(value);
                orderGroupId = value;
            }
        }

        public OrderForm ParentOrderForm
        {
            get
            {
                return parentOrderForm;
            }
            set
            {
                SetDirty(value);
                parentOrderForm = value;
            }
        }

        public override void  SetDirty(object propertyValue)
        {
            base.SetDirty(propertyValue);
            if (this.parentOrderForm != null)
            {this.parentOrderForm.SetChildDirty();}
        }

        public override void  SetChildDirty()
        {
            if (!StorageLoadInProgress && this.parentOrderForm != null)
            {this.parentOrderForm.SetChildDirty();}
        }

        public override void  SetChildDirty(DateTime modificationTime)
        {
            if (!StorageLoadInProgress && this.parentOrderForm != null)
            {this.parentOrderForm.SetChildDirty(modificationTime);}
        }

        public override void  SetParent(object parent)
        {
            if (parent == null)
            {
                this.parentOrderForm = null;
                this.orderGroupId = Guid.Empty;
                this.orderFormId = Guid.Empty;
            }
            else
            {
                this.parentOrderForm = (MyOrderForm)parent;
                this.orderFormId = this.parentOrderForm.OrderFormId;
                if (this.parentOrderForm != null)
                {this.orderGroupId = this.parentOrderForm.ParentOrderGroup.OrderGroupId;}
            }
        }
    }
}

See Also

Other Resources

Orders Example: Adding Virtual Gift Certificates