Skip to content

Instantly share code, notes, and snippets.

@benfoster
benfoster / gist:2926701
Created June 13, 2012 21:50
Switching between PageDown and Redactor editors
var editors = {
Markdown: {
name: "Markdown",
select: function() {
$("'.editor").addClass("editor-md");
$("#wmd-button-bar").show();
this.init();
},
init: function () {
if ($(".editor-md").length > 0 && !$(".editor-md").data(this.name)) {
@benfoster
benfoster / gist:2940739
Created June 16, 2012 09:38
ASP.NET MVC Controller Scaffolding
public class MyController {
private readonly IViewFactory viewFactory;
private readonly ICommandBus bus;
public MyController(IViewFactory viewFactory, ICommandBus bus) {
this.viewFactory = viewFactory;
this.bus = bus;
@benfoster
benfoster / gist:3004655
Created June 27, 2012 15:04
Composite Views in ASP.NET MVC
public interface IWidget
{
string Title { get; }
IHtmlString Render(HtmlHelper htmlHelper);
}
public class LatestTweetsWidget : IWidget
{
public string Title { get { return "Latest Tweets"; } }
public string Handle { get; set; }
@benfoster
benfoster / gist:3083223
Created July 10, 2012 13:28
ViewBuilder/ViewFactory
namespace Fabrik.Common.Web
{
public interface IViewBuilder<TView>
{
TView Build();
}
public interface IViewBuilder<TInput, TView>
{
TView Build(TInput input);
@benfoster
benfoster / gist:3118697
Created July 15, 2012 21:29
CDF Base64 Encoded Image Test
body {
font-size: .85em;
font-family: "Trebuchet MS", Verdana, Helvetica, Sans-Serif;
margin: 0;
padding: 0;
color: #696969;
background: url(data:image/jpg;base64,/9j/4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP/sABFEdWNreQABAAQAAAA9AAD/4QNtaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLwA8P3hwYWNrZXQgYmVnaW49Iu+7vyIgaWQ9Ilc1TTBNcENlaGlIenJlU3pOVGN6a2M5ZCI/PiA8eDp4bXBtZXRhIHhtbG5zOng9ImFkb2JlOm5zOm1ldGEvIiB4OnhtcHRrPSJBZG9iZSBYTVAgQ29yZSA1LjAtYzA2MCA2MS4xMzQ3NzcsIDIwMTAvMDIvMTItMTc6MzI6MDAgICAgICAgICI+IDxyZGY6UkRGIHhtbG5zOnJkZj0iaHR0cDovL3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZGYtc3ludGF4LW5zIyI+IDxyZGY6RGVzY3JpcHRpb24gcmRmOmFib3V0PSIiIHhtbG5zOnhtcE1NPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvbW0vIiB4bWxuczpzdFJlZj0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL3NUeXBlL1Jlc291cmNlUmVmIyIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bXBNTTpPcmlnaW5hbERvY3VtZW50SUQ9InhtcC5kaWQ6QTdDM0IxNjQyOEJGRTExMTkwMzA4NUUwMkVFMTQzMDMiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6NDYyRThDOEZCRjMxMTFFMUJEM0NFODZCQTUyOUNEMDMiIHhtcE1NOkluc3Rhb
@benfoster
benfoster / gist:3150146
Created July 20, 2012 10:54
RavenDB Session Management in ASP.NET Web API with StructureMap
public class StructureMapFilterProvider : IFilterProvider
{
private readonly IContainer container;
public StructureMapFilterProvider(IContainer container)
{
this.container = container;
}
public IEnumerable<FilterInfo> GetFilters(HttpConfiguration configuration, HttpActionDescriptor actionDescriptor)
@benfoster
benfoster / gist:3150615
Created July 20, 2012 13:01
Setting response properties
static Dictionary<int, string> etags = new Dictionary<int, string>();
// GET api/site/5
public HttpResponseMessage Get(int id, HttpRequestMessage request)
{
var etag = request.Headers.IfNoneMatch; // strange that we can't do FirstOrDefault()
if (etag != null && etags.ContainsKey(id))
{
if (etags[id] == etag.ToString())
@benfoster
benfoster / gist:3151971
Created July 20, 2012 17:16
Validating ModelState in WebApi
public class ValidateModelStateAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (!actionContext.ModelState.IsValid)
{
var errors = from k in actionContext.ModelState.Keys
from e in actionContext.ModelState[k].Errors
select new ValidationError { ParameterName = k, ErrorMessage = e.ErrorMessage };
@benfoster
benfoster / gist:3153711
Created July 20, 2012 22:42
Raven Index with Transform for Oliver
[TestFixture]
public class OliverTest : RavenTest
{
public class OnlineStoreSection
{
public string Id { get; set; }
public TargetAudience TargetAudience { get; set; }
public IEnumerable<string> Tags { get; set; }
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Type q to Quit");
string input;
while ((input = Console.ReadLine()) != "q")
{
var sw = new Stopwatch();