Partager via


Example Step 1: Create the Derived 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 derives from a core orders system class.

To create the extended MyLineItem class

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

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

  3. Add using directives for the following namespaces:

  4. Add a new class named MyLineItem that derives from the LineItem class, and add the Serializable attribute to the class definition.

  5. Define a private property to store the value of the new property. For this example, create a private string property named widgetDescriptionProperty.

  6. Within the constructor for the MyLineItem class:

    1. Call the constructor for the base class.

    2. Set a default value for the widgetDescriptionProperty property.

  7. Define a public property to expose the private widgetDescriptionProperty property. For this example, name the property WidgetDescriptionProperty.

  8. Within the set accessor:

    1. Perform any required validation.

    2. Call the SetDirty method.

      Note

      The SetDirty method validates the size of a string value against the size of the database column to which the value is mapped, updates time stamps, and performs internal optimizations.

    3. Set the value of the private property.

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

  10. Define a deserialization constructor that creates an instance of the MyLineItem class from serialization information. Within the deserialization constructor:

    1. Call the deserialization constructor of the base class to set all the properties of the base class.

    2. Add a try/catch block.

      Note

      If you already have LineItem objects in the database, when these objects are deserialized as instances of the MyLineItem class, they will not have a value for the widgetDescriptionProperty property. For more information about how to deserialize instances of multiple versions of an extended class, see How to Create Multiple Versions of an Extended Orders Class.

    3. Within the try section, set the value of the widgetDescriptionProperty property.

    4. Catch the SerializationException exception. Within the catch block, set the value of the widgetDescriptionProperty property to a default value.

  11. Override the GetObjectData method, and add the SerializationFormatter security demand. Within the GetObjectData method:

    1. Call the GetObjectData method of the base class to serialize all the properties of the base class.

    2. Serialize the widgetDescriptionProperty property.

Example

The following example extends the LineItem class by creating a new class named MyLineItem that contains an additional property.

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

namespace MyOrders
{
    [Serializable]
    public class MyLineItem : LineItem
    {
        private string widgetDescriptionProperty;
        public MyLineItem()
            : base()
        {
            this.widgetDescriptionProperty = "No Description";
        }

        public string WidgetDescriptionProperty
        {
            set
            {
                if (value != null)
                    value = value.Trim();
                SetDirty(value);
                this.widgetDescriptionProperty = value;
            }
            get
            {
                return this.widgetDescriptionProperty;
            }
        }

        protected MyLineItem(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            try
            {
                widgetDescriptionProperty = info.GetString("widgetDescriptionProperty");
            }
            catch (SerializationException se)
            {
                widgetDescriptionProperty = "No Description";
            }
        }

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

Compiling the Code

After you build the MyOrders assembly, copy the MyOrders.dll file to the /bin directory of your Commerce Server Web site.

See Also

Other Resources

Orders Example: Extending the LineItem Class