Skip to content

Instantly share code, notes, and snippets.

View thecodejunkie's full-sized avatar

Andreas Håkansson thecodejunkie

View GitHub Profile
@thecodejunkie
thecodejunkie / gist:5056745
Created February 28, 2013 13:34
InstancePerMatchingLifetimeScope for Nancy.Bootstrappers.Autofac
/// <summary>
/// Creates a per request child/nested container
/// </summary>
/// <returns>Request container instance</returns>
protected override ILifetimeScope CreateRequestContainer()
{
return ApplicationContainer.BeginLifetimeScope("AutofacRequestLifetime");
}
/// <summary>
@thecodejunkie
thecodejunkie / gist:5051634
Created February 27, 2013 20:55
Addition to bootstrapper readme file
A [bootstrapper](https://github.com/NancyFx/Nancy/wiki/Bootstrapper) implementation, for the [Nancy](http://nancyfx.org) framework, based on the Autofac inversion of control container.
## Usage
When Nancy detects that the `AutofacNancyBootstrapper` type is available in the AppDomain of your application, it will assume you want to use it, rather than the default one.
The easiest way to get the latest version of `AutofacNancyBootstrapper` into your application is to install the `Nancy.Boostrappers.Autofac` nuget.
## Customizing
@thecodejunkie
thecodejunkie / gist:4985569
Last active December 13, 2015 22:38
Loading any assembly, that references a Nancy assembly, into the application domain
public static void LoadAssembliesWithNancyReferences()
{
if (nancyAssembliesLoaded)
{
return;
}
UpdateAssemblies();
foreach (var directory in GetAssemblyDirectories())
@thecodejunkie
thecodejunkie / gist:4753348
Created February 11, 2013 08:53
Method rewriting on steroids
private void RewriteMethod()
{
if (!this.Method.Equals("POST", StringComparison.OrdinalIgnoreCase))
{
return;
}
var overrides =
new List<Tuple<string, string>>
{
@thecodejunkie
thecodejunkie / gist:4749016
Last active December 12, 2015 09:08
Removing lowest homework grade per student
import pymongo
import sys
connection = pymongo.Connection("mongodb://localhost", safe=True)
db = connection.school
collection = db.students
students = collection.find({})
@thecodejunkie
thecodejunkie / gist:4737661
Created February 8, 2013 09:26
Changing RootPathProvider property to return an instance
protected virtual IRootPathProvider RootPathProviderNew
{
get
{
var providerType = AppDomainAssemblyTypeScanner
.TypesOf<IRootPathProvider>()
.SingleOrDefault(type => type != typeof(DefaultRootPathProvider));
if (providerType == null)
{
public abstract class ApplicationRegistrations : IApplicationRegistrations
{
private readonly IList<CollectionTypeRegistration> collectionRegistrations = new List<CollectionTypeRegistration>();
private readonly IList<InstanceRegistration> instanceRegistrations = new List<InstanceRegistration>();
private readonly IList<TypeRegistration> typeRegistrations = new List<TypeRegistration>();
public IEnumerable<CollectionTypeRegistration> CollectionTypeRegistrations
{
get { return this.collectionRegistrations; }
}
public abstract class ApplicationRegistrations : IApplicationRegistrations
{
private readonly IList<CollectionTypeRegistration> collection = new List<CollectionTypeRegistration>();
private readonly IList<InstanceRegistration> instance = new List<InstanceRegistration>();
private readonly IList<TypeRegistration> type = new List<TypeRegistration>();
public IEnumerable<CollectionTypeRegistration> CollectionTypeRegistrations
{
get { return this.collection; }
}
@thecodejunkie
thecodejunkie / gist:4699240
Created February 2, 2013 21:08
Inheriting a generic TypeRegistration with discovery built in
public class TypeRegistration<TRegistration> : TypeRegistration
{
public TypeRegistration()
: base(typeof(TRegistration), GetImplementingType())
{
}
public TypeRegistration(Type defaultImplementingType)
: base(typeof(TRegistration), GetImplementingType(defaultImplementingType))
{
public static class ApplicationRegistrationsExtensions
{
public static TypeRegistration Type<T>(this IApplicationRegistrations registrations)
{
return new TypeRegistration(typeof(T), AppDomainAssemblyTypeScanner.TypesOf(typeof(T)).Single());
}
public static TypeRegistration TypeWithDefault<T>(this IApplicationRegistrations registrations, Type defaultType)
{
var type =