Skip to content

Instantly share code, notes, and snippets.

View feanz's full-sized avatar

Richard Forrest feanz

View GitHub Profile
@feanz
feanz / Expire.cs
Created September 18, 2013 14:45
Expire Header WCF Service
public static class Expires
{
public static void Set(int seconds)
{
var response = WebOperationContext.Current.OutGoingResponse;
if(response != null)
{
var expires = DateTime.UtcNow.AddSeconds(seconds).ToEpireString();
@feanz
feanz / ShuffleExtension.cs
Created September 15, 2013 14:12
Shuffle
public static void Shuffle<T>(this T[] array, Random random)
{
int n = array.Length;
while (n > 1)
{
int k = random.Next(n--);
T temp = array[n];
array[n] = array[k];
array[k] = temp;
}
@feanz
feanz / Hash
Created September 10, 2013 22:13
Guid Hash
public static Guid Hash(params object[] args)
{
var composite = "";
foreach (var a in args)
{
composite += a.ToString();
}
Guid result;
using(var md5 = new MD5CryptoServiceProvider())
@feanz
feanz / SymmetricExtensions
Created September 5, 2013 20:12
Symmetric Crypto
public static class SymmetricExtensions
{
private const ushort _iteration = 1000;
private const int _saltSize = 8;
public static byte[] Encrypt(this byte[] plainTextData, string password)
{
byte[] encryptedData = null;
using (var provider = new RijndaelManaged())
{
@feanz
feanz / GetName
Created September 4, 2013 09:01
Diff methods to get the typed name of a proeprty
void Main()
{
var variable = 1;
100000.Times("Get variable name", () => GetName(new {variable}));
100000.Times("Get variable name", () => GetNameCached(new { variable}));
100000.Times("Get variable name", () => GetNameExpression(() => variable));
100000.Times("Get variable name", () => GetNameIL(() => variable));
}
public static string GetName<T>(T item) where T : class
public class SomeClass {
public void SomeMethod() {
this.Log().Info("Here is a log message with params which can be in Razor Views as well: '{0}'".FormatWith(typeof(SomeClass).Name));
}
}
@feanz
feanz / OperationStatus
Last active December 20, 2015 19:39
Operation Status Object could be returned from persistence layer.
[DebuggerDisplay("Status: {Status}")]
public class OperationStatus
{
public bool Status { get; set; }
public int RecordsAffected { get; set; }
public string Message { get; set; }
public Object OperationId { get; set; }
//Store simple string for exception message to avoid any possible serialization issues
public string ExceptionMessage { get; set; }
@feanz
feanz / Benchmark Extensions.cs
Created March 25, 2012 11:40
A set of extension methods for doing benchmarks
static class BenchmarkExtension {
public static void Times(this int times, string description, Action action) {
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < times; i++) {
action();
}
watch.Stop();
Console.WriteLine("{0} ... Total time: {1}ms ({2} iterations)",
@feanz
feanz / gitignore
Created February 14, 2012 18:03
Git ignore file
#OS junk files
[Tt]humbs.db
*.DS_Store
#Visual Studio files
*.[Oo]bj
*.user
*.aps
*.pch
*.vspscc
@feanz
feanz / MenuHtmlhelper.cs
Created January 25, 2012 13:25
A html helper that a nav Menu based on the contents of default Sitemap. Menu supports permission based filtering
/// <summary>
/// Output Menu UL based on the contents of default Sitemap. Menu supports permission based filtering
/// </summary>
/// <param name="helper"></param>
/// <param name="user"></param>
/// <param name="menuItem"></param>
/// <returns></returns>
public static IHtmlString Menu(this HtmlHelper helper, IPrincipal user, string menuItem = "")
{
var sb = new StringBuilder();