Skip to content

Instantly share code, notes, and snippets.

public static class RouteValueDictionaryExtensions
{
public static RouteValueDictionary FlattenEnumerablesForLink(this RouteValueDictionary routeValueDictionary)
{
var result = new RouteValueDictionary();
foreach (var key in routeValueDictionary.Keys)
{
var value = routeValueDictionary[key];
@christopherbauer
christopherbauer / AutoVerticalGrowTextArea.js
Last active August 29, 2015 14:21
Expand Focused Textareas
$('table tr').on('focus', 'input,textarea', function(event) {
var $blurFrom = $(event.delegateTarget);
$blurFrom.css("background-color","yellow");
$blurFrom.find("textarea[cols]").each(function(i, element) {
expandHeight($(element));
});
});
$('table tr').on('focusout', 'input,textarea', function(event) {
var $blurFrom = $(event.delegateTarget);
@christopherbauer
christopherbauer / EventBus.cs
Last active December 13, 2015 01:49
EventBus pattern in C#
public class EventBus
{
private Dictionary<Type, List<Action<IEvent>>> actions = new Dictionary<Type, List<Action<IEvent>>>();
public void Listen<T>(Action<IEvent> callback) where T : IEvent
{
if (!actions.ContainsKey(typeof(T)))
{
actions.Add(typeof(T), new List<Action<IEvent>>());
@christopherbauer
christopherbauer / HttpSessionStateTests.cs
Last active August 29, 2015 14:22
Mocking IHttpRequestContext for Unit Testing
[TestFixture]
public class when_removing_a_coupon
{
[Test]
public void then_should_remove_the_coupon_from_the_session()
{
// Arrange
var httpSessionStateBase = new Mock<HttpSessionStateBase>();
var coupons = new Dictionary<int, Coupon>();
httpSessionStateBase.SetupGet(session => session["PendingBuyerCoupons"])
@christopherbauer
christopherbauer / ContentResolver.cs
Created June 1, 2015 20:10
Testable, mockable version of a url.content resolver
using System.Web;
using System.Web.Mvc;
namespace KK.Store.DigitalCoupons.Web.Controllers
{
public class ContentResolver : IContentResolver
{
public string Resolve(string imageLocation, HttpRequestBase httpRequestBase)
{
return new UrlHelper(httpRequestBase.RequestContext).Content(imageLocation);
@christopherbauer
christopherbauer / Chevron.css
Last active August 29, 2015 14:22
CSS Shapes
.chevron {
margin-top: 200px;
margin-left: 200px;
position: relative;
width: 100px;
height: 1px;
}
.chevron .left {
position: absolute;
background-color:red;
@christopherbauer
christopherbauer / ExampleController.cs
Last active August 29, 2015 14:23
HttpContextFactory + Test
public class ExampleController : Controller
{
private readonly IConfigurationReader _configurationReader;
private readonly IHttpContextFactory _httpContextFactory;
public MaintenanceController(IConfigurationReader configurationReader, IHttpContextFactory httpContextFactory)
{
_configurationReader = configurationReader;
_httpContextFactory = httpContextFactory;
}
public static class Misspellings
{
public static string ToStrign(this object obj)
{
return obj.ToString();
}
}
[TestFixture]
public class when_getting_the_a_connection
{
[Test]
public void then_should_return_a_new_instance_of_an_odbc_connection()
{
// Arrange
const string connectionString = "Server=Localhost;";
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.ConnectionStrings.ConnectionStrings.Remove("AS400ConnectionString");
[Test]
public void then_should_return_users()
{
// Arrange
var reader = new ConfigurationReader();
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
string a = "A";
string b = "B";
config.AppSettings.Settings.Remove(a);
config.AppSettings.Settings.Remove(b);