Skip to content

Instantly share code, notes, and snippets.

View chrisnicola's full-sized avatar

Chris Nicola chrisnicola

View GitHub Profile
@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>
{
@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 / 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>()); };
}
using (OpenRastaConfiguration.Manual)
{
ResourceSpace.Has.ResourcesOfType<RetailStream>()
.AtUri("/streams/{id}").And.AtUri("/streams")
.HandledBy<RetailStreamHandler>()
.TranscodedBy<JsonRestCodec>().And.;
ResourceSpace.Has.ResourcesOfType<RetailStreamListItem[]>()
.AtUri("/streams")
.HandledBy<RetailStreamHandler>()
@chrisnicola
chrisnicola / DynamicExamples.cs
Created January 19, 2011 03:59
Some examples of how the dynamic keyword works
[Fact]
public void cant_pass_base_class_to_function_dynamically()
{
dynamic handler = new MessageHandler();
IMessage message = new Message();
handler.Handle(message);
}
[Fact]
public void can_pass_base_class_to_function_via_reflection()
using System;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
namespace Presentation.Helpers {
public static class TreeViewitemFinder {
public static TreeViewItem BringTreeViewItemIntoView(this TreeView treeView, TreeViewItemModel item) {
if (item == null) return null;
//Apply selector to a random subset of source of size count and return the results
public static class ExtensionHelpers {
public static IEnumerable<TOut> SelectRandom<TSource, TOut>(this IEnumerable<TSource> source,
Func<TSource, TOut> selector,
int count) {
var rand = new Random((int) (DateTime.Now.Ticks % int.MaxValue));
for (var i = 0; i < count; i++)
yield return selector(source.ElementAt(rand.Next(source.Count())));
}
}
// ==UserScript==
// @author Chris
// @co-author None
// @name Timer
// @namespace myTest
// @description eRepublik Advanced Tools
// @version 1.0.0
// @include *
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// ==/UserScript==
00000000000000000003
5b406bc7-2f68-4e97-bfe2-75e179571b31;634163655158954274;Ncqrs.Eventing.Storage.NoDB.Tests.Fakes.CustomerCreatedEvent, Ncqrs.Eventing.Storage.NoDB.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null;1.0;adc02213-cde3-4eaf-942e-1ef4d5adb406;0;{ "Name": "Foo", "Age": 35}
59e9e60b-dba5-4b96-97a2-90ae64b17e48;634163655158974275;Ncqrs.Eventing.Storage.NoDB.Tests.Fakes.CustomerNameChanged, Ncqrs.Eventing.Storage.NoDB.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null;1.0;adc02213-cde3-4eaf-942e-1ef4d5adb406;1;{ "CustomerId": "adc02213-cde3-4eaf-942e-1ef4d5adb406", "NewName": "Name2"}
df092cec-9857-4f6c-b6ff-32110ebc0d75;634163655158984276;Ncqrs.Eventing.Storage.NoDB.Tests.Fakes.CustomerNameChanged, Ncqrs.Eventing.Storage.NoDB.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null;1.0;adc02213-cde3-4eaf-942e-1ef4d5adb406;2;{ "CustomerId": "adc02213-cde3-4eaf-942e-1ef4d5adb406", "NewName": "Name3"}
public abstract class DomainEventTestBase<T> : IEventHandler<T> where T : DomainEvent
{
protected T Result;
protected InProcessEventBus DomainEventBus;
#region IEventHandler<T> Members
public void Handle(T evnt)
{
Result = evnt;