Skip to content

Instantly share code, notes, and snippets.

[assembly: WebActivator.PreApplicationStartMethod(typeof(Mvc4Sandbox.App_Start.StructuremapMvc), "Start")]
public class RedirectedConsole : IDisposable
{
readonly TextWriter outBefore;
readonly TextWriter errBefore;
readonly StringWriter console;
public RedirectedConsole()
{
console = new StringWriter();
outBefore = Console.Out;
@plioi
plioi / linebreaks_wp.cs
Created July 11, 2013 00:32
Wordpress's automatic <p> tag insertion meat grinder, portedn to C#.
static string linebreaks_wp(string body)
{
if (body.Trim() == "")
return "";
//Ensure all newlines are simply \n and that we end with a \n
body = body.Replace("\r\n", "\n")
.Replace("\r", "\n");
body = body + "\n";
@plioi
plioi / Maybe.cs
Last active May 16, 2020 14:51
Shorthand for the int.TryParse(string, out int value) pattern, useful when the C# 8 "Nullable Reference Types" feature is enabled.
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;
namespace ContactList.Core.Domain
{
using System;
public abstract class Entity
{
public Guid Id { get; set; }
}
public class Contact : Entity
public abstract class Entity
{
public Guid Id { get; set; }
}
public class Customer : Entity
{
public Customer()
{
Orders = new List<Order>();
public class ContactsContext : DbContext
{
private DbContextTransaction _currentTransaction;
...
public void BeginTransaction()
{
if (_currentTransaction != null)
return;
public class ExampleDbContext : DbContext
{
private DbContextTransaction _currentTransaction;
...
public void BeginTransaction()
{
if (_currentTransaction != null)
return;
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);
...
}
public class UnitOfWork : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var database = DependencyResolver.Current.GetService<ContactsContext>();
database.BeginTransaction();
}
public override void OnActionExecuted(ActionExecutedContext filterContext)