Skip to content

Instantly share code, notes, and snippets.

@SamLeach
SamLeach / log4mongo-net.json
Last active August 29, 2015 14:07
log4mongo-net sample document
{
"_id" : ObjectId("54256f999d663727782e9569"),
"timestamp" : ISODate("2014-09-26T13:52:25.155Z"),
"level" : "INFO",
"thread" : "7",
"logger" : "Sam.MongoLogging.Logging.Logger",
"message" : "Home"
}
@SamLeach
SamLeach / electorateBadge.cs
Created September 28, 2014 18:12
StackOverflow Electorate Badge award
private bool AwardElectorateBadge(User user)
{
const int MinimumRequiredQuestionVotes = 600;
return user.Votes.Questions >= MinimumRequiredQuestionVotes &&
user.Votes.Total <= user.Votes.Questions * 4;
}
@SamLeach
SamLeach / convertToCapped.js
Created September 28, 2014 19:15
convert collection to capped collection
// size in bytes
db.runCommand({"convertToCapped": "logs", size: 100000});
@SamLeach
SamLeach / safeApplyCurrentValues.cs
Created October 2, 2014 09:15
Entity Framework 4 Apply current values
if (entity.EntityKey != null)
{
ObjectStateEntry stateEntry;
bool isPresent = this.Entities.ObjectStateManager.TryGetObjectStateEntry(
entity.EntityKey,
out stateEntry);
if (isPresent)
{
this.Entities.ApplyCurrentValues("Entity", entity);
}
@SamLeach
SamLeach / copyEntity.cs
Created October 2, 2014 09:18
Entity Framework 4 Copy Entity
public T CopyEntity<T>(T entity, bool copyKeys = false) where T : EntityObject
{
var clone = this.Context.CreateObject<T>();
var pis = entity.GetType().GetProperties();
foreach (var pi in pis)
{
var attrs = (EdmScalarPropertyAttribute[])pi.GetCustomAttributes(typeof(EdmScalarPropertyAttribute), false);
foreach (EdmScalarPropertyAttribute attr in attrs)
@SamLeach
SamLeach / executeSql.cs
Created October 2, 2014 09:19
Entity Framework 4 Execute Sql
public void ExecuteSql(string sql)
{
var entityConnection = (System.Data.EntityClient.EntityConnection)Context.Connection;
DbConnection conn = entityConnection.StoreConnection;
ConnectionState initialState = conn.State;
try
{
if (initialState != ConnectionState.Open)
conn.Open();
using (DbCommand cmd = conn.CreateCommand())
@SamLeach
SamLeach / ThrowIfNull.cs
Created October 10, 2014 15:26
Throw argument null exception if null
public static class ObjectExtensions
{
public static object ThrowIfNull(this object argument, string argumentName)
{
if (argument == null)
{
throw new ArgumentNullException(argumentName);
}
return argument;
}
@SamLeach
SamLeach / nullArguments.cs
Created October 10, 2014 15:30
Null arguments class
public MyClass(
IDependency1 dependency1,
IDependency2 dependency2,
IDependency3 dependency3,
IDependency4 dependency4)
{
if (dependency1 == null)
{
throw new ArgumentNullException("dependency1");
}
@SamLeach
SamLeach / classWithThrowIfNull.cs
Created October 10, 2014 15:34
Dummy class using ThrowIfNull extension method
public MyClass(
IDependency1 dependency1,
IDependency2 dependency2,
IDependency3 dependency3,
IDependency4 dependency4)
{
dependency1.ThrowIfNull("dependency1");
dependency2.ThrowIfNull("dependency2");
dependency3.ThrowIfNull("dependency3");
dependency4.ThrowIfNull("dependency4");
@SamLeach
SamLeach / GuardAgainstNulls.cs
Created October 10, 2014 15:39
Guard Against Null
static class Guard
{
public static void AgainstNulls(object parameter, string name)
{
if (parameter == null)
{
throw new ArgumentNullException(name);
}
}
}