Skip to content

Instantly share code, notes, and snippets.

@jjvdangelo
jjvdangelo / ControllerSpecsFor.cs
Created March 28, 2013 05:39
Some SpecsFor extensions used to test ASP.NET MVC controllers (C# and F#).
namespace SpecsForExtensions
{
using System;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
public class ControllerSpecsFor<T> : DbSpecsFor<T>
where T : Controller
@jjvdangelo
jjvdangelo / DisplayModelErrorsAttribute.cs
Created June 24, 2013 16:40
Allows HttpGet decorated controllers to display model errors.
namespace Attributes
{
using System.Web.Mvc;
public class DisplayModelErrorsAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
var model = filterContext.Controller.ViewData.Model;
var metadatas = ModelMetadataProviders.Current.GetMetadataForProperties(model, model.GetType());
@jjvdangelo
jjvdangelo / Simple.Web.Behaviors.Example.cs
Created June 27, 2013 15:47
Example of what I'm trying to achieve in Simple.Web using behaviors.
namespace MyCompany.Product
{
// I tend to utilize immutable value objects in my domain and am
// attempting to, basically, inject them into my handlers via
// a behavior--tying into the Simple.Web pipeline.
public class Foo
{
private readonly string _value;
public string Value { get { return _value; } }
@jjvdangelo
jjvdangelo / HashExtensions.cs
Created July 13, 2013 23:44
A simple helper for implementing GetHashCode().
namespace Helpers
{
// Used like:
// public override int GetHashCode() {
// return 17.CombineHashWith(foo).CombineHashWith(bar).CombineHashWith(GetType());
// }
public static int CombineHashWith<T>(this int initialHash, T other)
{
var otherHash = other == null ? 0 : other.GetHashCode();
return initialHash + initialHash * 23 + otherHash;
module CommandBus =
let createCommandRouter handlers =
let event = Event<Command>()
handlers |> List.iter(fun handler -> event.Publish |> Event.add handler)
let agent = MailboxProcessor.Start(fun inbox ->
let loop() = async {
let! cmd = inbox.Receive()
cmd |> event.Trigger
return! loop() }
open System.Web.Hosting
open Simple.Web.Helpers
type PathUtility() =
interface IPathUtility with
member __.MapPath virtualPath =
virtualPath |> HostingEnvironment.MapPath
[<RequireQualifiedAccess>]
module Authentication
open System
open System.Collections.Generic
open Simple.Web
open Simple.Web.Http
open Simple.Web.Authentication
type Payload = { UserId: Guid; Name: string; Expires: int64 }
[<RequireQualifiedAccess>]
module Authentication
open System
open System.Collections.Generic
open Simple.Web
open Simple.Web.Http
open Simple.Web.Authentication
type Payload = { UserId: Guid; Name: string; Expires: int64 }
module Counter =
type State = { Value: int }
static member Zero = { Value: 0 }
type Command =
| Incremement
| Decrement
type Event =
| Incremented
// --Snip--
let getEntity id =
use conn = EntityContext.GetDataContext()
query { for t1 in conn.Table_1 do
join t2 in conn.Table_2 on (t1.id = t2.table_1_id)
where (t1.id = id)
select t2
headOrDefault } |> function
| null -> Unchecked.defaultof<_>
| t2 -> { Id = t2.id; Value = t2.value }