Skip to content

Instantly share code, notes, and snippets.

View cammerman's full-sized avatar

Chris Ammerman cammerman

View GitHub Profile
Customer customer =
A.BuilderFor<Customer>().With(new {
GivenName = "John",
FamilyName = "Doe",
Address = A.BuilderFor<Address>().With(new {
FirstLine = "123 Main Street",
City = "Nashville",
State = "TN"
})
});
define(
['underscore'],
function (_) {
// Outputs "true"
console.log(_.deepClone !== undefined);
}
);
@cammerman
cammerman / TypeExtensions.cs
Created November 6, 2012 19:21
Type extensions to help check for generic closure.
public static class TypeExtensions
{
public static bool Closes(this Type type, Type checkType)
{
if (!checkType.IsGenericTypeDefinition)
throw new ArgumentException("Type being checked against is not an open generic type.");
return
type.IsGenericType
&& type.GetGenericTypeDefinition() == checkType;
@cammerman
cammerman / test.cs
Created October 3, 2012 20:02
Simple.Data stored function mocking
// test setup
var adapter = new InMemoryAdapter();
var stubId = 15;
adapter.AddFunction<int>("GenerateId", () => stubId); // A breakpoint here inside the lambda *does* break when we call it later
Database.UseMockAdapter(adapter);
dynamic db = Database.Open();
var resultId = db.GenerateId().ReturnValue;
@cammerman
cammerman / OnErrorResumeNextPipeline.cs
Created August 15, 2011 19:45
OnErrorResumeNext in .NET
void IgnoreException(Action operation)
{
try { operation(); }
catch (Exception) { }
}
void OnErrorResumeNext(List<Action> operations)
{
operations.ForEach(op => IgnoreException(op));
}
@cammerman
cammerman / DataContext.cs
Created August 9, 2011 03:57
Lifetime objects
public class DataContext : IDataContext
{
public DataContext(ISessionProvider sessionProvider)
{
this.SessionProvider = sessionProvider.ThrowIfNullArgument("sessionProvider");
}
private ISessionProvider SessionProvider { get; set;}
protected ISessionFacade Session
@cammerman
cammerman / ExtMethod.cs
Created August 2, 2011 17:00
IEnumerable Null Check
public static class IEnumerableExtensions
{
public static IEnumerable<T> EmptyIfNull<T>(this IEnumerable<T> source)
{
return source ?? Enumerable.Empty<T>();
}
}
@cammerman
cammerman / Latch.cs
Created July 27, 2011 19:15
Idea for a double-checked lock helper object.
// Latch definition
public class DoubleCheckLockLatch
{
private readonly Object _lockSync = new Object();
private readonly Func<bool> _isLatchClosed;
public DoubleCheckLockLatch(Func<bool> isLatchClosed)
{
_isLatchClosed = isLatchClosed;
}
@cammerman
cammerman / LifetimeScopeSyntaxSample.cs
Created May 15, 2011 04:54
Sample desired syntax for declarative definition of bound lifetime scopes.
// Container context tags
enum EContainerContext
{
UnitOfWork,
Plugin,
MultiInstanceWindow
}
// Registration code for an object that owns a lifetime scope
builder.RegisterType<MyDbContext>()
@cammerman
cammerman / ConfigLoader.cs
Created March 1, 2011 03:53
Configuration data class.
internal interface IConfigLoader
{
IConfigSettings LoadConfig();
}
internal class ConfigLoader : IConfigLoader
{
public ConfigLoader()
{
}