Skip to content

Instantly share code, notes, and snippets.

View feanz's full-sized avatar

Richard Forrest feanz

View GitHub Profile
@feanz
feanz / ValidationException.cs
Created January 23, 2012 10:48
A standard validation exception
using System;
namespace Utilities
{
/// <summary>
/// Classed used to distinguish validation exceptions from standard argument or application exceptions
/// </summary>
public class ValidationException : ArgumentException
{
public ValidationException()
@feanz
feanz / ValidatorFluent.cs
Created January 23, 2012 12:52
A fluent validation system to do domain model validation
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.RegularExpressions;
using Utilities.Extensions;
using Utilities.Patterns;
@feanz
feanz / ValidationExtensions.cs
Created January 23, 2012 13:24
A set of standard extension that help implment data annotations validation
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
namespace Utilities.Validation.ExtensionMethods
{
public static class ValidationExtensions
{
/// <summary>
@feanz
feanz / CustomAuthorizeAttribute.cs
Created January 25, 2012 11:08
A custom authorize attribute
using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace StarterSite.Web.Helpers
{
/// <summary>
/// Override the standard AuthorizeAttribute with a Custom implmentation that uses are custom Identity and
@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();
@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 / 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 / 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; }
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 / 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