Skip to content

Instantly share code, notes, and snippets.

@wasimf
wasimf / WebAPiCamelCasinJson
Created April 2, 2013 10:42
ASP.NET WebAPI Tip: camelCasing JSON
Let's say you have the following type definition in C#:
public class Thing
{
public int Id { get; set; }
public string FirstName { get; set; }
public string ISBN { get; set; }
public DateTime ReleaseDate { get; set; }
}
@wasimf
wasimf / InstanceMembers.js
Created April 5, 2013 17:52
Instance members are accessed from instance objects which are created with the “new” operator. Instance members are created via “this” keyword, prototype, constructor closure or Object.defineProperty
function Cat(name) {
var voice = "Meow";
this.name = name;
this.say = function () {
return voice;
}
}
Cat.prototype.eat = function () {
return "Eating";
@wasimf
wasimf / PrivateStaticMembers.js
Created April 5, 2013 18:55
There is no direct support for static members. Constructor functions are used to create static members. Static members can’t be accessed with “this” keyword.
var Book = (function () {
// private static field
var numOfBooks = 0;
// private static method
function checkIsbn(isbn) {
if (isbn.length != 10 && isbn.length != 13) throw new Error("isbn is not valid!");
}
function Book(isbn, title) {
checkIsbn(isbn);
@wasimf
wasimf / SingletonObject.js
Created April 5, 2013 19:01
JavaScript singleton object
var Logger = {
enabled: true,
log: function (logText) {
if (!this.enabled) return;
if (console && console.log) console.log(logText);
else alert(logText);
}
}
Or
@wasimf
wasimf / ObjectCreation.js
Created April 5, 2013 19:07
With "new" Operator Instances of built-in or user-defined object types can be created with the “new” operator. The “new” operator runs only with constructor functions. An already created object can’t be used with “new” again. First, the “new” operator creates an empty object; then it calls the constructor function while assigning the newly creat…
//or var dog = {};
//or var dog = new MyDogType();
//or var newDog = Object.create( null );
var dog = new object();
dog.name = "Scooby";
dog.owner = {};
dog.owner.name = "Mike";
dog.bark = function () {
return "Woof";
};
@wasimf
wasimf / BasicConstructor
Created May 29, 2013 12:36
The Constructor Pattern
function Car( model, year, miles ) {
this.model = model;
this.year = year;
this.miles = miles;
// toString is redefined for each of the new objects created using the Car constructor
// its not so optimal.
this.toString = function () {
return this.model + " has done " + this.miles + " miles";
@wasimf
wasimf / ModulePattern
Created June 2, 2013 06:39
Javascript Module Pattern
var myNamespace = (function () {
var myPrivateVar, myPrivateMethod;
// A private counter variable
myPrivateVar = 0;
// A private function which logs any arguments
myPrivateMethod = function( foo ) {
console.log( foo );
@wasimf
wasimf / RevealingModulePattern
Created June 2, 2013 06:41
The Revealing Module Pattern : Define all functions and variables in the private scope and return an anonymous object with pointers to the private functionality we wished to reveal as public.
var myRevealingModule = function () {
var privateVar = "Ben Cherry",
publicVar = "Hey there!";
function privateFunction() {
console.log( "Name:" + privateVar );
}
function publicSetName( strName ) {
@wasimf
wasimf / Dispose
Created June 20, 2013 05:16
Implement IDisposable correctly
public class Resource : IDisposable
{
private IntPtr nativeResource = Marshal.AllocHGlobal(100);
private AnotherResource managedResource = new AnotherResource();
// Dispose() calls Dispose(true)
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
@wasimf
wasimf / AspNetEventBroker
Created July 4, 2013 06:17
ASP.NET Event Broker - Event Broker implementation provides a way to broadcast and receive loosely coupled events. Behind the scenes the Event Broker uses the HttpContext.Items to keep track of its information.
using System;
using System.Web;
using System.ComponentModel;
public static class EventBroker
{
private static object EventsKey = new object();
public static void Subscribe<TType>(this HttpContextBase source, object key, EventHandler<TType> value)
where TType : EventArgs