Skip to content

Instantly share code, notes, and snippets.

@ChrisMcKee
ChrisMcKee / BootstrapSupport.cs
Created January 4, 2013 12:09
usage i.e. String Editor Template... usage ala first comment
using System;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
public static class ControlGroupExtensions
{
public static IHtmlString BeginControlGroupFor<T>(this HtmlHelper<T> html,
Expression<Func<T, object>> modelProperty)
{
@ChrisMcKee
ChrisMcKee / GenericDataAnnotations.cs
Created January 4, 2013 12:07
UK Date format / Must be true (for T&C's) / Range "if"
namespace Helpers.Validators
{
using System.ComponentModel.DataAnnotations;
public class DateFormatAttribute : RegularExpressionAttribute
{
public DateFormatAttribute() : base(@"^((0[1-9])|([1-2][0-9])|3[0-1])\/((0[1-9])|(1[0-2]))\/[0-9]{4}$")
{
}
using System.Web.Mvc;
using Elmah;
public class ElmahHandledErrorLoggerFilter : IExceptionFilter
{
public void OnException(ExceptionContext context)
{
// Log only handled exceptions, because all other will be caught by ELMAH anyway.
@ChrisMcKee
ChrisMcKee / PostCode.cs
Created January 4, 2013 12:01
UK Post Code Validation (Used in website validation) ALA var formattedPostCode = new Postcode(); Postcode.TryParse(viewModel.Postcode, out formattedPostCode); Try Parse will return bool; formatted postcode is outputted.
using System;
using System.Text.RegularExpressions;
[Flags]
public enum PostcodeParseOptions
{
None = 0,
IncodeOptional = 1,
MatchBfpo = 2,
MatchOverseasTerritories = 4,
public MvcApplication()
{
BeginRequest += delegate { CurrentSessionContext.Bind(SessionFactory.OpenSession()); };
EndRequest += delegate
{
if (SessionFactory != null && CurrentSessionContext.HasBind(SessionFactory))
CurrentSessionContext.Unbind(SessionFactory).Dispose();
};
}
using System;
using System.Web.Mvc;
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class TransactionAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var session = MvcApplication.SessionFactory.GetCurrentSession();
session.BeginTransaction();
namespace Data
{
using System;
using System.Configuration;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Conventions.Helpers;
@ChrisMcKee
ChrisMcKee / IRepositoryFactory.cs
Created January 4, 2013 11:47
Repository Factory Interface / Implementation (using ninject/nhibernate)
namespace Core.Repository
{
using Core.Model;
using Core.Model.Some;
public interface IRepositoryFactory
{
ISomeRepository SomeRepository();
IRepository<SomeObjectType> SomeObjectTypeRepository();
IRepository<T> Repository<T>() where T : class;
@ChrisMcKee
ChrisMcKee / Repository.cs
Created January 4, 2013 11:41
Generic Repository Interface and implementation. NHIBERNATE
namespace Core.Repository
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
public interface IRepository<T> where T : class
{
bool Add(T entity);
@ChrisMcKee
ChrisMcKee / Specification.cs
Created January 4, 2013 11:38
Specification Pattern Template
namespace Specifications
{
public interface ISpecification<T>
{
bool IsSatisfiedBy(T candidate);
ISpecification<T> And(ISpecification<T> other);
ISpecification<T> Or(ISpecification<T> other);
ISpecification<T> Not();
}