Skip to content

Instantly share code, notes, and snippets.

View beyond-code-github's full-sized avatar

Pete Smith beyond-code-github

View GitHub Profile
@beyond-code-github
beyond-code-github / gist:4723682
Created February 6, 2013 16:16
Read a delta from a custom media type formatter... if a bit naughty.
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<EfContext>("EfContext");
IEdmModel model = builder.GetEdmModel();
var odataFormatters = ODataMediaTypeFormatters.Create(model);
var delta = content.ReadAsAsync(type, odataFormatters).Result;
@beyond-code-github
beyond-code-github / gist:5048997
Created February 27, 2013 15:59
Useful feature for Delta<T> - Get previous and current values for changed properties
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
beyond-code-github / Config
Last active December 14, 2015 15:18
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
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
beyond-code-github / gist:5115938
Last active December 14, 2015 16:29
Quick sketch of pulling together data required to build nested routes
var timer = new Stopwatch();
timer.Start();
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
beyond-code-github / gist:5142264
Last active December 14, 2015 20:09
Very simple example of hierarchical route generation in asp.net web api
// 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
{
public static void RegisterCors(HttpConfiguration httpConfiguration)
{
WebApiCorsConfiguration corsConfig = new WebApiCorsConfiguration();
corsConfig.RegisterGlobal(httpConfiguration);
corsConfig.ForAllOrigins().AllowMethods("GET", "POST", "PUT", "PATCH", "DELETE").AllowAllRequestHeaders();
}
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
beyond-code-github / gist:5244422
Last active December 15, 2015 10:19
Cache invalidation use-case #1
I have a URI used to manipulate data records:
http://api/{AppId}/AppData/
There is also the ability to retrieve several report dataviews based on the records.
http://api/{AppId}/ChartData/{chartId}
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
beyond-code-github / PhantomExpress.cs
Created March 29, 2013 22:33
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.
namespace PhantomExpress
{
using System;
using System.Diagnostics;
using System.Threading.Tasks;
internal class Program
{
private static int Main(string[] args)
{
@beyond-code-github
beyond-code-github / gist:5310180
Last active December 15, 2015 19:19
Http routes as state machine
The route state machine defined in pseudo code below is able to handle the following routes:
/Api/Products/
/Api/Products/1
/Api/Categories
/Api/Categories/1
/Api/Products/1/Categories
/Api/Products/1/Categories/2