次の方法で共有


How to Create Multiple Versions of an Extended Orders Class

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

When you extend an orders class after your Commerce Server application is in production, orders that are stored in the database might not comply with the new class definition. You must enable your Commerce Server application to access orders that were stored in a different format.

For example, assume that you extended the Basket class by creating a new class named Basket1 with a new property named newProperty1. Your site has been in production for a while. Therefore, customers have placed orders and these orders are stored in the database.

Now you have to extend the Basket1 class. You create a new class named Basket2 and add a new property named newProperty2. You can save an instance of the Basket2 class and load the instance again. However, when you try to load a basket that was saved as an instance of the Basket1 class, the instance cannot be created because there is no value for the newProperty2 property.

To access orders that were stored as instances of an obsolete class, you must do two things:

  • Define a special deserialization constructor for the new class.

  • Configure the new class to replace the obsolete class.

This topic describes how to create a deserialization constructor. For more information about how to configure the new class to replace the old class, see SerializationBindings Element.

To define a deserialization constructor

  1. Define a protected constructor that takes two arguments: a SerializationInfo object and a StreamingContext object.

  2. Add the base keyword to the constructor to call the constructor of the base class, and pass both parameters to the constructor of the base class.

  3. Within the constructor of the new class, add a try/catch block for each new property that you added to the extended class.

  4. In the try section, set the value of the new property to the value that you obtain from the SerializationInfo object.

  5. Catch the SerializationException exception. Within the catch section, set the value of the new property to a default value.

Example

The following example creates an instance of the Basket2 class from a serialized representation of a basket.

This example is provided for illustrative purposes only and cannot be compiled or run as is.

protected Basket2 (SerializationInfo info, StreamingContext context) : base(info, context)
{
     try
    {
        newProperty2 = info.GetString("newProperty2");
    }
    catch(SerializationException se)
    {
        newProperty2 = "";
    }
}

If the database also contains instances of the original Basket class, you might have to deserialize baskets that are missing both the newProperty1 property and the newProperty2 property. In that case, you would add another try/catch block to obtain the value of newProperty1, and set it to a default value if an exception occurs because the property is missing.

See Also

Other Resources

How to Derive a New Orders Class

How to Integrate a Derived Class with the Orders System

SerializationBindings Element

Extending the Orders Runtime