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: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
{
@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 / 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: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 / 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;