Skip to content

Instantly share code, notes, and snippets.

public static class Build
{
public static OrderBuilder Order(Cutomer customer)
{
return new OrderBuilder(customer);
}
public static CustomerBuilder Customer(string name)
{
return new CustomerBuilder(name);
@jmhdez
jmhdez / gist:6669478
Last active December 23, 2015 17:29
Enumerators y Dispose
Una consulta básica usando Linq-to-NHibernate (con EF supongo que sería parecido)
var dbQuery = session.Query<Product>();
El IEnumerable que devuelve, internamente tiene un Enumerator parecido a esto:
IEnumerator<Product> GetEnumerator() {
using (var reader = command.ExecuteReader())
{
while (reader.Read())
@jmhdez
jmhdez / AsDict.cs
Last active December 22, 2015 00:28
Extension Method para convertir un objeto anónimo en un IDictinary
public static class Extensions
{
public IDictionary<string, object> AsDict(this object obj)
{
return obj.GetType().GetProperties().ToDictionary(x => x.Name, x => x.GetValue(obj, null))
}
}
var obj = new { Name = "Lucas", Age = 14 }.AsDict();
@jmhdez
jmhdez / Elements.cs
Last active December 20, 2015 11:19
Compartiendo elementos en posiciones dependientes del contenedor
public interface IElement
{
IEnumerable<ChildElement> Children { get; }
}
public class ChildElement
{
public readonly IElement Value;
public readonly Point Point;
@jmhdez
jmhdez / Sample.cs
Last active December 19, 2015 19:29
Unas cuantas opciones para refactorizar un método a la hora de testearlo
// Situación inicial
public class OrderStatsCalculator
{
// Inyectado por constructor. En todos los casos lo hago igual.
private IOrderRepository repository;
// Esto se puede testear con un mock/stub/fake/etc. que inyectes por
// el constructor
public int Calculate()
{
@jmhdez
jmhdez / DuckType.cs
Created December 16, 2012 16:32
Duck typing con C# preparado para realizar implementaciones de interfaces con objetos anónimos
// Required libs
// - NUnit
// - Castle.DynamicProxy
using System;
using System.Linq;
using System.Reflection;
using Castle.DynamicProxy;
using NUnit.Framework;
"scripts": {
"browserify": "browserify lib/index.js -o dist/app.js -t [babelify --presets [es2015 react]]",
"dev": "nodemon --watch lib --exec npm run browserify"
},
@jmhdez
jmhdez / GoToPlugin.java
Created June 22, 2012 07:03
Sample Cordova Plugin for Android
package koalite.cordova;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.apache.cordova.api.PluginResult.Status;
import org.json.JSONArray;
import android.content.Intent;
import android.net.Uri;
public class GoToPlugin extends Plugin {
@jmhdez
jmhdez / analytics.cs
Created March 19, 2012 09:38 — forked from gregoryyoung/analytics.cs
Usage Tracking with Google Analytics
public class TrackingService
{
// Sample usage:
// service.TrackEvent("customers/add");
// service.TrackEvent("order/discount-applied");
public void TrackEvent(string path)
{
ThreadPool.QueueUserWorkItem(x => TrackPageView("/events/" + path));
}
@jmhdez
jmhdez / MachineLearning.cs
Last active August 29, 2015 14:17
Ejemplo de uso de numl para generar árboles de decisión mediante aprendizaje automático sobre un corpus de Princesas Disney
// Require instalar el paquete numl desde NuGet
using System;
using numl;
using numl.Model;
using numl.Supervised.DecisionTree;
namespace MachineLearning
{
public enum HairColor