Partager via


How to Add a Weakly Typed Indexer Property to an Orders Class

Most public classes in the Orders System have an indexer. Given a string as an index, the indexer stores or returns a value. The value can be of any type. You can use the indexer to add custom properties to the core Orders objects.

There are two primary reasons to use an indexer for custom properties:

  • To pass information to or from a pipeline component.

  • To persist properties that are needed by the Web application, needed for data mining, or needed by another business system.

The following example illustrates how to add a custom property, personalizationText, to a LineItem object.

LineItem myLineItem = new LineItem();
myLineItem["personalizationText"] = "Dad";

The following Orders classes have indexers that you can add properties to:

Persisting the Properties in an Indexer

Commerce Server Core Systems persists Baskets and PurchaseOrders and the objects that are contained within them to the SQL Server database. Many fields of these objects are stored in named columns in tables in the database. However, the properties from the indexer are serialized into a binary large object (BLOB) and stored in the database as a single string that contains name/value pairs. When the object is retrieved from the database, it is deserialized and turned into an instance of the appropriate class.

By default, any properties that you add via an indexer are added to the BLOB and persisted to the database. If you do not want a property to be persisted to the database, use a string that begins with the underscore character ("_") for the index. You can also define a mapping between a property and a column in the database.

The following example illustrates how to add a custom property, _season, to a LineItem object. Because the index of the custom property begins with the underscore character, the property will not be persisted to the database.

LineItem myLineItem = new LineItem();
myLineItem["_season"] = "winter";

A property that is not persisted to the database is similar to a local variable that is accessible only from within the current Web page.

Any object that is persisted to the database must implement the ISerializable interface. In addition, if the object that is persisted contains other objects, the nested objects must also implement the ISerializable interface.

The following example illustrates how to add a custom property, materials, to a LineItem object. Since the index of the custom property does not begin with the underscore character, the property will be persisted to the database. Therefore, the MaterialSpec class must implement the ISerializable interface.

LineItem myLineItem = new LineItem();
MaterialSpec ringDetails = new MaterialSpec("gold", "ruby");
MyLineItem["materials"] = ringDetails;

Note

The name that you use as an index cannot be the same as the name of a member variable or a key in the Pipeline Adapter mapping file.

See Also

Other Resources

How to Map a Weakly Typed Indexer Property to an Explicit Database Column

Extending the Orders Runtime

How to Derive a New Orders Class