Skip to content

Instantly share code, notes, and snippets.

View michaeljbailey's full-sized avatar

Mike Bailey michaeljbailey

  • Converged Technology Group
  • Long Island, New York
View GitHub Profile
@michaeljbailey
michaeljbailey / TemplateCache.js
Created July 16, 2015 16:45
Provides a general purpose sync-or-async template cache that builds on top of underscore's template engine. I make no guarantees this actually works, as I haven't tested this at all.
(function() {
"use strict";
if (!_) {
throw "Cannot find underscore.js";
}
var TemplateCache = function(baseUrl) {
this.BaseUrl = baseUrl;
this.Templates = {};
@michaeljbailey
michaeljbailey / ContainerDiagnosticExtensions.cs
Created July 15, 2015 15:20
Provides extension method for analyzing the container for any warnings. If any come up, an exception is thrown.
using System;
using System.Linq;
using SimpleInjector;
using SimpleInjector.Diagnostics;
public static class ContainerDiagnosticExtensions
{
public static Registration GetRegistration<TService>(this Container container)
{
return container.GetRegistration(typeof(TService)).Registration;
@michaeljbailey
michaeljbailey / CacheFunctionExtensions.cs
Created July 14, 2015 14:43
Just some simple memoization implementations. Probably done a million times but here it is because I'm too lazy to go searching each time.
using System;
using System.Collections.Generic;
public static class CacheFunctionExtensions
{
public static Func<TResult> Memoize<TResult>(this Func<TResult> function, bool threadSafe = false)
{
var lazy = new Lazy<TResult>(function, threadSafe);
return () => lazy.Value;
}
$('table tr').on('focus', 'textarea[rows][cols]', function(event) {
var $blurFrom = $(event.delegateTarget);
$blurFrom.css("background-color","yellow");
$blurFrom.find("textarea[rows][cols]").each(function(i, element) {
expandHeight($(element));
});
});
$('table tr').on('focusout', 'textarea[rows][cols]', function(event) {
var $blurFrom = $(event.delegateTarget);
@michaeljbailey
michaeljbailey / ExceptionExtensions.cs
Created May 12, 2015 18:12
Unwraps exceptions to the specified exception type
public static class ExceptionExtensions
{
public static TException Unwrap<TException>(this Exception exception) where TException : Exception
{
for (; exception != null; exception = exception.InnerException)
{
var typedException = exception as TException;
if (typedException != null)
{
return typedException;
@michaeljbailey
michaeljbailey / TraceScope.cs
Created May 11, 2015 13:22
Timing for Razor rendering
namespace Timing
{
using System;
using System.Diagnostics;
using System.Web.Mvc;
public class TraceScope : IDisposable
{
private readonly string _name;
private readonly ViewContext _viewContext;
@michaeljbailey
michaeljbailey / Logger.cs
Created April 24, 2015 13:53
Sensible log4net wrapper
using System;
using log4net;
using log4net.Config;
using log4net.Util;
[assembly: XmlConfigurator(Watch = true, ConfigFile = "log4net.config")]
namespace LogWrapper
{
public static class Logger
@michaeljbailey
michaeljbailey / Pager.js
Created April 23, 2015 20:47
Pure JavaScript paging. Makes no assumptions about how you want your page structured. Just new it up, bind it, and go.
(function() {
"use strict";
window.Pager = function (itemSelector, pageSize) {
this.ItemSelector = itemSelector;
this.PageSize = pageSize;
this.CurrentPage = 1;
this.ItemCount = $(itemSelector).length;
this.PageCount = Math.round(this.ItemCount / pageSize + 0.5);
this.Selectors = {
@michaeljbailey
michaeljbailey / FailedJobs.sql
Created April 20, 2015 14:14
Returns any jobs that have failed within the last 7 days and at what date and time they were supposed to have run.
SELECT
[Job].[name] [JobName],
[Category].[name] [CategoryName],
[History].[step_name] [StepName],
CAST(CAST([History].[run_date] AS varchar(8)) AS date) [RunDate],
CAST(CAST([History].[run_time] / 10000 AS varchar(2)) + ':' + CAST(([History].[run_time] % 10000) / 100 AS varchar(2)) + ':' + CAST(([History].[run_time] % 100) AS varchar(2)) AS time(0)) [RunTime],
[History].[message] [Message]
FROM msdb.dbo.sysjobs [Job]
INNER JOIN msdb.dbo.syscategories [Category] ON [Job].[category_id] = [Category].[category_id]
CROSS APPLY
@michaeljbailey
michaeljbailey / FormatterConfig.cs
Created April 15, 2015 21:12
Sane defaults to use for pure JSON Web APIs
public static class FormatConfig
{
private class JsonContentNegotiator : IContentNegotiator
{
private readonly JsonMediaTypeFormatter _jsonFormatter;
public JsonContentNegotiator(JsonMediaTypeFormatter formatter)
{
_jsonFormatter = formatter;
}