Tuesday, January 17, 2012

Closures in JavaScript

Closures are not hard to understand once you understand the core concept behind it. You won't understand it better if you read academic papers or anything like that so lets start by looking at some examples. 

Most basic closure example

Here func1 creates local variable name and a function. the function doesn't have a local variable but reuses the variable defined in the parent function. 


In the func2 there's a slight difference. Unlike in func1 here you return the displayName function. so in the displayName function definition gets assigned to the variable myFunc. But it also alerts 'Chrome' which was not a local variable to the displayName function.

You'll see Chrome in the alert is because displayName has become a closure. Closure is a special kind of object which combines

  1. a function
  2. and the environment in which that function was created (environment consists of local variables that were in-scope at that time)
The environment consists of any local variable that were in-scope at the time that closure was created. It will be kept alive even after the function returns. In other words, whenever you see a function keyword within another function, the inner function has access to the variables in outer function. It's a stack-frame which is not de-allocated when the function returns. The local variables are not copied, they are kept by reference.



Closures in practice


Closures can be used to different things. You can emulate private methods using closures (module pattern)


Creating closures in loops


You must be careful when creating a closure inside a loop. Read more about it here at MDN article. Stackoverflow


Performance considerations

Creating closures will have some impact on the script performance from processing speed and memory consumption. When creating new object/class, methods should normally be associated to the object prototype rather than defined into the object constructor. This is because whenever constructor is called, the method will get reassigned for every object creation. (However redefining the prototype is not recommended). 

Closure vs object performance (marijnhaverbeke)

Resolving this with Closures

sources:

Thursday, January 12, 2012

MVC Framework and Application structure

ASP.NET MVC is an open source web application framework. MVC uses the ASP.NET routing engine, which provides flexibility for mapping URLs to controller classes. 

MVC does not use ASP.NET Web forms post-back model for interactions with the server. Instead, all end-user interactions are routed to a controller class. It Maintains separation between UI logic and business logic and helps testability. As a result, ASP.NET view state and ASP.NET web forms page life-cycle events are not integrated 

With MVC based views. Routes are initialized in the Application_Start method of Global.asax file


from MSDN
    Global.asax file
    • Also known as ASP.NET application file. This file is optional
    • Resides in the root directory
    • Derived from HttpApplication class

    HttpApplicationState (implements NameObjectCollectionBase)

    You can use HttpApplicationState to share global information across multiple sessions and requests. 

    A Single instance of an HttpApplicationState class is created first time a client requests any URL resource within a particular ASP.NET application virtual directory. A separate single instance is created for each ASP.NET application on Web server. Reference to each instance is then exposed via the Application object. 

    You can access this via HttpContext.Application property. (In MVC 3 like this). You can use this to store application data that does not change typically. But sometimes it is better not to use this in case you want to access data outside the ASP.NET request pipeline (see this). 

    Application state is not shared across either a Web farm or a Web garden


    Future of ASP.NET 

    In the latter versions of ASP.NET, ASP.NET MVC, ASP.NET Web API and ASP.NET Web Pages will merge into a unified MVC 6. This is also known as ASP.NET vNext. 


    Resources

    Saturday, January 7, 2012

    What you need to know about REST

    If you're in software industry you surely have heard about REST. You might not know what it actually even means but you might know its about designing an API to consume network calls.

    Representational state transfer protocol (REST) is an architectural style for designing networked application using simple* HTML protocols. REST relies on stateless, client-server architecture. You can think of it as a light weight web service. It is platform and language independent protocol.

    Difference between SOAP and REST



    Designing REST API

    APIs exposes functionality of an application or a service.




    * : I used simple with comparison to CORBA, RPC and SOAP which are bit complex to implement nowadays.

    COAP (COnstrained Application Protocol)
    Protocol intended to use in simple electronic devices that allow them to communicate over the internet. It's an application layer protocol. It uses UDP protocol (Check difference between TCP and UDP)
    http://www.slideshare.net/jvermillard/co-ap

    Sources

    Designing REST APIs
    The Increasing importance of APIs in web development 
    Web API growing trends 
    http://stackoverflow.com/questions/7551/best-practices-for-securing-a-rest-api-web-service



    last updated October 2014
    Powered by Blogger.


    Software Architect at Surge Global/ Certified Scrum Master

    Experienced in Product Design, Software Engineering, Team management and Practicing Agile methodologies.

    Search This Blog

    Facebook