How C# Extension Methods Work


1. Introduction.

1.1 Extension methods were introduced in C# 3.0.

1.2 The main purpose of extension methods is to provide for seamless integration of new functionalities to existing classes.

1.3 I have personally used extension methods to great effect in my projects and I intend to share some of my extension methods with readers in the future.

1.4 In this blog, I wish to explore how extension methods work under the hood.

1.5 We will explore low-level IL code generated for extension methods as well as their client code.

1.6 For a little bit of fun, I will also show how, with a little bit of IL code tweaking, we can turn a normal static method into an extension method.

2. A Sample Extension Method.

2.1 First let’s create a small C# class library with the following namespace and class :

using System; using System.Collections.Generic; using System.Linq; using System.Text…

View original post 2,962 more words


GSoC-2016 Final Report



Student: Shekhar Reddy

Mentors: Jan Flowers, Pascal Brandt

Project Wiki : Atlas 3.0

OpenMRS Atlas

The OpenMRS Atlas is a website on which members of the OpenMRS Community can register their site(s).  The Atlas serves not only to let the world know about your site but also help sites to discover other implementations in their region.

About Atlas 3.0

OpenMRS Atlas Server (openmrs-contrib-atlas) was designed to be a RESTful backend and a JavaScript client frontend – i.e., the server is concerned only with providing a RESTful API and Authentication and the JavaScript frontend uses Google Map API to do all of the UI.

The technologies used here are outdated which makes the code more complex, So the community decided to rewrite the Atlas server which provides truly RESTful API using Node.js. Atlas 3.0 is the server side code for OpenMRS-Atlas which presents simple RESTful API written in NodeJS.


  • RESTful Backend.

Now the server side code is RESTful which serves the endpoints for the resources (markersites, distributions).

Link for the corresponding code of all the routes is here.

  • Authenticating the Atlas.

I have explained more about authentication of an app against OpenMRS ID in my blog. I have to roll out my own library which encodes and decodes the multipass token which is sent by OpeMRS ID through the URL

Links : BlogPost 1, Blog Post 2, Code for authentication. atlas-node-multipass Library

  • Changes to the templates in the frontend and BUGS 

Since the Atlas server code is written in PHP Laravel framework which uses Blade template engine in the front-end code to render the GoogleMapAPI so the code was a mix of PHP and HTML. I refactored it replacing blade.php files with the EJS template engine.

Links: Blogpost, Code, PRs

  • Documentation

I have done documentation for the code I have written explaining the structure of code so that in future if anyone who takes up this could easily understand the code.

Link : Wiki


Since this project is functionally same as openmrs-contrib-atlas but the code structure and technologies are different so I created a new repo and committed all the code there.

During Authenticating Atlas, I didn’t install the openmrs ID locally as I felt that would kill my time so I rolled out my own MockID in Node which serves the exact purpose of OpenMRSID.

Project GitHub Repo : Link and commits(all the commits included)

Auth Library : Link

Atlas-mockID-node : Link

PRs for frontend : Link

All the Blog Posts: Link

Talk Thread: Link

What is yet to be done for the release

Here are a few things which I am currently working on after which the project will be ready to RELEASE!!

  • Accept the Pings from the module.
  • RSS feed.
  • Admin Interface.

I am planning to complete the above in a week or two and make it ready for the release.

Useful Resources and Links:

I have updated the project in a great detail with all the relevant links


Throughout the summer, I have learned a lot of new technologies like NodeJS, Express  and this is not possible without my mentors and the active community members. I feel really blessed to have such responsive mentors who are always active on forums.

I express my deep regards to all of you. In the coming days, I will contribute to this project and successfully release it and I am Leading the daily scrum meets on IRC!!



Shekhar Reddy 🙂

Week#: 13 Documentation

Hello, Time is flying and I feel indebted for this organization for providing such a great opportunity and I would like to thank my mentors Jan and Pascal for the guidance and whole community members for the support. I will work for this org post-GSoC and release the Atlas 3.0 soonish.

By the way, I am leading the scrum meets these days you can check them here.

This week I have documented what I have done till now: More on documentation

The Project structure :

public/ This directory contains js, CSS, images and lib files
routes/ Contains all the routes
views/ Contains all the front end code
bin/www.js Contains the script for creating and running the HTTP server
app.js Creates an express app and holds all the middleware e.g auth middleware
package.json Manifest file used to manage project dependencies.

What does each directory contain? :

  • package.json
    This file consists all the details of the dependencies, dev-dependencies, semantic version, etc.. which shows the state of the application.
  • app.js
           This file is the start of the application where an express app holds and uses all the middleware. The middleware that we use is here.
  • bin/www.js 
           Contains all script which creates the HTTP server on a mentioned PORT and starts the server once we start the server.
  • views/
           This directory contains all the template files which are generally the front-end part which uses the JS, CSS, Images and all resources from the public/ directory.
    1. index.ejs
        This project uses .ejs template engine for the views. The index.ejs file will render the google map API and the javascript from the public directory.
    2. error.ejs
        This file is a standard error file which can be rendered whenever an error is thrown so as the user knows what type of the error is that.
  • public/
          This directory consists of all the resources like JS, CSS, Images etc..
    1. CSS/
        contains all the CSS files which are used in the template engine.
    2. js/
        contains all the javascript files which are used in the views.
    3. images/
        contains all the images.
  • routes/
          routes dir contains all the route files each denoting a resource wherein the endpoints are defined. The resources that we have.
    1. distributions.js
        This file consists the defined endpoints for the distributions like one can create,update and delete the distributions.
    2. authentications.js
         This file consists the defined endpoints for the authentication.SSO decoding and logging the user –all that is related to auth resides here.
    3. markersites.js
          This file consists the defined CRUD operations for the markersites.


I have written a library for authentication which can be used by anyone who wants to authenticate their app against OpenMRSID. This library can encode and decode a multipass token so that we could access the user details in the app and log the user.

In Atlas, we decode the token and put the user details in a session and use the session wherever we need the authentication. For detailed view of the code: Link


Atlas 3.0 doesn’t alter any functionalities that were in the older version of Atlas.


External Links

Github Repo

Thanks for reading 🙂


Shekhar 🙂

Week#: 12 Updates

This week was productive. I have completed the authentication and now the user logged in will be able to edit his site and the update the site.

Commits for this week :

Next week I will be documenting it and there are some more functionalities to achieve like

  • Rss feed
  • module comaptibility
  • Admin interface

Then RELEASE!!! 🙂

Week 11: Atlas multipass library

So this week I have written a library which encodes and decodes a multipass token and it is specific to the atlas and can be hacked to our requirements.

encode() method, encode the user details and generate a token using the keys and decode() method, decode the token to a JSON object which can be used to log the user.

var crypto = require('crypto');

// Block size
var BLOCK_SIZE = 16;

// Create an return a Multipass API
var api = module.exports = function(apiKey, siteKey) {
if (!(this instanceof api)) return new api(apiKey, siteKey);
if (!(typeof apiKey == 'string' && apiKey.length > 0)) throw new Error('Invalid API key');
if (!(typeof siteKey == 'string' && siteKey.length > 0)) throw new Error('Invalid site key');
this._key = crypto.createHash('sha1').update(apiKey + siteKey).digest('binary').substring(0, BLOCK_SIZE);
this._iv = new Buffer('OpenSSL for Node', 'binary');
return this;

// Encode a Multipass token
api.prototype.encode = function(obj) {
if (!obj) return;
// Create a buffer
var data = new Buffer(typeof obj == 'string' ? obj : JSON.stringify(obj), 'utf8');
// Encrypt with AES
var cipher = crypto.createCipheriv('aes-128-cbc', this._key, this._iv),
token = cipher.update(data, 'binary', 'binary') +'binary');

//prepend the iv
token = new Buffer(token, 'binary');
token = token.toString('base64');

token = token.replace(/\n/g, '') // Remove newlines
.replace(/\=+$/, '') // Remove trailing =
.replace(/\+/g, '-') // Replace + with -
.replace(/\//g, '_'); // Replace / with _
return token;

// Decode a Multipass token
api.prototype.decode = function(token) {
if (typeof token != 'string') return;
token = token.replace(/_/g, '/') // Replace _ with /
.replace(/\-/g, '+'); // Replace - with +
// Decrypt with AES
var cipher = crypto.createDecipheriv('aes-128-cbc', this._key, this._iv.toString('binary')),
data = cipher.update(token, 'base64', 'binary') +'binary');
// Create a buffer
data = new Buffer(data);
try {
return JSON.parse(data.toString('utf8'));
} catch (e) {};

Time is flying and I have to gear up to meet the expectations:)

NPM Package:

To install the package

npm i atlas-node-multipass


Shekhar 🙂

week#10 Decoding the multipass tokens and Blocker

I know this post is a bit late…

In the previous blog post, I mentioned about the mechanism to authenticate a site against OpenMRS ID. To authenticate a user the atlas and OpenMRS ID uses a pair of keys by using which the token can be easily decoded. –the process is

This mockup explains it better :

A typical SSO strategy:


And for OpenMRS Atlas :



The blocker that I have mentioned in the title refers to writing the library which decodes the token. I thought that with some existing third party library we could achieve the same but that didn’t turn up. So I have to write a package which serves our need.

Next post is on how decoding and encoding do by the library which will be ready soonish.

Thanks for reading 🙂


Shekhar 🙂

Week 9 : Issues with the frontend and models

This week I was my progress is little not up to what I have expected.

I thought I would have completed the part related to authentication.

So.. What I have done this week goes below

The share-url button have come up and working as i was expecting. Researched a bit regarding the models in REST.Coming to authentication part, which I am hoping to complete by this week. Since the front-end code was a bit complex and poor documentation have affected the progress.

Thanks for reading. stay tuned for next week updates.


Shekhar 🙂

Week 8 Updates

Hello everyone,

This week I have worked on following

  • Updated the README with the instructions as how to install the atlas server locally.
  • Written the SQL script files for the DB on a Gist and shared it with instructions.
  • Made to get the lock symbol on the edit info window of the bubble.
  • Scrum meets.


OpenMRS ID is the system of user management used by the OpenMRS Community. User accounts are managed and created using the OpenMRS ID Dashboard, a web app written in Node.js. Data model is built around LDAP user object and stored on an OpenLDAP server. In order to support unified authentication for all OpenMRS sites and external services, ID Dashboard also provides SSO, OAuth EndPoints.

For Atlas, we use the SSO Endpoints provided by the OpenMRS ID.

So.. What is SSO ?

SSO– Single Sign On authentication of a user with which the user enter his credentials only once to access a group related systems.

How we use it in Atlas ?

We use the multipass strategy of SSO to get the user authenticated to Atlas. For more details into how multipass actually works.. head to this LINK .

In Atlas, we need not encode the user details into AES encrypted JSON –that is done by the OpenMRS ID but we need to decode the encrypted JSON and use the details as we need it.

I use this Library for Decoding .


Shekhar 🙂

Week#7 Legend for OpenMRS Atlas

Hello everyone,

Updates for this week regarding the project.

  • Since, the previous I am not entirely done with the frontend part, this week -I have invested in completing it and I am done with the same.
  • Got the legend for the atlas up and now I can easily switch between the types, versions and distro options from the controller on the atlas.
  • The markers are up from the previous week.
  • Gained some knowledge regarding the SSO authentication and multi-pass.
  • Commits for this week : Link .

Coming weeks

  • Will be working on authentication part, using the multi-pass authentication(SSO) against the openmrsID.
  • Get the RSS feed consistent.
  • Atlas cronjob (screen-captures) using the PhantomJS.
  • Make sure the REST URI are consistent with the module and updates from the module.


Shekhar 🙂

Week #6 Playing with JQuery

Hello everyone,

This week, I started with getting the map show up when the app runs with the rest URI’s I’ve written in node, but surprisingly that didn’t happen as expected so I have to run through the code related to the frontend part and figured out a few points as how the frontend was designed.

  • All the code in the front end is just simple JavaScript using AJAX requests with Jquery using a gmap3 plugin for google maps API.
  • So up till now I am able to show the map on the browser and markers showing up on them
  • The view files which are  a part of backend use the blade template engine in which we have the PHP code partially inserted, and the worst part was to remove all the PHP code in view file and replace them with corresponding JavaScript.
  • The view files that I use right now are .ejs which are relatively very easy to understand and write

Objectives yet to achieve and plan coming weeks

  • Probably it may take a few days to come up with the markers and Legend/Distributions show up on the map.
  • Start the authentication part with the help of the community.