Example Step 1: Create the Derived Class
This topic describes how to create a new class that derives from a core orders system class.
To create the extended MyLineItem class
In Visual Studio, create a new class library project named MyOrders.
Add a reference to the Microsoft.CommerceServer.Runtime.dll assembly.
Add using directives for the following namespaces:
System.Runtime.Serialization
System.Security.Permissions
Add a new class named MyLineItem that derives from the LineItem class, and add the Serializable attribute to the class definition.
Define a private property to store the value of the new property. For this example, create a private string property named widgetDescriptionProperty.
Within the constructor for the MyLineItem class:
Call the constructor for the base class.
Set a default value for the widgetDescriptionProperty property.
Define a public property to expose the private widgetDescriptionProperty property. For this example, name the property WidgetDescriptionProperty.
Within the set accessor:
Within the get accessor, return the value of the private property.
Define a deserialization constructor that creates an instance of the MyLineItem class from serialization information. Within the deserialization constructor:
Call the deserialization constructor of the base class to set all the properties of the base class.
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.
Within the try section, set the value of the widgetDescriptionProperty property.
Catch the SerializationException exception. Within the catch block, set the value of the widgetDescriptionProperty property to a default value.
Override the GetObjectData method, and add the SerializationFormatter security demand. Within the GetObjectData method:
Call the GetObjectData method of the base class to serialize all the properties of the base class.
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 Core Systems Web site.