Created
November 3, 2011 14:08
-
-
Save rdingwall/1336570 to your computer and use it in GitHub Desktop.
Rich's handy C# Extension Methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ReSharper disable CheckNamespace | |
namespace System.Configuration | |
// ReSharper restore CheckNamespace | |
{ | |
public static class ConnectionStringSettingsCollectionExtensions | |
{ | |
// Connection strings by default just return null. We want something | |
// a bit more snappy. | |
public static string GetOrThrow(this ConnectionStringSettingsCollection connectionStrings, string name) | |
{ | |
if (String.IsNullOrWhiteSpace(name)) throw new ArgumentException("name was null or empty.", "name"); | |
var connectionString = connectionStrings[name]; | |
if (connectionString == null) | |
throw new Exception(String.Format("The connection string '{0}' was not found.", name)); | |
return connectionString.ConnectionString; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ReSharper disable CheckNamespace | |
namespace System | |
// ReSharper restore CheckNamespace | |
{ | |
public static class DateTimeExtensions | |
{ | |
public static bool IsWeekend(this DateTime date) | |
{ | |
return (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday); | |
} | |
public static Guid ToDeterministicGuid(this DateTime date) | |
{ | |
var str = String.Format("{0:yyyyMMdd-HHmm-00ss-ffff}-000000000000", date); | |
return Guid.Parse(str); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ReSharper disable CheckNamespace | |
namespace System.Collections.Generic | |
// ReSharper restore CheckNamespace | |
{ | |
public static class EnumeratorExtensions | |
{ | |
public static IEnumerable<T> ToIEnumerable<T>(this IEnumerator<T> enumerator) | |
{ | |
while (enumerator.MoveNext()) | |
yield return enumerator.Current; | |
yield break; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
// ReSharper disable CheckNamespace | |
namespace NHibernate | |
// ReSharper restore CheckNamespace | |
{ | |
public static class NHibernateExtensions | |
{ | |
const int DefaultBatchSize = 100; | |
public static void BatchInsertWithTransactions<T>(this IStatelessSession session, IEnumerable<T> items) | |
{ | |
session.BatchInsertWithTransactions(items, DefaultBatchSize); | |
} | |
public static void BatchInsertWithTransactions<T>(this IStatelessSession session, IEnumerable<T> items, int batchSize) | |
{ | |
if (session == null) throw new ArgumentNullException("session"); | |
if (items == null) throw new ArgumentNullException("items"); | |
if (batchSize < 1) | |
throw new ArgumentOutOfRangeException("batchSize", "Cannot have a batch size smaller than 1."); | |
do | |
{ | |
using (var transaction = session.BeginTransaction()) | |
{ | |
foreach (var item in items.Take(batchSize)) | |
session.Insert(item); | |
transaction.Commit(); | |
} | |
items = items.Skip(batchSize); | |
} | |
while (items.Any()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment