Skip to content

Instantly share code, notes, and snippets.

View DForshner's full-sized avatar

David Forshner DForshner

View GitHub Profile
// Init vector of ints
static const int values[] = {6,2,7,9,5,8,1,4,3,0};
vector<int> v (values, values + sizeof(values) / sizeof(values[0]) );
// Convert to string
std::stringstream ss;
for (int n : v)
ss << n;
string str = ss.str();
foreach (var item in yourList.Select((Value, Index) => new { Value, Index }))
{
Console.WriteLine("Value=" + item.Value + ", Index=" + item.Index);
}
@DForshner
DForshner / SimpleStratagyFactory.cs
Last active December 18, 2015 02:19
A factory that uses a variation of the strategy pattern to create new products based on a product key and passes in a dependency using constructor injection. In this particular design the factory gets configured once during application start. Contains the example classes and some tests.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
namespace SimpleStratagyFactory
{
#region Classes
/// <summary>
/// Enumeration of different types of products to create
@DForshner
DForshner / PropertyComparer.cs
Last active May 30, 2022 01:08
PropertyComparer is a custom IEqualityComparer that compares the value of a single or multiple properties between instances of the same class. it uses expressions instead of reflection. This file contains both the class and some unit tests.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
namespace PropertyComparer
{
#region PropertyComparer Class
public class PropertyComparer<T> : IEqualityComparer<T>
@DForshner
DForshner / SinglePropertyComparer.cs
Last active December 17, 2015 23:19
SinglePropertyComparer - Custom IEqualityComparer that compares the value of a single property between instances of the same class. Uses expressions instead of reflection. Contains both the class and some unit tests.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
namespace SinglePropertyComparer
{
#region SinglePropertyComparer
public class SinglePropertyComparer<T> : IEqualityComparer<T>
@DForshner
DForshner / AlwaysCreateDatabaseWithTransactionIsolationContextInitializer.cs
Last active December 17, 2015 17:29
This class duplicates the functionality of the built in DropCreateDatabaseAlways database initializer but it alters the new database to enable allow snapshot isolation.
using System.Data.Entity;
using System.Data.SqlClient;
namespace CustomDatabaseInitializer
{
/// <summary>
/// This class duplicates the functionality of the built in DropCreateDatabaseAlways database
/// initializer but it alters the new database to enable allow snapshot isolation.
/// </summary>
public abstract class AlwaysCreateDatabaseWithTransactionIsolationContextInitializer<TContext> : IDatabaseInitializer<TContext> where TContext : System.Data.Entity.DbContext
@DForshner
DForshner / ListSnapshotIsolationAndReadCommited.sql
Last active December 17, 2015 17:19
List the snapshot isolation and read committed snapshot isolation level of all databases. Change the read committed transaction level behavior to use snapshot isolation for reads.
-- List the snapshot isolation and read committed snapshot isolation level of all databases.
SELECT name, snapshot_isolation_state_desc AS 'Snapshot Isolation Enabled', is_read_committed_snapshot_on AS 'Read Commited Snapshot'
FROM sys.databases
-- Change the read committed transaction level behavior to use snapshot isolation for reads.
-- Reader gets last committed data and will not block / Writer will wait until lock is released.
ALTER DATABASE [Foo] SET READ_COMMITTED_SNAPSHOT ON
@DForshner
DForshner / PhantomTypes.cs
Last active December 17, 2015 03:08
Phantom Types In C#
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// Compile time (Sigil) types. (Or how to abuse static typing)
/// </summary>
public interface ISides { } // Base interface
public interface IZero : ISides { } // Interface that indicates zero sides
public interface IOnePlus<TLen> : ISides where TLen : ISides { } // Interface that gets layered on top as sides are added to the polygon
@DForshner
DForshner / NaiveFibonacciSequenceGenerator.cs
Last active December 17, 2015 02:08
C# IEnumerable fibonacci sequence
using System;
using System.Collections;
public class NaiveFibonacciSequenceGenerator : IEnumerable
{
private readonly int sequenceSize;
public NaiveFibonacciSequenceGenerator(int sequenceSize)
{
this.sequenceSize = sequenceSize;
@DForshner
DForshner / PrototypePattern.js
Last active December 17, 2015 00:59
JavaScript Prototype Pattern
// The Prototype pattern allows objects to be created
// based on a template of an existing object using cloning.
var Dragon = {
name: "TROGDOR"
};
//Object.create takes its first argument and applies it to the prototype of your new object.
var myDragon = Object.create(Dragon);