Skip to content

Instantly share code, notes, and snippets.

View feanz's full-sized avatar

Richard Forrest feanz

View GitHub Profile
public static class DomainEvents
{
[ThreadStatic] //so that each thread has its own callbacks
private static List<Delegate> actions;
public static IContainer Container { get; set; } //as before
//Registers a callback for the given domain event
public static void Register<T>(Action<T> callback) where T : IDomainEvent
{
@feanz
feanz / NoCacheAttribute.cs
Created June 24, 2014 14:45
No Cache Action filter
public class NoCacheAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
base.OnActionExecuted(actionExecutedContext);
if (actionExecutedContext.Response.IsSuccessStatusCode)
{
var cc = new System.Net.Http.Headers.CacheControlHeaderValue();
cc.NoStore = true;
cc.NoCache = true;
@feanz
feanz / RedirectToRouteResult.cs
Created April 10, 2014 10:36
strongly typed redirect to route result
protected RedirectToRouteResult RedirectToAction<T>(Expression<Action<T>> action, RouteValueDictionary values) where T : Controller
{
var body = action.Body as MethodCallExpression;
if (body == null)
{
throw new ArgumentException("Expression must be a method call.");
}
if (body.Object != action.Parameters[0])
@feanz
feanz / AutoMoqWebApiDataAttribute.cs
Last active November 30, 2017 18:07
AutoMoq Web Api Controllers
public class AutoMoqWebApiDataAttribute : AutoDataAttribute
{
public AutoMoqWebApiDataAttribute()
: base(new Fixture().Customize(new AutoMoqWebApiCustomization()))
{
}
}
public class AutoMoqWebApiCustomization : CompositeCustomization
{
@feanz
feanz / Allowuploadsafefilesattribute.cs
Created April 2, 2014 10:30
MVC Action Filter Allow upload of safe files attribute
using System;
using System.linq;
using System.collections.generic;
using System.IO;
using System.web.mvc;
namespace Securitymodule
{
[Attributeusage (Attributetargets.method, Allowmultiple = false )]
public sealed class Allowuploadsafefilesattribute: Actionfilterattribute
@feanz
feanz / NoneNavigateableAttribute.cs
Created March 17, 2014 11:33
Walk Object Graph
/// <summary>
/// A decoration only property used to determine if a property should be walked by specific graph walking functions
/// </summary>
[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public class NoneNavigateableAttribute : Attribute{}
@feanz
feanz / MethodOverrideHandler.cs
Created February 5, 2014 10:21
Way to use the method override header to route traffic to controller actions.
public class MethodOverrideHandler : DelegatingHandler
{
readonly string[] _methods = { "DELETE", "HEAD", "PUT" };
const string _header = "X-HTTP-Method-Override";
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
// Check for HTTP POST with the X-HTTP-Method-Override header.
if (request.Method == HttpMethod.Post && request.Headers.Contains(_header))
@feanz
feanz / ValidateAntiForgeryTokenAttribute.cs
Created January 31, 2014 09:40
Validate AntiForgery Token Attribute that could be used for web api
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public sealed class ValidateAntiForgeryTokenAttribute : FilterAttribute, IAuthorizationFilter
{
public Task<HttpResponseMessage> ExecuteAuthorizationFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
{
try
{
//could add optional ajax header check here
AntiForgery.Validate();
}
@feanz
feanz / Email.cs
Created January 20, 2014 13:33
Fluent Email
public class Email : IDisposable
{
private SmtpClient _client;
private bool? _useSsl;
private Email()
{
Message = new MailMessage();
_client = new SmtpClient();
}
@feanz
feanz / ApplicationManagement.cs
Created January 7, 2014 16:45
Application Management
public class ApplicationManagement : IApplicationManagement
{
private readonly IEnumerable<IApplicationSetting> _settings;
public ApplicationManagement(IEnumerable<IApplicationSetting> settings)
{
_settings = settings;
try
{