Skip to content

Instantly share code, notes, and snippets.

View FernandoVezzali's full-sized avatar

Fernando Ayrosa Vezzali FernandoVezzali

  • 22:21 (UTC -03:00)
View GitHub Profile
@FernandoVezzali
FernandoVezzali / TransactionScope
Created August 12, 2013 10:33
TransactionScope Entity Framework 5
using(var scope = new TransactionScope(TransactionScopeOption.Required,
new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted }))
{
// Do something
context.SaveChanges();
// Do something else
context.SaveChanges();
scope.Complete();
}
@FernandoVezzali
FernandoVezzali / replaceAll
Created August 14, 2013 14:00
replaceAll function for JavaScript
String.prototype.replaceAll = function( token, newToken, ignoreCase ) {
var _token;
var str = this + "";
var i = -1;
if ( typeof token === "string" ) {
if ( ignoreCase ) {
_token = token.toLowerCase();
@FernandoVezzali
FernandoVezzali / SetupControllerForTests.cs
Created September 16, 2013 15:41
Unit Testing ASP.NET Web API
//http://www.peterprovost.org/blog/2012/06/16/unit-testing-asp-dot-net-web-api/
private static void SetupControllerForTests(ApiController controller)
{
var config = new HttpConfiguration();
var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/products");
var route = config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}");
var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "products" } });
controller.ControllerContext = new HttpControllerContext(config, routeData, request);
@FernandoVezzali
FernandoVezzali / DelegateOrLambda.cs
Created September 20, 2013 12:41
Delegate or Lambda
string[] names = new string[] { "Bill", "Jane", "Bob", "Frank" };
var Bs_1 = names.Where(delegate(string s) { return s.StartsWith("B"); }); // Delegate
var Bs_2 = names.Where(s => s.StartsWith("B")); // Lambda
@FernandoVezzali
FernandoVezzali / WaitingParallelTasks.cs
Created October 3, 2013 09:54
Waiting for Parallel Tasks to Complete
var tasks = new Task[10];
for (var i = 0; i < 10; i++)
{
var x = i;
tasks[i] = Task.Factory.StartNew(() => new WorkerClass(x).Do());
}
Task.WaitAll( tasks );
@FernandoVezzali
FernandoVezzali / GetJsonFromURL.cs
Created October 15, 2013 10:19
GetJsonFromURL
private static string GetJsonFromURL(string url)
{
var request = WebRequest.Create(url);
var response = request.GetResponse();
var dataStream = response.GetResponseStream();
var reader = new StreamReader(dataStream);
string json = HttpUtility.HtmlDecode(reader.ReadToEnd());
reader.Close();
@FernandoVezzali
FernandoVezzali / CustomRoute.cs
Last active December 26, 2015 09:59
Custom Route MVC
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "CustomRoute",
url: "{controller}/{action}/{startYear}/{startMonth}/{startDay}/{endYear}/{endMonth}/{endDay}",
defaults: new
{
controller = "Home",
@FernandoVezzali
FernandoVezzali / MvcScaffolding_VS2013
Last active December 29, 2015 04:09
MVCScaffolding for MVC 5 (Visual Studio 2013)
Install-Package EntityFramework.SqlServerCompact -version 6.0.1
Install-Package MvcScaffolding -Version 1.0.8-vs2013 -Pre
public class Category {
public int CategoryId { get; set; }
public string Name { get; set; }
}
public class Product
{
@FernandoVezzali
FernandoVezzali / RequestBinExample.cs
Created December 18, 2013 06:11
RequestBinExample
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace RequestBinExample
{
class Program
{
static void Main(string[] args)
{
// first install this NuGet package: Install-Package ftplib
static void Main(string[] args)
{
using (var tConn = new FtpConnection("00.000.00.000", "UserName", "Password"))
{
tConn.Open();
tConn.Login();
if (tConn.DirectoryExists("/www"))