Tuesday, August 25, 2009

Architecture of .Net

Software development industry today is full of incompatibilities. Modules written in different languages do not integrate easily with each other. Applications made on different operating systems use different API’s which makes the compatibility issues even more difficult. In addition, as the software industry focus shifts from stand-alone applications (Desktop applications) to client/server applications there are a new set of compatibility issues appears. Instead of compiled languages, we are using scripting languages for web applications. Instead of Rich User Interface we have HTML and instead of Object oriented concepts we have number of unrelated technologies like COM, XML, DHTML for internet application development.

The Microsoft .NET Framework

.Net comprises of two key components: the Common Language Runtime (CLR or simply “runtime”) and the .Net Framework Class Libraries (FCL). The CLR abstracts operating system services and provides an execution engine or environment to run applications. Application that runs completely under CLR is called as managed code. It is also possible to use COM or windows API objects as well in your .Net application. However, these codes will not be processed by CLR. Such codes are called as unmanaged code.


The Common Language Runtime (CLR)

If the .Net Framework were a leaving human being then CLR would be its heart and soul. Every byte of code that we write either runs in CLR or is given permission by CLR to run.

CLR extracts services of the operating system and provides an environment for execution of managed code. Every code before execution needs to be complied. As any code in JAVA after compilation is converted to byte codes, .Net framework complaint languages are converted to pseudo-machine language called Common Intermediate Language (CIL Or Microsoft Intermediate Language, MSIL or simply IL). Compilation occurs in two steps in .Net

1. Compilation of source code to IL.
2. Compilation of IL to platform specific code by CLR.

Apart from code compilation CLR does other important functions as well.

1. Memory Allocation
2. Thread Management
3. Garbage Collection
4. Maintains Type Safety (CTS)

CIL code is Just In Time compiled (JIT) in to machine code, It means that a piece of code will not be compiled to machine language till it is being called. Most of the cases JIT code compiled only once and thereafter cached in to Memory and subsequent request to the code will be served from cached memory. Say for example if my CIL code contains a method A(), method A() will not be compiled to machine language till its being called by some application or program. Once compiled it will be cached into Memory and subsequent calls to method A() will be served from the memory.

As the CLR converts the CIL to machine code it enforces code verification to make sure that the code is type safe. One of the biggest advantages of CIL is that CIL codes are strongly Type Safe. This means that all the variables belong to a specific data type. There is no scope of “Variant” data types as in Visual Basic (Variant data can be converted to any type at runtime).

Let us discuss a small example. The following code will compile in VB but will not compile in .Net because of type safety.

Dim Variable1 //Compiler treat this as variant as no data type is defined
Dim intVariable as Integer // Integer Variable
Dim strVariable as String //String Variable

Variable1 = intVariable
Variable1 = strVariable

Variable1 can be converted to a string or integer in Visual Basic, as it is a variant type but to run the same code in .Net we have to define two variables in .Net (one Integer type for intVariable and one string type for strVariable).

Windows isolate different applications by hosting them in different processes. If you are running two applications windows opens two different processes for this. Unfortunately one-process-per-one-application model consume more memory. One of the advantages of code verification technology is that it allows CLR to host multiple applications in a single process by isolating them in different compartments. Each compartment has its own set of boundaries and these compartments are known as “application domains”. CLR does not lunch a new process for each application rather it hosts all application in a single process keeping them in different application domains.

Another benefit of managed code is all memory allocated by the managed code is garbage collected. In other words, you allocate the memory by creating different objects but the CLR will free it for you. The process through which system does this is Garbage Collector.


Microsoft’s initiative in to answer all these issues is Microsoft .Net or simply .Net. .Net is a new way of building and deploying software that uses open standards like XML and HTTP to make the interoperability a reality. An important part of this initiative is “.Net Framework”. .Net Framework provides a platform for developing software and it makes the development process easy and less time consuming.

At this point, we have an idea that Microsoft has come up with something called .Net to provide an environment for fast development and which tackles the interoperability issues. Let us now focus on its key components.

No comments: