Skip to content

Instantly share code, notes, and snippets.

View jarrettmeyer's full-sized avatar

Jarrett Meyer jarrettmeyer

View GitHub Profile
@jarrettmeyer
jarrettmeyer / HtmlHelpers.cs
Last active December 28, 2015 17:59
An HTML Helper for creating an array of checkboxes.
public static class HtmlHelpers
{
public static IHtmlString ArrayCheckBoxFor<TModel, TArray>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TArray[]>> expression, TArray value)
{
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
var tagBuilder = new TagBuilder("input");
tagBuilder.Attributes.Add("type", "checkbox");
tagBuilder.Attributes.Add("name", metadata.PropertyName);
tagBuilder.Attributes.Add("value", value.ToString());
public interface IWhateverDataTableProvider
{
DataTable GetData();
}
public class WhateverDataTableProvider : IWhateverDataTableProvider
{
public DataTable GetData()
{
// put all that hard-coded, hard to test stuff here...
// An example of how to work with an array in a non-blocking
// loop.
// This will store our results.
var result = {
count: 0,
sum: 0,
toString: function () {
return "Count: " + result.count + ", Sum: " + result.sum;
}
@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
class X
def do_something
'**PUBLIC**'
end
private
def do_something_privately
'--private--'
end
@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; }
}
@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 / 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()
{
public class DBTest : IDisposable
{
private DataContext dataContext;
public DBTest()
{
dataContext = new DataContext();
}
public void Dispose()
@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;