Skip to content

Instantly share code, notes, and snippets.

View atsvetkov's full-sized avatar
🎧
tinkering

Alexander Tsvetkov atsvetkov

🎧
tinkering
View GitHub Profile
class Program
{
static void Main(string[] args)
{
BenchmarkRunner.Run<GuardBenchmark>();
}
}
[MemoryDiagnoser]
public class GuardBenchmark
void Foo(SomeReferenceType input)
{
Guard.NotNull(input, nameof(input));
// do something useful
}
public static void NotNull<T>(T value, string name) where T: class
{
if (value == null)
{
throw new ArgumentNullException(name);
}
}
void Foo(SomeReferenceType input)
{
Guard.NotNull(() => input);
// do something useful
}
public static class Guard
{
public static void NotNull<T>(Expression<Func<T>> expression) where T : class
{
var value = /* somehow compile the expression and invoke the delegate to produce a value */;
if (value == null)
{
throw new ArgumentNullException(GetParameterName(expression));
}
}
void Foo(SomeReferenceType input)
{
Guard.NotNull(input);
// do something useful
}
public static class Guard
{
public static void NotNull(object value)
{
if (value == null)
{
throw new ArgumentNullException(/* how to get original argument name here??? */);
}
}
}
void Foo(SomeReferenceType input)
{
if (input == null)
{
throw new ArgumentNullException("input");
}
// if we made it here, input can be safely dereferenced and used
}

Keybase proof

I hereby claim:

  • I am atsvetkov on github.
  • I am atsvetkov (https://keybase.io/atsvetkov) on keybase.
  • I have a public key whose fingerprint is ED30 494E 8268 E48A F3CE 7394 F7FB 7DE3 7BC9 07AA

To claim this, I am signing this object:

@atsvetkov
atsvetkov / Program.cs
Last active May 27, 2017 10:52
DbUp console application sample
static void Main(string[] args)
{
var connectionString = ConfigurationManager.ConnectionStrings["Database"].ConnectionString;
// creates the database if not yet exists
EnsureDatabase.For.SqlDatabase(connectionString);
var upgrader = DeployChanges.To.SqlDatabase(connectionString)
.WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly())
.LogToConsole()