Skip to content

Instantly share code, notes, and snippets.

View chrisnicola's full-sized avatar

Chris Nicola chrisnicola

View GitHub Profile
@chrisnicola
chrisnicola / NancySample.cs
Created January 24, 2011 04:59
Some example usage of Nancy
public AccountHandler()
{
Get["/accounts/{id}"] = x => { return GetById(x.id); };
Get["/accounts"] = x => { return GetListing(); };
Post["/accounts", req => CanPost(req)] = x => { return CreateNewAccount(Request.Body.FromJson<NewAccountForm>()); };
}
@chrisnicola
chrisnicola / AuthenticationInterceptor.cs
Created January 24, 2011 05:21
A sample authentication interceptor for Nancy using Castle Interceptors for AOP
public class AuthenticationInterceptor : IInterceptor
{
bool CanIntercept(IInvocation invocation) { return invocation.Method.GetCustomAttributes(true).Any(a => a is RequiresAuthenticationAttribute); }
public void Intercept(IInvocation invocation)
{
if (!CanIntercept(invocation)) invocation.Proceed();
else
{
var module = invocation.InvocationTarget as NancyModule;
@chrisnicola
chrisnicola / DynamicDictionary.cs
Created January 26, 2011 18:37
Updated dynamic dictionary with support for implicit casting
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq.Expressions;
using Microsoft.CSharp.RuntimeBinder;
namespace Nancy
{
public class DynamicDictionary : DynamicObject, IEquatable<DynamicDictionary>
{
public class when_updating_a_feature_for_a_product : domain_context<SetFeaturesForProduct, SetFeaturesForProductHandler>
{
static Product _product;
Establish context = () =>
{
_product = Given<Product>(History.product_created, History.product_feature_added);
ExpectedEvents = 1;
};
public static class DomainSpecificationExtensions
{
public static void ShouldHave<T>(this IEnumerable<ISourcedEvent> source, params Predicate<T>[] conditions) where T : class, ISourcedEvent
{
T actualEvent = (T) source.FirstOrDefault(x => x is T);
if (actualEvent == null)
throw new SpecificationException(string.Format("{0} did not happen as expected", typeof (T).Name));
actualEvent.ShouldMatch(conditions);
}
public class when_an_array_is_updated_internally : mongo_repository_context<CategoryModel>
{
static CategoryModel model = new CategoryModel { Name = "Test", FeatureTypes = new[] {
new FeatureTypeModel{ Name = "feature1", DefaultValue = "Test1" },
new FeatureTypeModel{ Name = "feature2", DefaultValue = "Test2" },
}};
Establish context = () => {
InternalCollection.Save(model);
cursor = InternalCollection.Find(Query.EQ("_id", "Test"));
/// <summary>
/// An entity which references a Category
/// </summary>
public class CategoryRef : Entity<Catalog>
{
string _slug;
public CategoryRef(Catalog catalog, Category category, Category parentCategory) : base(catalog, Guid.NewGuid())
{
ApplyEvent(new CategoryAddedToCatalog {Category = category, Slug = category.Name.ToSlug() });
@chrisnicola
chrisnicola / AuthenticatedModule.cs
Created February 19, 2011 20:41
PerWebRequest
public RetailStreamHandler(IDocumentRepository<ShoppingCart> cartRepository)
{
_cartRepository = cartRepository;
Get["/cart"] = x => GetListing();
Post["/cart"] = x => PostNewShoppingCart(Request.Body.FromJson<ShoppingCartForm>());
Get["/cart/{id}"] = x => GetById(x.id);
}
[RequiresAuthentication]
public virtual Response PostNewShippingCart(ShoppingCartForm form)
[core]
editor = vim
excludesfile = ~/.gitignore
[user]
name = chris.nicola
email = chris.nicola@visioncritical.com
[color]
diff = auto
branch = auto
status = auto
@chrisnicola
chrisnicola / GzipFilter.cs
Created August 15, 2011 19:36
Gzip Compression Filter for Nancy
/* A JSON gzip compression filter, which could easily be adapted to any pattern needed. This uses a custom AfterFilter
* type which is just a fancy wrapper of Action<NancyContext>. It's useful for convention based loading of filters
*/
public class GzipCompressionFilter : AfterFilter
{
protected override void Handle(NancyContext ctx)
{
if ((ctx.Response.ContentType == "application/json") && ctx.Request.Headers.AcceptEncoding.Any(