Skip to content

Instantly share code, notes, and snippets.

Avatar

Patrick Lioi plioi

  • Austin, TX
View GitHub Profile
@plioi
plioi / Maybe.cs
Last active May 16, 2020
Shorthand for the int.TryParse(string, out int value) pattern, useful when the C# 8 "Nullable Reference Types" feature is enabled.
View Maybe.cs
using System;
using System.Diagnostics.CodeAnalysis;
public static class Maybe
{
public static bool Try<T>(Func<T> maybeGetValue, [NotNullWhen(true)] out T output)
{
output = maybeGetValue();
return output != null;
View ComplexTypes.cs
public abstract class Entity
{
public Guid Id { get; set; }
}
public class Customer : Entity
{
public Customer()
{
Orders = new List<Order>();
View ExampleDbContext.cs
public class ExampleDbContext : DbContext
{
private DbContextTransaction _currentTransaction;
...
public void BeginTransaction()
{
if (_currentTransaction != null)
return;
View Assertions.cs
public static class Assertions
{
public static void ShouldMatch<T>(this T actual, T expected)
=> Json(actual).ShouldEqual(Json(expected), "Expected two objects to match on all properties.");
private static string Json<T>(T actual)
=> JsonConvert.SerializeObject(actual, Formatting.Indented);
...
}
View Testing.cs
public static class Testing
{
private static IContainer Container => TestDependencyScope.CurrentNestedContainer;
public static T Resolve<T>()
{
return Container.GetInstance<T>();
}
public static object Resolve(Type type)
View AutoFixtureParameterSource.cs
public class AutoFixtureParameterSource : ParameterSource
{
public IEnumerable<object[]> GetParameters(MethodInfo method)
{
// Produces a randomly-populated object for each
// parameter declared on the test method, using
// a Fixture that has our customizations.
var fixture = new Fixture();
View NestedContainerPerCase.cs
public class NestedContainerPerCase : CaseBehavior
{
public void Execute(Case context, Action next)
{
TestDependencyScope.Begin();
next();
TestDependencyScope.End();
}
}
View IoC.cs
public static class IoC
{
private static readonly Lazy<IContainer> Bootstrapper = new Lazy<IContainer>(Initialize, true);
public static IContainer Container => Bootstrapper.Value;
private static IContainer Initialize()
{
return new Container(cfg =>
{
View ResetDatabase.cs
public class ResetDatabase : CaseBehavior
{
public void Execute(Case context, Action next)
{
var checkpoint = new Checkpoint
{
SchemasToExclude = new[] { "RoundhousE" },
TablesToIgnore = new[] { "sysdiagrams" }
};
View InitializeAutoMapper.cs
public class InitializeAutoMapper : ClassBehavior
{
public void Execute(Class context, Action next)
{
AutoMapperBootstrapper.Initialize();
next();
}
}
You can’t perform that action at this time.