RequestContext property
You can use the RequestContext property of the ServiceProviderRequest class to exchange information between pipeline processors and pipelines.
For example, the CommerceServer Connect ECS uses the RequestContext property to share the Basket amongst processors, therefore promoting modularity of the processors and performance by making sure the Basket is loaded only once during the execution of a pipeline.
The following is a simplified version of the AddCartLines pipeline:
<commerce.carts.addCartLines>
<!-- Setup the commerce server context -->
<processor type="Sitecore.Commerce.Connect.CommerceServer.Orders.Pipelines.ResolveBasket, Sitecore.Commerce.Connect.CommerceServer" />
<processor type="Sitecore.Commerce.Connect.CommerceServer.Orders.Pipelines.AddLinesToCart, Sitecore.Commerce.Connect.CommerceServer" >
<param desc="Rollup">True</param>
<param ref="eaPlanProvider"/>
<param ref="eaStateCartRepository"/>
</processor>
<processor type="Sitecore.Reference.Storefront.Connect.Pipelines.Carts.RunPipeline, Sitecore.Reference.Storefront.Powered.by.CommerceServer">
<param desc="pipelineName">Basket</param>
</processor>
<processor type="Sitecore.Reference.Storefront.Connect.Pipelines.Carts.RunTotalPipeline, Sitecore.Reference.Storefront.Powered.by.CommerceServer">
<param desc="pipelineName">Total</param>
</processor>
<processor type="Sitecore.Commerce.Pipelines.Carts.Common.RunSaveCart, Sitecore.Commerce.Connect.Core"/>
</commerce.carts.addCartLines>In the above example, the first processor of every cart-related operation makes use of a ResolveBasket processor. This processor is responsible for loading the Commerce Server basket and setting up the request context. All of the other processors will fetch the Commerce Server basket from the request context.
The last processor, RunSaveCart, calls another pipeline to save the basket. The request context automatically flows to this pipeline and it will be able to fetch the basket in order to save its content.
The following shows the CartPipelineContext utility class:
public class CartPipelineContext : PipelineContext
{
protected CartPipelineContext()
: base(PipelineContextNames.CartPipelineContext)
{
this.HasBasketErrors = false;
}
public Basket Basket { get; set; }
public static CartPipelineContext Get(ServiceProviderRequestContext requestContext)
{
CartPipelineContext pipelineContext = requestContext.Properties[PipelineContextNames.CartPipelineContext] as CartPipelineContext;
if (pipelineContext == null)
{
pipelineContext = new CartPipelineContext();
requestContext.Properties[PipelineContextNames.CartPipelineContext] = pipelineContext;
}
return pipelineContext;
}
}In the above code, the static Get() method provides a useful way for creating and retrieving the CartPipelineContext instance from the RequestContext property. Notice that a key/value pair is added to the Properties collection of the request context. It is important to make this name unique to make sure it does not enter in conflict with other request context items that could be added by other processors.
The following is a snippet of code from the ResolveBasket processor:
IOrderRepository repository = CommerceTypeLoader.CreateInstance<IOrderRepository>();
var cartContext = CartPipelineContext.Get(args.Request.RequestContext);
cartContext.Basket = repository.GetBasket(Guid.Parse(userId), cartName);Because the call to the CartPipelineContext.Get() class is issued from the first processor, the context class is created and added to the RequestContext collection. Next, the code simply sets the cartContext.Basket class to the actual Commerce Server basket.
All other processors, including the RunSaveCart pipeline, simply perform the following to retrieve the Basket from the RequestContext property:
var cartContext = CartPipelineContext.Get(request.RequestContext);
Assert.IsNotNull(cartContext, "cartContext");
Assert.IsNotNull(cartContext.Basket, "cartContext.Basket");At this point, you can use the cartContext.Basket class to perform the necessary operation (such as adding a line item) on the Commerce Server basket.