次の方法で共有


Transactions in Commerce Server 2007 Catalog

 

There is no inbuilt support for transaction for commerce server 2007 catalog. For e.g. if one product needs to be added in the one commerce server catalog and also same products needs to be related to the product in same catalog or in different catalog, this thing needs to be done in the transaction, but CS2007 catalog API's do not have in built support of transactions. This can be achieved using the TransactionScope object.

Transaction scope defines a block of code that participates in a transaction. If the code block completes successfully, the transaction manager commits the transaction. Otherwise, the transaction manager rolls back the transaction.

using (TransactionScope scope = new TransactionScope())

{

// get the new Id for this product.

string productId = “POD101”;

// get the base catalog so that we can create the product.

CS2K7Catalog.BaseCatalog baseCatalog = this.CatalogContext.GetCatalog(“BaseCatalog”, “en-US”);

// create a new product.

CS2K7Catalog.Product newProduct = baseCatalog.CreateProduct(“ProductDefinitionName”, productId, 0, “CategoryName”);

// and save it.

newProduct.Save(true);

CS2K7Catalog.ProductCatalog contentCatalog = this.CatalogContext.GetCatalog(“contentCatalog”, “en-US”);

ProductConfiguration productConfiguration = new ProductConfiguration();

productConfiguration.LoadRelatedProducts = true;

CS2K7Catalog.Product relatedProduct = contentCatalog.GetProduct(relatedProductId, productConfiguration);

newProduct.AddRelationshipToProduct(this.ContentCatalog, relatedProductId, “RelationshipName”, “RelationshipDescription”);

scope.Complete();

}

Above code snippet adds product in Base Catalog as well creates relationship to the product in the content catalog. These 2 operations are performed under the context of the transaction scope object. TransactionScope can also be nested.