Skip to content

Instantly share code, notes, and snippets.

@jeffhandley
jeffhandley / SingerTests.cs
Created November 2, 2011 22:50
How to validate an object
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace QuickTests
{
[TestClass]
public class SingerTests
{
@jeffhandley
jeffhandley / ExceptionHandlingUtility.cs
Created November 3, 2011 01:06
Checking for a fatal exception
internal static class ExceptionHandlingUtility
{
/// <summary>
/// Determines if an <see cref="Exception"/> is fatal and therefore should not be handled.
/// </summary>
/// <example>
/// try
/// {
/// // Code that may throw
/// }
<!-- What you can do today -->
<label for="firstname" class="@(ModelState.IsValidField("firstname") ? "" : "invalid")">First Name</label>
<!-- A proposed helper method that needs a name -->
<label for="firstname" class="@Validation.InvalidClass("firstname", "invalid")">First Name</label>
<!-- With a property that defines the invalid class name, which could be defaulted to "invalid" -->
@{ Validation.InvalidClassName = "invalid"; }
<label for="firstname" class="@Validation.InvalidClass("firstname")">First Name</label>
@jeffhandley
jeffhandley / _layout.cshtml
Created February 28, 2012 20:35
Bundling
<!-- Raw -->
<link href="~/Content/themes/base/jquery.ui.all.css" rel="stylesheet" type="text/css" />
<link href="~/Content/Site.css" rel="stylesheet" type="text/css" />
<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<script src="~/Scripts/jquery-1.6.2.min.js"></script>
<script src="~/Scripts/jquery-ui-1.8.11.js"></script>
<script src="~/Scripts/modernizr-2.0.6-development-only.js"></script>
<script src="~/Scripts/AjaxLogin.js"></script>
@jeffhandley
jeffhandley / odata.sql
Created August 23, 2012 21:37
OData / EF vs. Rewritten queries
SELECT TOP (30
[Project1].[PackageRegistrationKey] AS [PackageRegistrationKey], 
[Project1].[Id] AS [Id], 
[Project1].[Version] AS [Version], 
[Project1].[FlattenedAuthors] AS [FlattenedAuthors], 
[Project1].[Copyright] AS [Copyright], 
[Project1].[Created] AS [Created], 
[Project1].[FlattenedDependencies] AS [FlattenedDependencies], 
[Project1].[Description] AS [Description], 
[Project1].[DownloadCount1] AS [DownloadCount], 
@jeffhandley
jeffhandley / Get-RandomPassword.ps1
Created August 22, 2013 21:14
Get a new random password
<#
.SYNOPSIS
Returns a random, timestamped, password
#>
# Base64-encode the Guid to add some additional characters
[DateTime]::Now.ToString("MMMddyy") + "!" + [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes([Guid]::NewGuid().ToString()))
@jeffhandley
jeffhandley / gist:7883875
Created December 10, 2013 00:43
Named objects in a graph
// What Example 46 shows here: http://json-ld.org/spec/latest/json-ld
"@graph" : [
{
"@id": "#homer",
"http://example.com/vocab#name": "Homer"
},
{
"@id": "#bart",
"http://example.com/vocab#name": "Bart",
"http://example.com/vocab#parent": { "@id": "#homer" }
@jeffhandley
jeffhandley / page.js
Last active February 13, 2016 18:02
A pattern for server-side async data loading with React components
import React from 'react';
export default (req, res, callback) => {
// Do async work, consuming data off req if needed
// Potentially set headers or other data on res
// When all the data is loaded, call the callback with the component
callback(React.createClass({
render() {
return (
<html>
@jeffhandley
jeffhandley / Greeting.jsx
Last active July 20, 2016 13:45
Is there a better way to build this React component other than using ReactDOMServer.renderToStaticMarkup and dangerouslySetInnerHTML?
// Returns a translated form of "Welcome back, <strong>Jeff</strong>. It's good to see you again."
// There are several messages that can be supplied and they can have different sentence structures
// Regardless of the message, the name needs to be wrapped in a <strong> tag
import React from 'react';
import ReactDOMServer from 'react-dom/server';
export default React.createClass({
displayName: 'Greeting',
@jeffhandley
jeffhandley / package.json
Last active September 19, 2016 04:35
webpack configs
{
"scripts": {
"build:core": "babel src -d lib && webpack",
"build": "NODE_ENV=production npm run build:core",
"postbuild": "echo 'Build Complete'",
"predev": "webpack --config webpack.dev.config.babel.js",
"dev": "NODE_ENV=development babel-node src/server",
"start": "nodemon --watch src/ -e js,jsx --exec npm run dev"
},
"devDependencies": {