Skip to content

Instantly share code, notes, and snippets.

@ChrisMissal
Created September 18, 2012 23:19
Show Gist options
  • Save ChrisMissal/3746676 to your computer and use it in GitHub Desktop.
Save ChrisMissal/3746676 to your computer and use it in GitHub Desktop.
A helper class to nicely wrap service calls and provide JSON responses back.
private class Helper<T> : IDisposable where T : CheckoutInformation
{
private readonly T _input;
private readonly IOrderService _orderService;
private readonly Func<object, JsonResult> _jsonResultSelector;
private object _data;
public CheckoutHelper(T input, IOrderService orderService, Func<object, JsonResult> jsonResultSelector)
{
_input = input;
_orderService = orderService;
_jsonResultSelector = jsonResultSelector;
}
public ActionResult Response { get; private set; }
public void Dispose()
{
Response = _jsonResultSelector(_data);
}
public void Action(Action<T, IOrderService, Order> action)
{
try
{
var order = _orderService.GetOrder();
if (order == null)
{
_data = new { success = false, redirect = "/cart" };
return;
}
if (_input.ClientId != order.Id)
{
Response = new HttpStatusCodeResult((int) HttpStatusCode.Unauthorized);
return;
}
action(_input, _orderService, order);
_data = order;
}
catch (Exception ex)
{
_data = new { success = false, message = ex.Message };
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment