Skip to content

Instantly share code, notes, and snippets.

@benfoster
benfoster / gist:4474276
Created January 7, 2013 11:08
Inbound route testing with ASP.NET MVC
using NSubstitute;
using NUnit.Framework;
using System.Linq;
using System.Web;
using System.Web.Routing;
namespace Fabrik.Web.Hosted.Specs
{
[TestFixture]
public class InboundRouteTests
@benfoster
benfoster / gist:4416655
Last active June 19, 2021 18:32
A lightweight message bus using TPL DataFlow
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
namespace TDFDemo
{
class Program
{
@benfoster
benfoster / gist:4407698
Last active December 10, 2015 08:28
Images that are uploaded to our API can also be transformed on the fly by our media server. The media server URI for the resource is included as an alternate link.
id: 3,
uri: "http://storage.blob.core.windows.net/02a/image.jpg",
title: "ddd",
mediaType: "Image",
links: [
{
rel: "edit",
href: "https://api.onfabrik.com/sites/1/projects/85/media/3"
}
{
@benfoster
benfoster / gist:4407167
Created December 29, 2012 14:15
JSON driven UI
{
"Name": "ProjectsPageSize",
"DisplayName": "Projects Page Size",
"DefaultValue": 15,
"Description": "The page size (number of projects) for the project list page.",
"IsReadOnly": false,
"IsRequired": true,
"UIHint": null,
"ValidationRule": "^[1-9][0-9]*$"
}
@benfoster
benfoster / gist:4182206
Created December 1, 2012 13:14
Theme support in Nancy Fx
public class Bootstrapper : DefaultNancyBootstrapper
{
protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
{
base.ApplicationStartup(container, pipelines);
this.Conventions.ViewLocationConventions.Add((viewName, model, context) =>
{
var theme = context.Context.Items["Theme"] as string;
return string.Concat("themes/", theme, "/views/", viewName);
@benfoster
benfoster / gist:4025238
Created November 6, 2012 15:01
Returning HttpError
private async Task<ApiResponse<TResult>> ReadResponseAsync<TResult>(HttpResponseMessage response)
{
Ensure.Argument.NotNull(response, "response");
var apiResponse = new ApiResponse<TResult>();
if (response.IsSuccessStatusCode)
{
apiResponse.Content = await response.Content.ReadAsAsync<TResult>().ConfigureAwait(false);
}
else
@benfoster
benfoster / gist:4024709
Created November 6, 2012 13:19
Deserializing HttpError
public static class HttpErrorExtensions
{
public static ModelStateDictionary GetModelState(this HttpError httpError)
{
Ensure.Argument.NotNull(httpError, "httpError");
object serialized;
if (httpError.TryGetValue("ModelState", out serialized))
{
var modelState = new ModelStateDictionary();
@benfoster
benfoster / gist:4017221
Created November 5, 2012 13:36
Complex validation in ASP.NET Web API
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Dependencies;
@benfoster
benfoster / gist:4016852
Created November 5, 2012 11:54
Web API validation
using StructureMap;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using ValidationDemo.Application;
@benfoster
benfoster / gist:3951821
Created October 25, 2012 10:16
API Client Refactor
ApiClient apiClient;
// create from connection string in web/app config
apiClient = ApiClient.FromConnectionString("api");
// parse in memory connection string
apiClient = ApiClient.ParseConnectionString(
"url=https://api.local.com;username=test;password=test;");
// use fluent expression