Skip to content

Instantly share code, notes, and snippets.

View ronnieoverby's full-sized avatar
🏠
Working from home

Ronnie Overby ronnieoverby

🏠
Working from home
  • Olo
  • Lexington, NC
View GitHub Profile

do it werk?

internal static IEnumerable<string> ReadLines(this string s)
{
string line;
using (var sr = new StringReader(s))
while ((line = sr.ReadLine()) != null)
yield return line;
}
public class ClutchProvider : WrappedInsightDbProvider
{
private ClutchProvider()
{
}
public override IEnumerable<Type> SupportedTypes
{
get
@ronnieoverby
ronnieoverby / App.xaml.cs
Last active September 16, 2023 12:08
wpf global exception handling
// somewhere early in App.xaml.cs
AppDomain.CurrentDomain.UnhandledException += (s, e) =>
LogUnhandledException((Exception) e.ExceptionObject, "AppDomain.CurrentDomain.UnhandledException");
DispatcherUnhandledException += (s, e) =>
LogUnhandledException(e.Exception, "Application.Current.DispatcherUnhandledException");
TaskScheduler.UnobservedTaskException += (s, e) =>
LogUnhandledException(e.Exception, "TaskScheduler.UnobservedTaskException");
@ronnieoverby
ronnieoverby / tests.cs
Last active October 12, 2016 03:58
failing test to demo fluent validation bug
// ReSharper disable InconsistentNaming
using FluentValidation;
using NUnit.Framework;
using FluentValidation.TestHelper;
namespace FailingTest
{
public class Hello
{
static void OtherInsertOverloadExample(BulkInserter<Person> bulkInserter, IEnumerable<Person> people)
{
// sql connection is being managed externally
foreach (var person in people)
{
// contrived example... do some work on each person
person.Salary = DetermineSalary(person);
// this will add the object to a list managed by BulkInserter<T>
// the data will not be written to the database until the list count == batch size
private static Func<T, object> CreatePropertyGetter(PropertyInfo propertyInfo)
{
if (typeof(T) != propertyInfo.DeclaringType)
throw new ArgumentException();
var instance = Expression.Parameter(propertyInfo.DeclaringType, "i");
var property = Expression.Property(instance, propertyInfo);
var convert = Expression.TypeAs(property, typeof(object));
return (Func<T, object>)Expression.Lambda(convert, instance).Compile();
}
var bi = new BulkInserter<Person>(sqlConnection, "People");
bi.PreBulkInsert += (sender, args) =>
{
// args.DataTable - this is the actual datatable being written to the server
// you can modify the rows that will be written
// args.Items - this is an array of the objects used to create the datatable
// that will be written. modifying these objects at this point has no effect
// on the bulk insert operation, but it's nice to have them if you need them
};
static void BulkInserterExample(string connectionString, IEnumerable<Person> people)
{
using (var sqlConnection = new SqlConnection(connectionString))
{
var bi = new BulkInserter<Person>(sqlConnection, "People", batchSize: 2000);
sqlConnection.Open();
bi.Insert(people);
sqlConnection.Close();
}
}
static void SqlBulkCopyExample(string connectionString, IEnumerable<Person> people)
{
using (var dataTable = new DataTable())
{
dataTable.Columns.Add("Id");
dataTable.Columns.Add("Name");
dataTable.Columns.Add("SpouseName");
dataTable.Columns.Add("BirthDate");
dataTable.Columns.Add("Salary");
dataTable.Columns.Add("IsMarried");