Request Templates Controllers

Controllers are the recommended way to talk to the Service Adapter in Commerce Server. The controller acts as a way for you to manipulate calls to the backend before they leave the presentation tier e.g. adding a user id to a request.

The CommerceServerContrib.Web dll contains a base controller which does most of the hard work for you. The base controller can handle the following...
  • Executing a request straight away
  • Enables request bundling, queue up all of your requests first and then execute
  • Makes sure request templates marked as containing sensitive data cannot be executed over an AJAX call
  • Makes sure a request template can only be executed by its associated controller.

public static CommerceEntity GetProductSummary(string productId, string catalogId)
{
	var parameters = new RequestTemplateParameterCollection();

	parameters.Add(new RequestTemplateParameter("ProductId", productId, RequestTemplateParameterType.Search));
	parameters.Add(new RequestTemplateParameter("CatalogId", catalogId, RequestTemplateParameterType.Search));
	var controller = ControllerFactory.GetController("Catalog");
	var serviceAdapterResponse = controller.ProcessRequest("GetProductSummary", parameters, "POCO");

	if (serviceAdapterResponse.Succeeded)
	{
		var response = serviceAdapterResponse.Value as List<CommerceEntity>;
		if(response !=null && response.Count >0)
		{
			return response[0];
		}
		//	It's possible that request template doesn't return anything so it's not an error if it returns nothing
	}
	else
	{
		// It is an issue if Succeeded == false
		throw new RequestTemplateException() { Errors = serviceAdapterResponse.Errors };
	}

	return null;
}

Last edited Nov 22, 2012 at 8:10 PM by cmcauliffe, version 2

Comments

No comments yet.