Skip to content

Instantly share code, notes, and snippets.

View testfirstcoder's full-sized avatar

Andriy Tolstoy testfirstcoder

View GitHub Profile
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Security.Principal;
using System.Threading.Tasks;
@testfirstcoder
testfirstcoder / UnityFilterProvider.cs
Created July 13, 2016 07:31
Create filter provider for Unity DI-Container
using System.Collections.Generic;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using Microsoft.Practices.Unity;
using Monads.NET;
namespace Infrastructure
{
public class UnityFilterProvider : IFilterProvider
@testfirstcoder
testfirstcoder / IQueryableMockExtensions.cs
Created June 14, 2016 10:45
Mock IQueryable with NSubstitute
public static class IQueryableMockExtensions
{
public static void SetUp<T>(this T self, IQueryable queryable) where T : IQueryable
{
self.GetEnumerator().Returns(queryable.GetEnumerator());
self.ElementType.Returns(queryable.ElementType);
self.Expression.Returns(queryable.Expression);
@testfirstcoder
testfirstcoder / PropertyConfigurationExtensions.cs
Created March 24, 2016 12:41
Entity Framework Fluent API - Unique key for multiple columns
public static class PropertyConfigurationExtensions
{
public static PrimitivePropertyConfiguration IsUnique(this PrimitivePropertyConfiguration configuration,
string indexName, int indexOrder)
{
var indexAttribute = new IndexAttribute(indexName, indexOrder)
{
IsUnique = true
};
@testfirstcoder
testfirstcoder / Functional.cs
Created March 17, 2016 13:49
Pipelining of functions
public static class Functional
{
public static TResult Do<T, TResult>(this T self, System.Func<T, TResult> func) => func(self);
}
@testfirstcoder
testfirstcoder / ModelValidationAttribute.cs
Last active March 22, 2016 10:54
ActionFilterAttribute for model validation
public class ModelValidationAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
var arguments = actionContext.ActionArguments;
var request = actionContext.Request;
if (HasNullArgument(arguments))
{
@testfirstcoder
testfirstcoder / LowercaseContractResolver.cs
Last active March 17, 2016 09:42
Serialize json attribute names in lower case
public class LowerCasePropertyNamesContractResolver : DefaultContractResolver
{
protected override string ResolvePropertyName(string propertyName) => propertyName.ToLowerInvariant();
}
@testfirstcoder
testfirstcoder / HttpRequestMessageHelpers.cs
Created March 2, 2016 15:16
Request root path (host path + virtual path)
public static class HttpRequestMessageHelpers
{
private const char separator = '/';
public static string GetRootPath(this HttpRequestMessage message)
=> GetHostPath(message) + separator + GetVirtualPath(message);
private static string GetHostPath(HttpRequestMessage message)
=> message.RequestUri.GetLeftPart(UriPartial.Authority).TrimEnd(separator);
@testfirstcoder
testfirstcoder / RegularExpressionAttributePattern.cs
Created February 26, 2016 13:31
Get pattern from DataAnnotations.RegularExpressionAttribute
string GetRegularExpressionAttributePattern<T>(string property)
{
return typeof(T)
.GetProperty(property)
.GetCustomAttribute<RegularExpressionAttribute>()
.Pattern;
}
@testfirstcoder
testfirstcoder / StringLengthAttributeValue.cs
Created February 26, 2016 11:58
Get value from DataAnnotations.StringLengthAttribute
int GetStringLengthAttributeValue<T>(string property)
{
return typeof(T)
.GetProperty(property)
.GetCustomAttribute<StringLengthAttribute>()
.MaximumLength;
}