Skip to content

Instantly share code, notes, and snippets.

View chilversc's full-sized avatar

Chris Chilvers chilversc

View GitHub Profile
@chilversc
chilversc / gist:9464280
Last active August 29, 2015 13:57
MSSQL 2008 allowing nulls in unique constraints.
-- See http://technet.microsoft.com/en-us/library/ms188783.aspx
-- "For UNIQUE indexes, only the selected rows must have unique index values."
CREATE TABLE foo (pk int PRIMARY KEY IDENTITY, x int NULL);
GO
CREATE UNIQUE INDEX U_Foo_x ON foo (x) WHERE x is not null;
GO
INSERT INTO foo (x) VALUES (1);
WindsorContainer container;
void Main()
{
CompositionRoot ();
Test ();
}
void Test ()
{
@chilversc
chilversc / ValueTypeHandler.cs
Created July 25, 2014 13:34
Dapper type handler nullable types
abstract class ValueTypeHandler<T> : SqlMapper.ITypeHandler where T : struct
{
public abstract T Parse (object value);
public abstract void SetValue(IDbDataParameter parameter, T value);
object SqlMapper.ITypeHandler.Parse (Type destinationType, object value)
{
if (value == null) {
return null;
} else if (value is DBNull) {
@chilversc
chilversc / example.cs
Last active August 29, 2015 14:04
Dapper type handler with nullable, now obsolete. Dapper handles this natively as of issue #136
void Main()
{
// SqlMapper.AddTypeHandler<T>(TypeHandler<T>) does not register the nullable version
// Instead, use the SqlMapper.AddTypeHandler(Type, ITypeHandler) overload
SqlMapper.AddTypeHandler(typeof(LocalDate), LocalDateHandler.Default);
// Without this line a NullReferenceException is thrown
SqlMapper.AddTypeHandler(typeof(LocalDate?), LocalDateHandler.Default);
using (var db = new SqlConnection (@"Data Source=.;Initial Catalog=tempdb;Integrated Security=True")) {
@chilversc
chilversc / gist:76246924d25fe6fc1b48
Last active August 29, 2015 14:07
Rebus automatic message subscribtion
#define NONEST
RebusBus bus = new RebusBus ();
WindsorContainer container;
void Main()
{
BuildContainer ();
Subscribe (bus, container.Kernel);
bus.subscribed.Dump ();
@chilversc
chilversc / SimpleModelBinderBase.cs
Created November 13, 2014 17:34
Base class for custom model binder that binds a single text field to a single value
public abstract class SimpleModelBinderBase : IModelBinder
{
public object BindModel (ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var key = bindingContext.ModelName;
var result = bindingContext.ValueProvider.GetValue (key);
if (result == null) {
return null;
}
@chilversc
chilversc / Gist
Last active August 29, 2015 14:09
SingleToInt32Bits and Int32BitsToSingle
void Main()
{
SingleToInt32Bits (5.0f).Dump ();
Int32BitsToSingle (1084227584).Dump ();
}
public static int SingleToInt32Bits (float f)
{
return new FloatConvert { F = f }.I;
}
@chilversc
chilversc / gist:b085ec9885221edb8692
Created January 13, 2015 17:31
T-SQL nesting transactions for stored procedures
DECLARE @mark CHAR(32) = replace(newid(), '-', '');
DECLARE @trans INT = @@TRANCOUNT;
IF @trans = 0
BEGIN TRANSACTION @mark;
ELSE
SAVE TRANSACTION @mark;
BEGIN TRY
-- do work here
@chilversc
chilversc / gist:03da811ccc973794f961
Created January 26, 2015 11:57
Sorting visual studio project files
static XNamespace ns = "http://schemas.microsoft.com/developer/msbuild/2003";
static XName ItemGroup = ns + "ItemGroup";
static XName Reference = ns + "Reference";
static XName ProjectReference = ns + "ProjectReference";
static XName Condition = "Condition";
static XName Include = "Include";
void Main(string[] args)
{
if (args.Length != 1) {
@chilversc
chilversc / gist:2a1da3e6bc5689f89f9d
Last active August 29, 2015 14:18
Bootstrap, overriding tables with cascading styles is too verbose
/*
Due to boostrap's high specificity on td styles
I cannot see a shorter method to override this.
*/
.table.vert-align-mid > thead > tr > td,
.table.vert-align-mid > thead > tr > th,
.table.vert-align-mid > tbody > tr > td,
.table.vert-align-mid > tbody > tr > th,
.table.vert-align-mid > tfoot > tr > td,