Skip to content

Instantly share code, notes, and snippets.

View testfirstcoder's full-sized avatar

Andriy Tolstoy testfirstcoder

View GitHub Profile
@testfirstcoder
testfirstcoder / HttpActionResultExtentions.cs
Last active December 12, 2018 11:52
Add custom header to IHttpActionResult
public static class HttpActionResultExtentions
{
public static IHttpActionResult AddHeader(this IHttpActionResult result, string name, IEnumerable<string> values)
=> new HeaderActionResult(result, name, values);
private class HeaderActionResult : IHttpActionResult
{
private readonly IHttpActionResult actionResult;
private readonly Tuple<string, IEnumerable<string>> header;
@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);
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 / 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 / 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 / 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 / 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 / ModelValidation.cs
Last active March 4, 2016 13:42
Validate model with System.ComponentModel.DataAnnotations
public static class ModelValidation
{
public static IEnumerable<ValidationResult> ValidateModel<T>()
where T : class, new() => ValidateModel(default(T));
public static IEnumerable<ValidationResult> ValidateModel<T>(T model) where T : class, new()
{
model = model ?? new T();
var validationContext = new ValidationContext(model);
@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);