Skip to content

Instantly share code, notes, and snippets.

View hudo's full-sized avatar

Hrvoje Hudo hudo

View GitHub Profile
@hudo
hudo / mediatrs.cs
Last active February 6, 2017 16:32
MediatR and StructureMap
using System;
using System.Reflection;
using System.Threading.Tasks;
using MediatR;
using StructureMap;
using StructureMap.Graph;
using StructureMap.Graph.Scanning;
using Xunit;
namespace MediatrSmTest
@hudo
hudo / PatternMatch.cs
Last active March 2, 2016 16:30
Pattern Matching in c#
public static class Match
{
public static PatternMatch<T, R> With<T, R>(T value)
{
return new PatternMatch<T, R>(value);
}
public struct PatternMatch<T, R>
{
private readonly T _value;
@hudo
hudo / OrderController.cs
Created October 14, 2015 21:52
Logger in controller
public class OrderController : Controller
{
// constructor and other dependent objects
[HttpPost, ValidateAntiForgeryToken]
public ActionResult Pay(PayRequest request)
{
var order = _repository.Get(request.OrderId);
_logger.InfoOperations("Trying to pay the order ", payRequest)
@hudo
hudo / LoggerExtension.cs
Last active October 14, 2015 22:08
Logger Extension
static class LoggerExtension
{
public static void InfoDiagnostics(this ILogger logger, string message, params object[] data)
{
logger.Log(LogLevel.Info, LogType.Diagnostics, message, data);
}
public static void InfoOperations(this ILogger logger, string message, params object[] data)
{
logger.Log(LogLevel.Info, LogType.Operations, message, data);
}
@hudo
hudo / Ilogger.cs
Created October 14, 2015 21:39
ILogger interface
interface ILogger
{
void Log(LogLevel level, LogType type, string message)
}
let matches (pattern : string) (x : string) : bool =
let rec matchesL pattern x =
match pattern, x with
| [], [] -> true
| '?'::pTail, xHead::xTail -> matchesL pTail xTail || matchesL pTail x
| '*'::pTail, xHead::xTail -> matchesL pTail xTail || matchesL pTail x || matchesL pattern xTail
| pHead::pTail, xHead::xTail when pHead = xHead -> matchesL pTail xTail
| _ -> false
class Program
{
static void Main(string[] args)
{
var server = WebApp.Start("http://+:56789", builder =>
{
var configuration = new HttpConfiguration();
configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
configuration.Routes.MapHttpRoute("default", "api/{controller}");
configuration.Formatters.JsonFormatter.SerializerSettings.TypeNameHandling = TypeNameHandling.Auto;
@hudo
hudo / Program.cs
Last active August 29, 2015 14:12
EF one to one with foreign keys
class Program
{
static void Main(string[] args)
{
Database.SetInitializer(new DropCreateDatabaseAlways<PeopleContext>());
var context = new PeopleContext();
context.Database.Log = Console.WriteLine; // so we can inspect SQL statements
AddOnePerson(context);
@hudo
hudo / OwinKatanaRaven.cs
Last active August 29, 2015 14:05
OWIN Katana Raven URL Shortener example
// https://github.com/hudo/Sample-UrlShortener
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Owin;
using Raven.Client;
using Raven.Client.Embedded;
@hudo
hudo / DataContextExtensions.cs
Created April 22, 2014 22:11
Extension methods to map private properties (string and collections)
public static class DataContextExtensions
{
/// <summary>
/// Map private collection property
/// </summary>
/// <typeparam name="T">Entity type</typeparam>
/// <typeparam name="R">Collection entity</typeparam>
/// <param name="propertyName">property name</param>
public static ManyNavigationPropertyConfiguration<T, R> HasMany<T, R>(this EntityTypeConfiguration<T> mapper, string propertyName)
where T : class