Skip to content

Instantly share code, notes, and snippets.

View hagbarddenstore's full-sized avatar

Kim Johansson hagbarddenstore

View GitHub Profile
@hagbarddenstore
hagbarddenstore / GuidConstraint.cs
Last active August 29, 2015 13:57
An IRouteConstraint for ASP.NET MVC that validates a route data value to be a valid GUID.
namespace Hagbarddenstore.Mvc
{
using System;
using System.Web;
using System.Web.Routing;
/// <summary>
/// Defines the GuidConstraint type.
/// </summary>
public class GuidConstraint : IRouteConstraint
using System.IO;
namespace A
{
public class B
{
private string _configurationFilePath = "config.txt";
private string _recPath = string.Empty;
public string ReadConfig()
@hagbarddenstore
hagbarddenstore / Main.cs
Created March 19, 2014 08:47
Sort dates in .NET.
void Main()
{
var dates = new[]
{
new DateTime(2000, 1, 1),
new DateTime(2005, 1, 1),
new DateTime(2010, 1, 1),
new DateTime(2015, 1, 1)
};
@hagbarddenstore
hagbarddenstore / ProjectStructure.cs
Created February 25, 2014 22:16
A simple project structure which separates business layer from the infrastructure layer and the application layer.
/**
* Solution
* |- Domain
* | `- Customers
* | |- Customer.cs
* | `- ICustomersRepository.cs
* |- Infrastructure
* | `- Customers
* | `- LinqToSqlCustomersRepository.cs
* `- Application
@hagbarddenstore
hagbarddenstore / LabelExtensions.cs
Created February 24, 2014 13:51
Provides ASP.NET MVC label extension methods for HtmlHelper.
public static class LabelExtensions
{
public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object htmlAttributes)
{
return LabelHelper(html, ModelMetadata.FromLambdaExpression(expression, html.ViewData), ExpressionHelper.GetExpressionText(expression), null, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
internal static MvcHtmlString LabelHelper(HtmlHelper html, ModelMetadata metadata, string htmlFieldName, string labelText = null, IDictionary<string, object> htmlAttributes = null)
{
var resolvedLabelText = labelText ?? metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
private IEnumerable<Task> CreateHandlerTasks(IDomainEvent domainEvent)
{
if (domainEvent == null)
{
return new Task[0];
}
Func<IDomainEvent, IEnumerable<Task>> handlerCreator;
var type = domainEvent.GetType();
void Main()
{
var testApplication = new TestApplication();
var eventInfo = testApplication.GetType().GetEvents().First();
EventHandlerPusher.AddEventHandler(testApplication, eventInfo, MyEventHandler);
EventHandlerPusher.AddEventHandler(testApplication, eventInfo, MyEventHandler);
testApplication.FireEvent();
void Main()
{
var testApplication = new TestApplication();
var eventInfo = testApplication.GetType().GetEvents().First();
EventHandlerPusher.AddEventHandler(testApplication, eventInfo, MyEventHandler);
testApplication.FireEvent();
}
void Main()
{
var testApplication = new TestApplication();
testApplication.SomeEvent += EventHandler;
testApplication.FireEvent();
}
void EventHandler(object sender, EventArgs e)
using System;
static class TimeKeeper
{
public static readonly Func<DateTime> DefaultTimeKeeper = () => DateTime.UtcNow;
private static readonly object LockObject = new object();
private static Func<DateTime> CurrentTimeKeeper = DefaultTimeKeeper;