Skip to content

Instantly share code, notes, and snippets.

@kenegozi
kenegozi / fix_crlf.bat
Created September 16, 2010 09:54
fixing text files to DOS style line endings (CRLF)
c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe fix_crlf.cs
fix_crlf.exe
@kenegozi
kenegozi / ParallelHttpRequests.cs
Created March 15, 2011 22:46
Demonstrating the use of WebClient.DownloadStringAsync to parallelize http requests
class ParallelHttpRequests
{
static void Main()
{
var count = 300;
var root = "http://files.kenegozi.com/temp/";
ServicePointManager.DefaultConnectionLimit = 1000;
@kenegozi
kenegozi / RandomAd.cs
Created July 5, 2011 21:04
Super dumbed-down ad server - serving a random image every-time.
using Manos;
using System;
using System.IO;
public class RandomAd : ManosApp {
public RandomAd () {
var ads = Directory.GetFiles(".", "*.gif");
var random = new Random();
Route ("/", ctx=> {
@kenegozi
kenegozi / IncludeInterfacesModelMetadataProvider.cs
Created August 22, 2011 21:23
Allowing MVC3 model validator to use interface attributes
class IncludeInterfacesModelMetadataProvider : DataAnnotationsModelMetadataProvider {
protected override IEnumerable<Attribute> FilterAttributes(Type containerType, PropertyDescriptor propertyDescriptor, IEnumerable<Attribute> attributes) {
var validationAttributesOnInterfaces =
from i in containerType.GetInterfaces()
from p in i.GetProperties()
where p.Name == propertyDescriptor.Name
from a in p.GetCustomAttributes(true).Cast<Attribute>()
where typeof(ValidationAttribute).IsAssignableFrom(a.GetType())
select a;
@kenegozi
kenegozi / use_inheritance.cs
Created October 8, 2011 13:15
Pass compositioned data to views
// adding GeneralView class with IsCurrentUserAdmin bool field,
// Derive LayoutView from GeneralView
// Derive HomepageView and PostPageView from LayoutView
public ActionResult ViewPost(string permalink) {
var post = magic.GetPostBy(permalink);
var view= new PostView {Post=post};
GetLayoutData(view);
view.Related = magic.GetContentRelatedTo(post);
Return View(view);
@kenegozi
kenegozi / static-latency-logging.cs
Created July 22, 2012 08:10
Statically logging method call latencies
interface IDoWork {
void DoThis();
string WhatsThat(int that);
}
class DoWork : IDoWork {
public void DoThis() {
Log.Action("DoWork.DoThis", () => DoThisImpl() );
}
@kenegozi
kenegozi / javascript-proxying-example.js
Created July 22, 2012 08:47
javascript-proxying-example
var x = {
doThis : function() { console.log('doThis'); },
whatIsThat : function(i) { console.log('whatIsThis'); return 'that is ' + i; },
name : 'I am x'
};
for (methodName in x) {
var method = x[methodName];
if (typeof method !== 'function') continue;
x[methodName] = (function(method, methodName) {
@kenegozi
kenegozi / untyped-mongodb.cs
Created August 28, 2012 21:02
Accessing mongodb using the official c# driver without types
using System;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Wrappers;
using Newtonsoft.Json;
namespace Mongothingies {
class Program {
static void Main() {
var server = MongoServer.Create();
@kenegozi
kenegozi / MobileServices_WindowsPhone.cs
Created August 29, 2012 00:38
First glance on my unofficial Windows Phone SDK for Azure Mobile Services
// in App.xaml.cs
public partial class App : Application
{
public static readonly MobileServiceClient MobileServiceClient;
public static User CurrentUser;
static App()
{
// Get this data from the management portal's quickstart page
// in the 'connect to existing apps' section
@kenegozi
kenegozi / GistsResolver.cs
Created September 4, 2012 07:03
Given a gist embed script tag, get the gist's content
public class GistsResolver {
public static string CreateContentForFeedFrom(string content) {
try {
return UnwrapGists(content, GetGist);
}
catch {
return content;
}
}