Skip to content

Instantly share code, notes, and snippets.

View jarrettmeyer's full-sized avatar

Jarrett Meyer jarrettmeyer

View GitHub Profile
@jarrettmeyer
jarrettmeyer / Rakefile
Created May 24, 2012 21:28
Octopress publish drafts
require "rubygems"
require "date"
# Configuration
base_dir = Dir.pwd
blog_dir = "#{base_dir}/blog"
source_dir = "#{blog_dir}/source"
posts_dir = "#{source_dir}/_posts"
git_remote = "origin"
git_branch = "master"
@jarrettmeyer
jarrettmeyer / HasHttpContext.cs
Created May 25, 2012 15:26
Determine if there is a current HTTP context without referencing System.Web
public static bool HasHttpContext
{
get
{
try
{
var assembly = Assembly.Load("System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
Type httpContextType = assembly.GetType("System.Web.HttpContext");
PropertyInfo currentProperty = httpContextType.GetProperty("Current");
object value = currentProperty.GetValue(null, null);
@jarrettmeyer
jarrettmeyer / IPaginate.cs
Created May 29, 2012 17:20
Simple Pagination with MVC3 + Twitter Bootstrap
/// <summary>
/// Indicates that a model provides support for pagination.
/// See http://twitter.github.com/bootstrap/components.html#pagination.
/// </summary>
public interface IPaginate
{
/// <summary>
/// Gets the current page in the record set.
/// </summary>
int Page { get; }
@jarrettmeyer
jarrettmeyer / HttpTest.cs
Created September 10, 2012 22:46
Sample code to run an in-memory HTTP context for unit testing in .NET
internal class HttpTest
{
private static HttpContext httpContext;
private static HttpWorkerRequest httpWorkerRequest;
private static StringBuilder outputStringBuilder;
private static TextWriter output;
private static NameValueCollection httpHeaders;
private static HttpCookieCollection requestCookies;
private static HttpCookieCollection responseCookies;
public class DBTest : IDisposable
{
private DataContext dataContext;
public DBTest()
{
dataContext = new DataContext();
}
public void Dispose()
@jarrettmeyer
jarrettmeyer / NHibernateSessionManager.cs
Last active July 31, 2017 23:26
How I configure NHibernate with Ninject
public class NHibernateSessionManager
{
private const string CONNECTION_STRING_NAME = "...";
private static Configuration configuration;
private static readonly ILog log;
private static ISessionFactory sessionFactory;
static NHibernateSessionManager()
{
@jarrettmeyer
jarrettmeyer / static_methods.cs
Created February 5, 2013 14:20
When to use statics in C#. I'm sure there are other examples, but these are always valid.
// There are three times when I will openly use static methods in C#.
// 1. Providers
// 2. State Machines (variation on provider, really)
// 3. Any time where there *really* is *one* of something.
/// <summary>
/// 1. Providers give you a way to create new objects without using a constructor. Ideally, you would always
/// use Dependency Injection and Inversion of Control. However, these techniques do not work in all situations,
/// so we need a testable fallback. Providers fill this need.
@jarrettmeyer
jarrettmeyer / HttpSession.cs
Created February 17, 2013 14:58
How to work with sessions in .NET
public abstract class HttpSession : IHttpSession
{
protected IKeyValueStore storage;
public virtual int UserId
{
get { return Get<int>("user_id"); }
set { storage["user_id"] = value; }
}
class X
def do_something
'**PUBLIC**'
end
private
def do_something_privately
'--private--'
end
@jarrettmeyer
jarrettmeyer / gist:5355188
Last active December 16, 2015 01:29
Ruby mixin behavior
module A
def do_something
puts "In A"
end
end
module B
def do_something_else
puts "In B"
end