Skip to content

Instantly share code, notes, and snippets.

@fabiomaulo
fabiomaulo / SalesController.cs
Created September 29, 2017 01:04
Cleaning a DocumentDB document to send it as in a tube
public class SalesController : Controller
{
private readonly IOrderPersister orderPersister;
private readonly IShippingPersister shippingPersister;
public SalesController(IOrderPersister orderPersister, IShippingPersister shippingPersister)
{
this.orderPersister = orderPersister ?? throw new ArgumentNullException(nameof(orderPersister));
this.shippingPersister = shippingPersister ?? throw new ArgumentNullException(nameof(shippingPersister));
}
@fabiomaulo
fabiomaulo / UndefinedSafeDocument.cs
Created September 28, 2017 20:14
Working with Azure Document DB and need 'undefined' safe object as well a way to set unexisting properties to null
public static class DocumentDbHack
{
// WARNING: This class works in the dark side of the force
private static readonly FieldInfo documentPropertiesHack = typeof(Document)
.GetField("propertyBag", BindingFlags.Instance | BindingFlags.NonPublic);
public static dynamic GetUnderlyingObject(this Document source)
{
return source == null ? null: documentPropertiesHack.GetValue(source);
@fabiomaulo
fabiomaulo / MixinPonele.cs
Created September 17, 2017 03:53
A sort of mixin
[Test]
public void WhenAddGetterThenExcuteLogic()
{
dynamic doc = new ExpandoObject();
doc.Object = "Pizza";
doc.State = "Calda";
dynamic ddoc = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(doc));
ddoc.ReState = JObject.FromObject(new ComputedState(ddoc));
@fabiomaulo
fabiomaulo / ITransformer.cs
Created September 17, 2017 03:45
Trasformer pipeline
public interface ITransformer
{
Task<dynamic> Transform(dynamic source);
Task<dynamic[]> TransformEach(IEnumerable<dynamic> source);
}
public abstract class TransformerPipeline: ITransformer
{
private readonly ITransformer innerTransformer;
@fabiomaulo
fabiomaulo / DynamicTransformer.cs
Last active July 9, 2017 13:16
Transform dynamic object: to reduce, expand, transform dynamic deserialized JSONs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Dynamic;
using System.Linq;
namespace Hunabku.DynTransfomer
{
/// <summary>
/// Definition and behaviour to transform a POCO to another POCO.
@fabiomaulo
fabiomaulo / Base64StringExtensions.cs
Created May 9, 2017 21:14
Extensions to create Base64Url encoded + MD5
public static class Base64StringExtensions
{
public static string ToBase64Url(this byte[] source)
{
var sb = new StringBuilder(Convert.ToBase64String(source, Base64FormattingOptions.None));
var paddingCount = 0;
for (var i = 0; i < sb.Length; i++)
{
switch (sb[i])
{
@fabiomaulo
fabiomaulo / POSTMAN.json
Created May 3, 2017 19:24
Upload documents to Azure Search
{
"variables": [],
"info": {
"name": "Azure Search",
"_postman_id": "16089191-30b3-c37d-4bdd-e7823ec7b8d8",
"description": "",
"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
},
"item": [
{
@fabiomaulo
fabiomaulo / TheTestProject.cdproj
Last active April 24, 2017 22:17
Run NUnit tests AspNetCore targeting 4.6.1
<PropertyGroup>
<TargetFramework>net461</TargetFramework>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup>
<DebugType Condition="'$(TargetFramework)' != '' AND '$(TargetFramework)' != 'netcoreapp11'">Full</DebugType>
</PropertyGroup>
@fabiomaulo
fabiomaulo / ValuesController.cs
Last active April 16, 2017 04:18
Revisited famous ValuesController
[Route("api/[controller]")]
public class ValuesController : Controller
{
private static Dictionary<int, string> valuesRepo = new Dictionary<int, string>();
[HttpGet]
public IActionResult Get()
{
return Ok(valuesRepo.Select(x=> new { Id= x.Key, Value= x.Value }));
}
@fabiomaulo
fabiomaulo / Expensive_queries.sql
Created April 6, 2017 12:40
Expensive queries
SELECT TOP 5
st.text,
qp.query_plan,
qs.*
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.plan_handle) st
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp
ORDER BY total_worker_time DESC