Skip to content

Instantly share code, notes, and snippets.

Avatar

Chris Chilvers chilversc

View GitHub Profile
@chilversc
chilversc / gist:9464280
Last active Aug 29, 2015
MSSQL 2008 allowing nulls in unique constraints.
View gist:9464280
-- 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);
View gist:f42d89ed1b476aa769f3
WindsorContainer container;
void Main()
{
CompositionRoot ();
Test ();
}
void Test ()
{
@chilversc
chilversc / ValueTypeHandler.cs
Created Jul 25, 2014
Dapper type handler nullable types
View ValueTypeHandler.cs
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 Aug 29, 2015
Dapper type handler with nullable, now obsolete. Dapper handles this natively as of issue #136
View example.cs
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 Aug 29, 2015
Rebus automatic message subscribtion
View gist:76246924d25fe6fc1b48
#define NONEST
RebusBus bus = new RebusBus ();
WindsorContainer container;
void Main()
{
BuildContainer ();
Subscribe (bus, container.Kernel);
bus.subscribed.Dump ();
@chilversc
chilversc / SimpleModelBinderBase.cs
Created Nov 13, 2014
Base class for custom model binder that binds a single text field to a single value
View SimpleModelBinderBase.cs
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 Aug 29, 2015
SingleToInt32Bits and Int32BitsToSingle
View Gist
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 Jan 13, 2015
T-SQL nesting transactions for stored procedures
View gist:b085ec9885221edb8692
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 Jan 26, 2015
Sorting visual studio project files
View gist:03da811ccc973794f961
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 Aug 29, 2015
Bootstrap, overriding tables with cascading styles is too verbose
View gist:2a1da3e6bc5689f89f9d
/*
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,