Skip to content

Instantly share code, notes, and snippets.

View thomasjo's full-sized avatar
🍉

Thomas Johansen thomasjo

🍉
View GitHub Profile
// Pretend this is a LINQ to SQL entity
public class Product
{
public string Name { get; private set; }
public IEnumerable<ProductTranslation> Translations { get; private set; }
public Product(string name, IEnumerable<ProductTranslation> translations)
{
Name = name;
// Tiniest BDD DSL/framework around?
// Licensed under MSPL/FreeBSD/ISC or any other OSS license you want
// Original idea from http://blog.kjempekjekt.com/2009/08/13/ultra-tiny-given-when-then-dsl-snippet/,
// turned into generic base class by Jonas Follesø.
public abstract class BDD<T> where T : BDD<T>
{
protected T Given { get { return (T)this; } }
protected T And { get { return (T)this; } }
protected T When { get { return (T)this; } }
require 'spec'
class DocsToCode
def initialize()
puts "hello"
end
def docs_in(path)
[]
end
def code_in(doc)
require 'spec'
class DocsToCode
def docs_in(path)
[]
end
def code_in(doc)
""
end
def extract(path)
@thomasjo
thomasjo / Parallelizer.cs
Created November 9, 2010 23:06
Very naïve implemention of parallelization in C# 3.
public class Parallelizer
{
private int actionCount;
private int completedActionsCount;
private volatile bool allActionsCompleted;
private ICollection<Exception> exceptions;
public IEnumerable<Exception> Exceptions
{
get { return exceptions; }
@thomasjo
thomasjo / meta.rb
Created November 13, 2010 13:34
Ruby metaclass example
class Greeter
def say_hello
puts 'Hello World!'
end
end
g1 = Greeter.new
g2 = Greeter.new
class << g2
def say_hello
@thomasjo
thomasjo / killme.cs
Created February 14, 2011 06:47
Horrendous hack
if (IsPostBack)
{
ModelEntity.EmailId = Convert.ToInt32(Request[ddlEmails.UniqueID + "$field"]);
}
@thomasjo
thomasjo / refactoring101.cs
Created February 14, 2011 23:39
Let's improve this code (in two different ways.)
// Original code - let's improve it...
public bool IsTelephoneNumberInUse(int clientId, int telephoneId)
{
ISubscriptionService service = GetService<ISubscriptionService>();
IList<Subscription> subs = service.GetSubscriptions(clientId, false);
bool inUse = false;
foreach (Subscription sub in subs) {
if (sub.ContactPhoneId == telephoneId) {
inUse = true;
prin·ci·ple/ˈprinsəpəl/Noun
1. A fundamental truth or proposition that serves as the foundation for a system of belief or behavior or for a chain of reasoning.
2. A rule or belief governing one's personal behavior.
@thomasjo
thomasjo / Chirpy.ConsoleRedux.cs
Created April 5, 2011 23:40
NOTE: This code sample is not great, but it works.
using System;
using System.IO;
using Zippy.Chirp;
using Zippy.Chirp.Engines;
namespace Chirpy.ConsoleRedux
{
public class Program
{
public static void Main(string[] args)