Skip to content

Instantly share code, notes, and snippets.

Pete Smith beyond-code-github

View GitHub Profile
beyond-code-github / gist:4723682
Created Feb 6, 2013
Read a delta from a custom media type formatter... if a bit naughty.
View gist:4723682
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
IEdmModel model = builder.GetEdmModel();
var odataFormatters = ODataMediaTypeFormatters.Create(model);
var delta = content.ReadAsAsync(type, odataFormatters).Result;
beyond-code-github / gist:5048997
Created Feb 27, 2013
Useful feature for Delta<T> - Get previous and current values for changed properties
View gist:5048997
public Dictionary<string, Tuple<object, object>> GetChanges(TEntityType original)
if (original == null)
throw new ArgumentNullException("original");
if (!_entityType.IsAssignableFrom(original.GetType()))
throw new InvalidOperationException(string.Format("Entity mismatch exception between {0} and {1}", _entityType, original.GetType()));
beyond-code-github / Config
Last active Dec 14, 2015
We have two different kinds of route at present, /Api/{AppId}/{Controller}/{Id} and /Api/{Controller}/{Id}. The Linked route provider ensures that if we PATCH or PUT to either of these that we also invalidate the cache for the same route without the {Id} parameter
View Config
config.MessageHandlers.Add(new CachingHandler
UriTrimmer = (uri) => uri.LocalPath,
LinkedRoutePatternProvider = LinkedRoutePatternProvider
public static IEnumerable<string> LinkedRoutePatternProvider(string s, HttpMethod httpMethod)
var appAware = new Regex("/Api/[0-9]+/([a-z]|[A-Z])+/[0-9]+");
if (appAware.IsMatch(s))
beyond-code-github / gist:5115938
Last active Dec 14, 2015
Quick sketch of pulling together data required to build nested routes
View gist:5115938
var timer = new Stopwatch();
var configuration = GlobalConfiguration.Configuration;
var explorer = new ApiExplorer(configuration);
var descriptions = explorer.ApiDescriptions;
var controllerDescriptors = descriptions.Select(o => o.ActionDescriptor.ControllerDescriptor).Distinct().ToList();
beyond-code-github / gist:5142264
Last active Dec 14, 2015
Very simple example of hierarchical route generation in web api
View gist:5142264
// Base route
config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new { id = RouteParameter.Optional });
// Nested route (must be distinguishable by param names from above) - Deisgnated by leading _
config.Routes.MapHttpRoute("NestedRoute", "_/{controller}/{rootId}");
// Root controller signature:
public class AppDataController : ApiController
View gist:5161831
public static void RegisterCors(HttpConfiguration httpConfiguration)
WebApiCorsConfiguration corsConfig = new WebApiCorsConfiguration();
corsConfig.ForAllOrigins().AllowMethods("GET", "POST", "PUT", "PATCH", "DELETE").AllowAllRequestHeaders();
View gist:5162126
protected void Application_BeginRequest(object sender, EventArgs e)
if (this.Context.Request.Path.Contains("signalr/"))
this.Context.Response.AddHeader("Access-Control-Allow-Headers", "accept,origin,authorization,content-type");
beyond-code-github / gist:5244422
Last active Dec 15, 2015
Cache invalidation use-case #1
View gist:5244422
I have a URI used to manipulate data records:
There is also the ability to retrieve several report dataviews based on the records.
I need a mechanism so that when I POST to http://api/{AppId}/AppData/, or DELETE\PATCH to http://api/{AppId}/AppData/{id} then any cached route starting with http://api/{AppId}/ChartData/ gets invalidated.
beyond-code-github / PhantomExpress.cs
Created Mar 29, 2013
Phantom Express is a console application that will start IIS Express for a given folder and port, then run Phantom JS as writing the results to the console. Once phantom JS is finished, IIS express is closed.
View PhantomExpress.cs
namespace PhantomExpress
using System;
using System.Diagnostics;
using System.Threading.Tasks;
internal class Program
private static int Main(string[] args)
beyond-code-github / gist:5310180
Last active Dec 15, 2015
Http routes as state machine
View gist:5310180
The route state machine defined in pseudo code below is able to handle the following routes:
You can’t perform that action at this time.