Skip to content

Instantly share code, notes, and snippets.

View jrgcubano's full-sized avatar

Jorge Rodríguez Galán jrgcubano

View GitHub Profile
@jrgcubano
jrgcubano / MongooseAggregate.js
Last active August 29, 2015 14:16
Mongoose aggregate example
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
//Database connection
var uristring = 'mongodb://localhost/test';
var mongoOptions = { };
mongoose.connect(uristring, mongoOptions, function (err, res) {
if (err) {
console.log('Error when connecting to: ' + uristring + '. ' + err);
// SQL TO JSON
SELECT
'{"<Code>k__BackingField":"' + [Code] +
'","<Name>k__BackingField":"' + [Name] +
'","<Description>k__BackingField":"' + [Description] +
'","<Parent>k__BackingField":"' + ISNULL([Parent], '') +
'"}, '
FROM [ISOValues].[dbo].[FuntionalSections]
@jrgcubano
jrgcubano / GuidUtility.cs
Last active April 29, 2017 05:13
Helper methods for working with <see cref="Guid"/>.
/*
Generating a deterministic GUID
Although a new GUID is typically created in order to provide a unique ID, there are occasions when it’s useful for two different systems to generate the same GUID independently. RFC 4122 provides an algorithm for deterministic creation of a GUID based on a namespace ID (itself a GUID) and a name within that namespace. These name- based GUIDs will never collide with GUIDs from other sources (e.g., Guid.NewGuid), and have a very (very) small chance of colliding with other name-based GUIDs. As per section 4.3:
The UUIDs generated at different times from the same name in the same namespace MUST be equal.
The UUIDs generated from two different names in the same namespace should be different (with very high probability).
The UUIDs generated from the same name in two different namespaces should be different with (very high probability).
If two UUIDs that were generated from names are equal, then they were generated from the same name in the same namespace (with very high probabil
@jrgcubano
jrgcubano / GuidSHA1Helper.cs
Created March 3, 2015 23:01
Generate guid from custom string
// Found this snippet to be useful when using unique identifier in a database for federated distribution
public static Guid ToGuid(string src)
{
byte[] stringbytes = Encoding.UTF8.GetBytes(src);
byte[] hashedBytes = new System.Security.Cryptography
.SHA1CryptoServiceProvider()
.ComputeHash(stringbytes);
Array.Resize(ref hashedBytes, 16);
@jrgcubano
jrgcubano / AsyncPump.cs
Created March 3, 2015 23:21
Await, SynchronizationContext WPF
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Threading;
public static class AsyncPump
{
public static void Run(Func<Task> func)
{
if (func == null) throw new ArgumentNullException("func");
@jrgcubano
jrgcubano / WPFWeakEventRxExample.cs
Created March 11, 2015 14:15
WPF Weak events pattern using Rx
using System;
using System.Collections.Generic;
using System.Disposables;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Diagnostics;
@jrgcubano
jrgcubano / JsonFormatHelper.cs
Created March 24, 2015 08:45
FormatJsonString
public static class JsonFormatHelper
{
private const string INDENT_STRING = " ";
public static string FormatJson(string str)
{
//if(ConfigHelper.GetAppSetting<bool>("Console", false))
return FormatStrJson(str);
//return String.Empty;
}
public static string FormatStrJson(string str)
@jrgcubano
jrgcubano / GuidIntUtily.cs
Created April 17, 2015 14:41
Int to Guid and Guid to Int
public static Guid Int2Guid(int value)
{
byte[] bytes = new byte[16];
BitConverter.GetBytes(value).CopyTo(bytes, 0);
return new Guid(bytes);
}
public static int Guid2Int(Guid value)
{
byte[] b = value.ToByteArray();
@jrgcubano
jrgcubano / SqlServicesServiceRepository.cs
Created April 21, 2015 07:12
SqlServicesRespository
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace MichaelKappel.Net.Repositories.Interfaces
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Concorde.AzurePack.Domain.Websites.Preallocation.Settings;
using Concorde.Contracts.Processes;
using Concorde.Infrastructure.Messaging;
using Microsoft.ServiceBus.Messaging;
namespace Allocation