Search

Custom Workflow Activity to calculate and return

The following sample workflow activity demonstrates how to return a calculated value from an activity.

Singleton in javascript

The singleton pattern is what you use when you want to ensure that only one instance of an object is ever created. In classical object-oriented programming languages, the concepts behind creating a singleton was a bit tricky to wrap your mind around because it involved a class that has both static and non-static properties and methods. I’m talking about JavaScript here though, so with JavaScript being a dynamic language without true classes, the JavaScript version of a singleton is excessively simple.

Why do you need the singleton?

Before I get into implementation details, I should discuss why the singleton pattern is useful for your applications. The ability to ensure you have only one instance of an object can actually come in quite handy. In server-side languages, you might use a singleton for handling a connection to a database because it's just a waste of resources to create more than one database connection for each request. Similarly, in front-end JavaScript, you might want to have an object that handles all AJAX requests be a singleton. A simple rule could be: if it has the exact same functionality every time you create a new instance, then make it a singleton.
This isn't the only reason to make a singleton, though. At least in JavaScript, the singleton allows you to namespace objects and functions to keep them organized and keep them from cluttering the global namespace, which as you probably know is a horrible idea, especially if you use third party code. Using the singleton for name-spacing is also referred to as the module design pattern.

Show me the singleton

To create a singleton, all you really need to do is create an object literal.

var Singleton = {
    prop: 1,
    another_prop: 'value',
    method: function() {…},
    another_method: function() {…}
};


You can also create singletons that have private properties and methods, but it's a little bit trickier as it involves using a closure and a self-invoking anonymous function. Inside a function, some local functions and/or variables are declared. You then create and return an object literal, which has some methods that refererence the variables and functions that you declared within the larger function's scope. That outer function is immediately executed by placing () immediately after the function declaration and the resulting object literal is assigned to a variable. If this is confusing, then take a look over the following code and then I'll explain it some more afterward.



The key is that when a variable is declared with var in front of it inside a function, that variable is only accessible inside the function and by functions that were declared within that function (the functions in the object literal for example). The return statement gives us back the object literal, which gets assigned to Singleton after the outer function executes itself.

Namespacing with the singleton

In JavaScript, namespacing is done by adding objects as properties of another object, so that it is one or more layers deep. This is useful for organizing code into logical sections. While the YUI JavaScript library does this to a degree that I feel is nearly excessive with numerous levels of namespacing, in general it is considered best practice to limit nesting namespaces to only a few lavels or less. The code below is an example of namespacing.


The use of namespacing, as I said earlier, keeps global variables to a minumum. Heck, you can even have entire applications attached to a single object namespace named app if that's your perogative.

Generating unique ID with c#

In order to generate the IDs used in Youtube the following serves as an potential example. It uses the datetime class to generate ID which should be unique if the code runs on a single system.

Parsing XML using Javascript

Example to parse Xml using JScript & XMLDOM

Generate Xml from Xslt file

In order to generate a xml from nowhere this method is a helped utility.

Xml Helper file

I wrote a helper utility to work with XMl files in C#, these are some common set of functions that i have been using for day-to-day work, I compiled it inside a common entity now ;)

Get UnCached version form Ajax file request

While loading external javascript there is problem of caching the general solutionfor this is appending a url parameter like "?rnd=Date.Now()" at the end of the url, so that the url is always unique while making the call. The other way around is to set the IIS ouput caching with FIle change notifications. But the same could be achieve without much effort by setting the response header. We can set the response header to get the modified version from the server instead of the cached one using the following script:

How to define a class in JScript ?

This a common example for JScript(ECMA262) to create a class with defined constructor. The constructor can be parameteries to accent a/multiple parameter.

Wiindows Search with batch script

Till last year i was using Windows XP which was quite faster then windows 7 on searching files in the local system. But again windows 7 started giving me hard-time with it’s indexing services. So i started using the old batch script i wrote in the late 90’s. Its’s called windows search.


All you have to do, is to copy and save this file as "search.bat" in the user folder(c:\users\).

calling it is even easier cause it supports wildcard(*) characters.

Example:
search *.txt

will search the whole directory for the files with text extension, the query can be modified if you know a part of the file name as
search read*.txt

After pressing enter.. it takes a while and pops up a message window(with instructions), clicking ok in the message window will open notepad with a file search.txt with all the results.

Recycling IIS Application Pools

Recycling IIS app pool are always considered a better option then IISReset to refresh & purge resources. As it Before recycling itself , it invokes a new worker process first and then shutdown the old worker process which makes the IIS still available to take request. Here is the appcmd to recycle appool

%windir%\system32\inetsrv\appcmd recycle apppool /apppool.name:<app pool name>

for Example:

To reset the CRMAppPool( Crm developers know why ;)

%windir%\system32\inetsrv\appcmd recycle apppool /apppool.name:CRMAppPool

will recycle the apppool named "CRMAppPool”.