h1

Three tier architecture

November 14, 2008

A three-tier architecture introduces a server (or an “agent”) between
the client and the server.  The role of the agent is manyfold.  It can
provide translation services (as in adapting a legacy application on a
mainframe to a client/server environment), metering services (as in
acting as a transaction monitor to limit the number of simultaneous
requests to a given server), or intellegent agent services (as in
mapping a request to a number of different servers, collating the
results, and returning a single response to the client.

3-tier application is a program which is organized into three major disjunctive tiers on layers. Here we can see that how these layers increase the reusability of codes.

These layers are described below.

1. Application layer or Business layer
2. Business layer
    a. Property layer(Sub layer of business layer)
3. data layer

Advantages of three Tier Architecture.

The main characteristic of a Host Architecture is that the application and databases reside on the same host computer and the user interacts with the host using an unfriendly and dump terminal. This architecture does not support distributed computing (the host applications are not able to connect a database of a strategically allied partner). Some managers found that developing a host application take too long and it is expensive. Consequently led these disadvantages to Client-Server architecture.

Client-Server architecture is 2-Tier architecture because the client does not distinguish between Presentation layer and business layer. The increasing demands on GUI controls caused difficulty to manage the mixture of source code from GUI and Business Logic (Spaghetti Code). Further, Client Server Architecture does not support enough the Change Management. Let suppose that the government increases the Entertainment tax rate from 4% to 8 %, then in the Client-Server case, we have to send an update to each clients and they must update synchronously on a specific time otherwise we may store invalid or wrong information. The Client-Server Architecture is also a burden to network traffic and resources. Let us assume that about five hundred clients are working on a data server then we will have five hundred ODBC connections and several ruffian record sets, which must be transported from the server to the clients (because the Business layer is stayed in the client side). The fact that Client-Server does not have any caching facilities like in ASP.NET, caused additional traffic in the network. Normally, a server has a better hardware than client therefore it is able compute algorithms faster than a client, so this fact is also an additional pro argument for the 3.Tier Architecture. This categorization of the application makes the function more reusable easily and it becomes too easy to find the functions which have been written previously. If programmer wants to make further update in the application then he easily can understand the previous written code and can update easily.

Application layer or Presentation layer

Application layer is the form which provides the user interface to either programmer of end user. Programmer uses this layer for designing purpose and to get or set the data back and forth.

Business layer

This layer is a class which we use to write the function which works as a mediator to transfer the data from Application or presentation layer data layer. In the three tier architecture we never let the data access layer to interact with the presentation layer.

a. Property Layer

This layer is also a class where we declare the variable corresponding to the fields of the database which can be required for the application and make the properties so that we can get or set the data using these properties into the variables. These properties are public so that we can access its values.

Data Access Layer

This layer is also a class which we use to get or set the data to the database back and forth. This layer only interacts with the database. We write the database queries or use stored procedures to access the data from the database or to perform any operation to the database.

Summary

  • Application layer is the form where we design using the controls like textbox, labels, command buttons etc.

  • Business layer is the class where we write the functions which get the data from the application layer and passes through the data access layer.

  • Data layer is also the class which gets the data from the business layer and sends it to the database or gets the data from the database and sends it to the business layer.

  • Property layer is the sub layer of the business layer in which we make the properties to sent or get the values from the application layer. These properties help to sustain the value in a object so that we can get these values till the object destroy.

h1

Unit-1 Introduction to VB.net

September 26, 2008

What is VB.NET

Visual Basic .NET is Microsoft’s Visual Basic on their .NET framework. Visual Basic is an object oriented programming language. Any programmer can develop applications quickly with Visual Basic. It is a very user-friendly language. All you have to do is arrange components using visual tools and then write code for the components. Most programmers of Visual Basic use Visual Studio for their development needs. Moving forward, Microsoft’s .NET framework is composed of preprogrammed code that users can access anytime. This preprogrammed code is referred to as the class library. The programs in the class library can be combined or modified in order to suit the needs of programmers. Programs in .NET run on the CLR or the Common Language Runtime environment. Regardless of computer, as long as this environment is present, programs developed in a .NET language will run.

Evolution of VB.NET

Before VB .NET, there was VB and before there was VB, there was BASIC. BASIC stands for Beginner’s All-purpose Symbolic Instruction Code. It was developed in 1963 by computer scientists John Kemeny and Thomas Kurtz in Dartmouth College. It was a general purpose programming language that was intended for beginners. In 1975, when the MITS Altair 8800 Microcomputer was released, BASIC became Altair BASIC, developed by the computer heavyweights Bill Gates and Paul Allen. During the eighties, when the personal computer was starting to get into everyone’s homes, the BASIC computer language started to lose its hold on the market because more and more people and corporations were using computer programs for complex tasks rather than simple and “basic” tasks. In 1991, BASIC was infused with its Visual component and became Visual Basic. The new graphical user interface was pioneered by Alan Cooper. Visual Basic was not an instant hit at first due to compatibility issues but it began getting a solid following in the mid to late nineties when developers started becoming familiar with it. In the new millennium, the Visual Basic .NET became the successor of the Visual Basic programming languages.

Pros and Cons

Pros

  1. You can develop programs faster using the VB .NET programming language. You have so many resources in the .NET Class Library to take advantage of. VB .NET has full access to the .NET framework.
  2. It is easier to program using Microsoft development environments like Visual Studio.
  3. It is easier to organize your thoughts in an object-oriented programming language like VB .NET.
  4. There are a lot of books and tutorials to improve your skills in VB .NET.
  5. The VB language itself interfaces with Microsoft applications seamlessly (VBA).

Cons

  1. Debugging is very difficult on the VB .NET programming languages especially with long codes.
  2. You’ll spend hundreds of dollars purchasing development tools from Microsoft (Retail version of Visual Studio costs around $300).

Where is VB.NET Most Useful

The VB .NET programming language is most useful for rapid application development or RAD. If you want to get your programs up and running in a jiffy then you should use VB .NET. Since VB .NET is more suitable for quick and easy programming, it is not recommended for the large, enterprise-wide software development solutions. Debugging and finding your way through long and tangling lines of VB .NET code will simply give you headaches.

h1

Unit 1- Introduction to .net technology

September 26, 2008

.NET – a framework

The most important question is What is .NET ?, and the simplest answer is : it is a Framework in which Windows applications may be developed and run. I agree that this answer does not tell much. In order to understand .NET we must go back in time and follow the development of Windows and the advent of Windows programming.

Let’s see how a traditional windows application works.

Windows provide the programmers with various functions – called API. Starting from the very first time Windows enter the commercial market to the latest version of WindowsXP, APIs are the basic tool to let the Windows know what you want it to do. If you want to create a Dialog Box you must call a specific API provided to you by Windows. Making a button requires another API call. And the list goes on. As new GUIs appear on the seen, new APIs are introduced in Windows. But using these native APIs was a very challenging task. Making a simple Window that prints “Hello World” could take more than hundred lines. Compare this to 5 lines of “Hello World” program in DOS. Due to this difficulty, Windows programming was considered something better left to the experts. Microsoft and other commercial organization’s realized this and started marketing Visual Tools that made the programmer’s life easy. Using Visual C++, Visual Basic, Borland’s C++ and other such IDEs, it became simpler to make Windows programs.

Various vendors develop their own “Wrapper classes”  that wrapped the Windows APIs in an Object-Oriented way. The Microsoft Foundation Classes (MFC) that are used in Visual C++, is an example of Wrapper classes. The MFC equivalent of Visual Basic is VBRun; and for Visual J++ it is WFC. These Wrapper Classes, along with the Visual GUI tools made it very convenient to make windows programs.

Microsoft realized that applications need a solid way to talk to each other. And this resulted in the introduction of Object Linking and Embedding (OLE). OLE was an extremely useful concept, but it had two major flaws : It was notoriously difficult to program, and it was very limited in its scope – i.e. it only did a few things like drag and drop, clipboard sharing, OLE client, OLE server etc. Microsoft addressed (or at least tried to address) both these problems. They upgraded OLE to COM. COM  was much more capable than OLE, and it introduced new concept like ActiveX controls which directly competed with Java Applets. As for the difficulty of programming OLE/COM; Microsoft expanded the MFC and VBRun to take care of most of the dirty job. Although making an ActiveX application still was slightly tricky in Visual C++, but developing an ActiveX application in Visual Basic was extremely easy; and hence Visual Basic became the foremost ActiveX development media.

The Internet revolution posed new problems and challenges. C/C++ which was the tool of champions was not suited/ready for Web Development. Microsoft tried expanding MFC, and included several network oriented classes – like CSocket, CASyncSocket, and several HTTP based classes. Using these classes a programmer could very develop a distributed application – although not without considerable effort. But these applications were always customized and targeted to the specific task. The developers had to take care of the gory network communication details themselves. By now object-oriented analysis and development had started becoming ubiquitous. Although technologies like Remote Procedure Call (RPC) was a great help to the programmers; but it was limited in its scope. With programmers following the Object-Oriented development RPC was not much help at all; since RPC did not allow passing objects as parameters. This major issue was addressed by introduction of industry’s agreed upon standards like CORBA, IIOP, RMI, DCOM etc. All these standards used customized protocol to transmit an object over the network, and they required a tight coupling between the server and the client – i.e. the client needed to be fully aware of how to talk to the server. Due to this tight client-server coupling all these protocols needed considerable deployment efforts in order for distributed applications to function properly. Sun did come up with another layer on top of RMI – the famous Enterprise Java Beans (EJB). The EJB container provided lot of services for free – all that a programmer had to do was to extend (inherit) from an appropriate EJB base class and there you have it – a fully functional distributed application. EJB made programmer’s life extremely easy; but it did not eradicate the client-server coupling issue.

While all this was happening – Microsoft was still not facing the writing on the wall – MICROSOFT NEEDED SOMETHING RADICALLY NEW TO  MATCH UP TO THE CHANGING TIMES AND CHANGING DEMANDS. Microsoft soon realized that upgrading their existing technologies would not work – what they needed was a complete change in their philosophy. OLE was upgraded to COM – and it was welcomed by all. COM was then upgraded to COM+. Microsoft addressed the distributed programming issue with the introduction of DCOM. Although COM/COM+/DCOM were all good technologies but these technologies required significant learning curve. Sun on the other hand was making things easier and hence a majority of developers were turning towards Java based technologies for distributed enterprise applications.

Microsoft – after the cold treatment faced by DNA – got their gurus together and asked them to reflect back on DNA and to come up with a future vision. This group came up with so many new and great ideas which made Microsoft realized that no amount of up gradation or extension in MFC/VBRun/WFC, COM/COM+/DCOM, ASP, APIs etc. would even come closer to realizing this new vision. So they made a radical but correct decision – and this was the decision of coming up with something big, something new, and something that lets Microsoft make up for their lapses – this was called the .NET Framework.

The remaining portion of this article discusses a few of the main features and components of .NET framework. After having introduced .NET in brief I’ll conclude this article by answering the million dollar question – “What is .NET ?”

This article assumes that the reader has a solid programming experience. IT also assumes some experience of JAVA. Although experience of RMI, EJB, ActiveX, VC, VB is not required but having an introductory knowledge of these tools would help appreciate .NET better.

Major Components of .NET

The diagram given below describes various components of .NET Framework

Now we explain these components briefly…

The .NET framework can only be exploited by languages that are compliant with .NET. Most of Microsoft languages have been made to fully comply with .NET.

.NET also introduces Web Forms, Web Services and Windows Forms. The reason why they have been shown separately and not as a part of a particular language is that these technologies can be used by any .NET compliant language. For example Windows Forms is used by VC, VB.NET, C# all as a mode of providing GUI.

The next component of .NET is the .NET Framework Base Classes. These are the common class libraries (much like Java packages) that can be used by any .NET compliant language. These classes provide the programmers with a high degree of functionality that they can use in their programs. For example their are classes to handle reading, writing and manipulating XML documents, enhanced ADOs etc.

The bottom most layer is the CLR – the common runtime language. The CLR is discussed  in further post.

What is “Common Language Specification” (CLS)

One of the obvious themes of .NET is unification and interoperability between various programming languages. In order to achieve this; certain rules must be laid and all the languages must follow these rules. In other words we can not have languages running around creating their own extensions and their own fancy new data types. CLS is the collection of the rules and constraints that every language (that seeks to achieve .NET compatibility) must follow. Microsoft have defined three level of CLS compatibility/compliance. The goals and objectives of each compliance level has been set aside. Given below are the three compliance levels with their brief description:

COMPLIANT PRODUCER

The component developed in this type of language can be used by any other language.

CONSUMER

The language in this category can use classes produced in any other language. In simple words this means that the language can instantiate classes developed in other language. This is similar to how COM components can be instantiated by your ASP code.

EXTENDER

Languages in this category can not just use the classes as in CONSUMER category; but can also extend classes using inheritance.

Languages that come with Microsoft Visual Studio namely Visual C++, Visual Basic and C#; all satisfy the above three categories. Vendors can select any of the above categories as the targeted compliance level(s) for their languages.


What is “Common Language Runtime” (CLR)

CLR is .NET equivalent of Java Virtual Machine (JVM). It is the runtime that converts a MSIL code into the host machine language code, which is then executed appropriately. further post gives a detailed description of CLR.


What is “Microsoft Intermediate Language” (MSIL)

A .NET programming language (C#, VB.NET, J# etc.) does not compile into executable code; instead it compiles into an intermediate code called Microsoft Intermediate Language (MSIL). As a programmer one need not worry about the syntax of MSIL – since our source code in automatically converted to MSIL.  . The MSIL code is then send to the CLR (Common Language Runtime) that converts the code to machine language which is then run on the host machine. MSIL is similar to Java Byte code. A Java program is compiled into Java Byte code (the .class file) by a Java compiler, the class file is then sent to JVM which converts it into the host machine language.

What is “Common Type System” (CTS)

All this time we have been talking about language interoperability, and .NET Class Framework. None of this is possible without all the language sharing the same data types. What this means is that an int should mean the same in VB, VC++, C# and all other .NET compliant languages. Same goes for all the other data types. This is achieved through introduction of Common Type System (CTS). CTS, much like Java, defines every data type as a Class. Every .NET compliant language must stick to this definition. Since CTS defines every data type as a class; this means that only Object-Oriented (or Object-Based) languages can achieve .NET compliance. Given below is a list of CTS supported data types:

Data Type Description
System.Byte 1-byte unsigned integer between 0-255
System.Int16 2-bytes signed integer in the following range:
32,678 to 32,767
System.Int32 4-byte signed integer containing a value in the following range:
-2,147,483,648   to   2,147,483,647
System.Int64 8-byte signed integer containing a value from
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
System.Single 4-byte floating point. The value limits are:
for negative values:
-3.402823E38  to – 1.401298E-45
for positive values:
1.401298E-45 TO 30402823E38
System.Double 8-bytes wide floating point. The value limits are:
for negative values:
-1.79769313486231E308 to – 4.964065645841247E-324
for positive values:
4.964065645841247E-324 to 1.79769313486232E308
System.Object 4-bytes address reference to an object
System.Char 2-bytes single Unicode Character.
System.String string of up to 2 billion Unicode characters.
System.Decimal 12-bytes signed integer that can have 28 digits on either side of decimal.
System.Boolean 4-Bytes number that contains true(1) or false (0)


.NET Framework Base Classes

Lets look at the following Visual C++ code extract.:

CView myView;
myView.MessageBox(“Hello World”, “.Net Article”, MB_OK);
::MessageBox(MB_ICONHAND);

The code above first creates an object of CView. CView is a built in MFC class. In second line the code calls CView method MessageBox() to show a dialog box containg an OK button and a “Hello World” message. The caption of the dialog box is “.Net Article”. In the third line the program makes a direct call to a windows API, the scope resolution symbol “::” before a method indicates that it is a direct API call. MessageBeep() uses a system defined wave file “MB_ICONHAND” to play the appropriate sound. The above three lines uses two different types of functions – one that the MFC provides and the other that the Operating System provides. Remember that MFC is nothing but a group of wrapper classes that encapsulate APIs. You can totally bypass MFC and develop an application solely using APIs. How about writing the same code in Visual Basic ?. The MFC equivalent of Visual Basic is VBrun. Although you may be able to  use the MessageBeep() API in VB; but the Class CView does not exist in VBRun. You will have to learn VBRun in order to use an equivalent of CView. The same holds for other programming languages; and to further complicate the situation various vendors have their own names and hierarchy of the wrapper classes. What this means is that MFC is Microsoft wrapper classes for C++; Borland has their own wrapper classes. Same goes for Java. Microsoft provides a powerful wrapper classes package named  WFC to be used with Visual J++, other vendors have their own wrapper classes.

In addition to this, if your application uses a COM component then you code would look radically different in different languages – this is because different languages have different implementation of COM, and have different data types. Given below is an extract of a COM component code (this has been taken out from SMTP.Server application). The code below returns a String. But in the world of COM, there is no string data type; instead the equivalent is “BSTR”. The MFC implementation of string is the class CString (very similar to String class in Java). CString provides a function AllocSysString() that does the necessary conversion to BSTR.

BSTR CServer::GetCcTo()
{
return m_strCcTo.AllocSysString();
}

Now if a COM component was to be developed in VB. The above code would need to go through serious changes. Based on the above discussion we come to the following conclusion :

Every Windows application language has their own implementation and interfaces for the following:

  1. COM Components,

  2. Operating System Specific APIs (e.g. Win32API, Win16 API, Windows CE APIs)

  3. Wrapper Classes (e.g. MFC, VBRun, WFC)

The above mentioned  differences create unnecessary work for a programmer; and hamper interoperability between various languages.

Many Visual C++ programmers are reluctant to learn Visual Basic despite the fact that VB is much easier than VC. Although VC applications are faster, and this may be the reason why programmers may prefer VC, but in case of a simple COM component the increased productivity in VB more than make up for a slight penalty in speed. Personally I prefer to stick to VC, mainly because of the fact that I will have to learn VBRun, VB specific data types, and VB specific COM implementation. It would be great if VB and VC had  common data types, and if MFC was also present in VB. This would reduce my learning curve to almost none, and would encourage thousands of programmers like me to embrace VB.

There is another problem with existing COM implementation. While a COM component can be used in many languages irrespective of how they were developed. These components can not be extended/inherited from. I have always fancied the very idea if being able to inherit/extend from a COM component. Unfortunately it is not (at least up till now) possible unless you have an access to the source code of the component. So the need of the hour is :

  • A Common wrapper class implementation

  • A Common Data Type system

  • Ability to inherit/extend from COM Components.

And the solution to all this is – .NET Class Framework. Those of you who are familiar with MFC/VBRun/WFC can look at this framework as a group of wrapper classes that are shared across VC, VB, and any other .NET compliant language (a language that follows the Common Language Specifications “CLS”  – set forth by Microsoft). So now we all have to learn only one Class Framework and can use it whether we are working in VB or in VC or in any other CLS compliant language.  An important terminology related to .NET Framework is Namespace. Since you would frequently come across this term in any .NET article; its good if we formally define it. Namespace is a logical grouping of related interfaces, structures and classes. Java programmers are familiar with the package concept. The namespace is very similar to the package concept. A Namespace may contain further namespaces. This would result in a tree like hierarchical structure. .Net Class Framework is nothing but a hierarchical structure of namespaces. In .NET Class Framework “System” is the starting namespace. A few other namespaces within System are System.Security, System.Data, System.Console, System.WinForms etc.

If you want to program a .NET application you will have to learn .NET Class Framework; just as a Java programmer learns the basic package hierarchy (e.g. java.util, java.lang, javax.servlet etc.).

Web Services

Web services is an extension of ActiveX. Those of you who have used ASP and JSP both, know the apparent shortcomings of ASP. JSP has been enriched with the concepts of Beans and Tags. ASP equivalent for Beans and Tags was ActiveX Controls and ActiveX automation servers. Let me take a minute to explain this point a bit further. Web Services is NOT a Microsoft proprietary standard. It is a W3Consortium standard, and has been developed by Microsoft, IBM and many other big names of the industry.

Functions are of two types. The ASP built-in functions, and the programmer defined/implemented functions . In order to use the built-in functions you just need to pass the appropriate parameters and make a simple call to these functions. The functions are implemented by the ASP itself. The string manipulation functions, Number conversion functions are an example of built in functions.

The programmer defined functions are the functions that are defined and implemented by the programmer. A programmer can either write these functions in the same asp file or can write them in another file. If the function code resides in the same asp file then the programmer can directly call that function. In case the function resides in another file , say “func.asp”; then the programmer needs to include that file by writing a statement like <!– #include file=”func.asp” –>; and now the programmer can use the function. The programmers can also make ActiveX automation servers, and call various function of these ActiveX servers. But one limitation is very obvious — no matter which type of function you use, the function MUST physically reside on the same machine. For example your ActiveX automation server must be implemented either as a .dll or as an .exe and then must also be registered in Windows Registry before an asp code can call its functions. (you may download SMTP.Server - an ActiveX component developed by the author – to get a better idea of how to use an ActiveX component from you ASP/VC/VB code.) In a world where the Internet has become not only a necessity but also a way of life – it is obvious that this limitation is a strong one. Microsoft’s answer to this problem is “Web Services”. The idea goes something like this :

  1. The Web service provider develops a useful function(s), and publish/advertise it. The Web Service provider uses Web Service Description Language (WSDL) standard to describe the interface of the function(s). This is much like the Type Libraries (TLB) and Object Description Language files (ODL) that needs to be generated with the ActiveX automation servers.

  2. The programmer/client who needs the function does a lookup by using a process called – Web Service Discovery or SOAP Discovery (also called DISCO for Web Service DISCOvery)

  3. The Actual communication between the client program and the web service takes place through a protocol called Simple Object Access Protocol (SOAP) – SOAP is an XML based light weight protocol used for communication in a decentralized distributed environment.

As is evident from the above discussion that at the heart of all the communication is XML. Both SOAP, WSDL leverage on XML.

We have all either used or at least heard of  network communication protocols like RPC (Remote Procedure Call); RMI (Remote Method Invocation), CORBA, DCOM, IIOP. All these technologies have the same purpose – to enable calling a function/Object on a remote machine. So how is Web Service (or SOAP) any different than these existing technologies ? . The main difference is that SOAP uses HTTP/HTTPS protocol; unlike all the other technologies that uses specialized protocols for distributed communication. Microsoft, with this simplified approach has tried to bring sanity and unification to the world of distributed programming. Distributed applications are heavily dependent on JNDI lookups, RMI, CORBA, IIOP, Serializability and other intricacies. With Web Service and .NET development tools ; Microsoft have provided us with a much simpler way of developing distributed applications. So what is the catch ?. The obvious catch is that this is an ASP.NET specific technology (at least for now); but with time SOAP, WSDL, DISCO will most certainly gain wider acceptance.

According to Microsoft’s tests an application developed ASP.NET using ADO.NET and Web Services,  is many times more efficient than an equivalent application developed in JAVA, JSP, Servlets and  EJBs.

Note that .NET has no direct equivalent of EJBs. So considering Web Services as an equivalent to EJB will be in correct. However some of the functionality of an EJB can be provided by Web Services.

With .NET, Microsoft has followed one guiding principle – make it as simple as possible. And Web Services is no exception to this ideology. See the example below and judge for yourself as to how easy it is to develop a Web Service. And compare this with how “EASY ?” it was to develop an ActiveX automation server; or how “easy ?” it is to develop an EJB.

Web Service Example

Open any text editor and type in the following Visual Basic code, and save the file under “.asmx” extension.

Imports System
Imports System.Web.Services
Imports Microsoft.VisualBasic
Public Class HelloWorld : Inherits WebService
Public Function <WebMethod()> GreetTheUser(strUserName as String)  as String
Select Case strUserName
Case “Kashif Manzoor”
return “Hello Author”
Case “Amna Kashif”
return “Hello Ms. Amna Kashif”
Case “Ahmed Luqman”
return “Hello little Ahmed”
Case Else
return “Hello Stranger”
End Select
End Function
End Class

The first three lines import needed Classes. Import is similar to the import used in Java or the #include in C/C++. The rest of the code is self explanatory. Notice that the Class extends/inherits from the built in Web Service class; this is mandatory. Also notice that the Function is declared with the key word <WebMethod()> this indicates that this function can be invoked from the web across the Internet. You may add other private functions in your class but those functions will not be accessible to outside word.

So that’s it !!! you have successfully made your first  Web Service. Although the service simply takes in a name and returns a greeting; but it is enough to give you a flavor of Web Services. This Web Service can now be accessed from your ASP.NET code. This article does not intend to explain either ASP.NET or Web Services in details , the interested reader should consult ASP.NET manual or visit MSDN site for more details.

Deploy your “.asmx” file on Web Service aware Application Server like IIS. And open up a Web Service aware browser like IE. Type in the appropriate URL for the file. If the file is in the default web application folder then the URL would be “http://localhost/HelloWorld.asmx“. What do you think would happen ?. You will see a well formatted web page, giving you the details of the GreetTheUser() method. And at the bottom of the page you will be given an edit box, where you can enter the “strUserName” and then press a button beside the edit box. Once you do that, you will receive the greeting string as an XML document. This is a new and a wonderful feature.

Lets not be unfair with Sun’s technologies here. Making an EJB (at least a stateless and stateful EJB) is no more difficult than the above example. What makes EJBs tricky is the deployment, the JNDI lookups, the Stubs and the application servers that support EJBs. With Microsoft’s “click and ready to go” approach and easy to use utilities that come with Visual Studio.NET, deploying any of .NET application is extremely easy.

In conclusion Web Services is an evolutionary idea as opposed to being a revolutionary idea, its just an other distributed development tool – which happens to be  extremely simple to use. Incorporation of Web Services in ASP.NET, has taken ASP to a new level of respectability. Web Services has already started gaining popularity and is also incorporated in the Java platform. Visit http://java.sun.com to get the latest on Web Services support in Java platform.


Web Forms

Just as the Win Forms provide a unified way of developing GUI for desktop application, the Web Forms provide similar tool for web applications. Web Forms has been introduced in .NET as a part of ASP.NET. Web Forms are a forms engine, that provide a browser-based user interface.

To appreciate Web Forms you may consider how GUI is rendered in current Web applications. The GUI is rendered by using HTML tags. (e.g. <input type=text name=editbox1 maxlength=10 size=10 >, will draw an edit box on the web page) Web Forms can also have the intelligence to use HTML, DHTML,WML etc. to draw the controls on the web page based on the browser’s capabilities. Web Forms can also incorporate the logic behind these controls. Its like hooking up the code to a GUI control. Just like in your VB application, you can associate a code with a button on the web page, this code will be run on the server when the button is pressed. This is in contrast to the scripts that run on the clients when a button is pressed. This approach is different to the Java approach. In Java a programmer can simulate this functionality through JavaScript and Servlets. But with Web forms this is done transparently. A Java programmer may consider as if each HTML control has its dedicated “Servlet” running in the background. Every time the control receives any event of interest (e.g. button pressed, selection changed etc.) this specific “Servlet” is called. This results in much cleaner code and an excellent logic separation between presentation and business logic  layers.

Web Forms consist of two parts – a template, which contains HTML-based layout information for all the GUI elements and a Component which contains all the logic to be hooked to the controls or GUI elements. This provides a neat presentation layer and application logic layer separation.

The GUI will be rendered on the client side, while the code that has been hooked to the GUI elements will run on the server side (very much like a button being pressed on a JSP and a Servlet being called in response. But with Win Forms this has been made extremely easy). The incorporation of Web Forms in ASP.NET is an attempt to take ASP to a new level where it can seriously challenge JSP.

Another good feature of Web Forms is that it can be built to have enough intelligence to support a vast variety of browsers. The same ASP page would render itself using DHTML, if the browser is IE 5.5. but if  the browser is Netscape the web page  will be rendered using HTML tags; if the page is being accessed through a WAP device the same page will render itself using WML tags.

One of the obvious disadvantage of ASP over Java was that there was that an ASP code was a maintenance nightmare. While a Java programmer can use Java Beans, Tags and also Servlets to achieve presentation and business layer separation – no such mechanism was present to a ASP programmer. With ASP.NET Microsoft has provided such presentation-business layer separation – by introducing the concept of Web Forms:

  1. ASP.NET Web Forms provide an easy and powerful way to build dynamic Web UI.
  2. ASP.NET Web Forms pages can target any browser client (there are no script library or cookie requirements).
  3. ASP.NET Web Forms pages provide syntax compatibility with existing ASP pages.
  4. ASP.NET server controls provide an easy way to encapsulate common functionality.
  5. ASP.NET ships with 45 built-in server controls. Developers can also use controls built by third parties.
  6. ASP.NET templates provide an easy way to customize the look and feel of list server controls.
  7. ASP.NET validation controls provide an easy way to do declarative client or server data validation.

For those of you (like me) who turned to Java for web development mainly due to spaghetti code of ASP- ASP.NET is worth exploring. Since it introduces some exciting new ways to write clean code (Personally I find Web Forms an exciting new concept – that does not have a direct equivalence in Java platform)

Windows Forms

Windows forms (also called Win Forms) are used to create GUI for Windows desktop applications. The idea of Win Form has been borrowed from Windows Foundation Classes (WFC) which were used for Visual J++. Win Form provide an integrated and  unified way of developing GUI. It has a rich variety of Windows controls and user interface support.

Numerous classes and functions were used by programmers to handle GUI. MFC in VC++, direct API in C++ and VB Forms Engine in VB are just a few examples of different ways of handling GUI.

Simply put – Win Form is just another group of wrapper classes that deal specifically with GUI. Win Form classes encapsulate the Windows Graphical APIs. Now the programmers would not need to use the Windows Graphical APIs directly; and since Win Form has been made a part of .NET Class Framework; all the programming languages would use the same Win Form classes. This would rid the programmers of the need to learn different GUI classes/tools. Win Forms in the part of the namespace System.Winforms.

With Win Forms we can make a single user interface, and use it in VC++, VB, C#. Using Visual Studio.NET simply design the GUI, by dragging the controls on a form (something that all VC++ and VB programmers are well familiar with). Now you can use the same form either in VB, VC++ or in C#. And this is all made possible because Visual Studio.NET uses the  System.Winforms namespace to draw the GUI. And any language that has the appropriate CLS compliance can use this form directly.

Various .NET Languages – VB.Net, ASP.NET, C#, J#, VC.NET

Sun intended to present JVM – as a single language virtual Machine. Meaning that only a Java program can be converted to a byte code (.class file) and then presented to JVM, which interprets the programs and runs it on the host machine. Although In concept, any language can be compiled to Java Byte code and then fed to JVM; but Sun did not encourage such approaches. Despite Sun’s lack of initiative in this regard many researchers and companies have developed languages following this approach. Sun’s vision of Java being “One language fits all” has both its advocates and its critics[5]

With CLR, Microsoft has adopted a much liberal policy. Microsoft has themselves evolved/developed/modified many of their programming language to be compliant with .NET CLR.

Although Visual C++ (VC++) , has undergone changes to incorporate .NET; yet VC++ also maintain its status as being a platform dependent programming. Many new MFC classes have been added; a programmer can choose between using MFC and compiling the program into a platform specific executable file; or using .NET framework classes and compile into platform independent MISL file. A programmer can also specify (via directives) when ever he uses “un-safe” (the code that by passes CLR – e.g. the use of pointers) code.

ASP, is another language that has been improved markedly. Most programmers know that ASP did not measure upto JSP; Microsoft has tried to turned the tables by introducing ASP.NET. ASP.NET makes extensive use of Web-Services. Web-Services is an open standard and JSP can use Web-services (Sun’s official web site gives a detail on Web services and how that are being incorporated in Java platform). There are many other features that have been introduced in ASP.NET, to make it an ideal distributed programming tool and to measure up against JSP. ASP code within <% %> tag, is compiled into .NET Framework (similar to JSP code being compiled into a servlet). This approach is different than how the <% %> was handled in ASP ASP.NET has been enhanced by Microsoft.

Out of ALL .NET languages, Visual Basic.NET (VB.NET) is one language that has probably undergone the most changes. Now VB.NET may be considered a complete Object-Oriented Language (as opposed to its previous “Half Object Based and Half Object Oriented” status).

Microsoft has also developed a brand new programming language C# (C Sharp). This language makes full use of .NET. It is a pure object oriented language. A Java programmer may find most aspects of this language to be identical to Java. If you are a new comer to Microsoft Technologies – this language is the easiest way to get on the .NET band wagon. While VC++, and VB enthusiast would stick to VC.NET and VB.NET; they would probably increase their productivity by switching to C#. C# is developed to make full use of all the intricacies of .NET. The learning curve of C# for a Java programmer is minimal. Microsoft has also come up with a The Microsoft Java Language Conversion Assistant - which is a tool that automatically converts existing Java-language source code into C# for developers who want to move their existing applications to the Microsoft .NET Framework.

Microsoft has also developed J# (Java Sharp). C# may be similar to Java, but it is not entirely identical. It is for this reason that Microsoft has developed J# – the syntax of J# is identical to Visual J++. Microsoft’s growing legal battle with Sun over Visual J++ – forced Microsoft to discontinue Visual J++. So J# is Microsoft’s indirect continuation of Visual J++. It has been reported that porting a medium sized Visual J++ project, entirely to J# takes only a few days of effort.

Microsoft encourages third party vendors to make use of Visual Studio.Net (launched on Feb 13, 2002). Third party vendors can write compilers for different languages – that compile the language to MSIL (Microsoft Intermediate Language). These vendors need not develop their own development environment. They can easily use Visual Studio.NET as an IDE for their .NET compliant language. A vendor has already produced COBOL.NET that integrates with Visual Studio.NET and compiles into MSIL[3]. Theoretically it would then be possible to come up with Java compiler that compiles into MSIL instead of Java Byte code; and uses CLR instead of JVM. However Microsoft has not pursued this due to possible legal action by Sun.

h1

Unit 1- Introduction to .net framework

September 26, 2008

Introduction to .net framework

This is the first tutorial as part of the Visual Basic .NET 2005 Training. In this tutorials we will be learning about the basics of The .NET Framework Architecture, The .NET vision, Common Language Runtime (CLR), .NET Framework Class Library and Microsoft Intermediate Language (MSIL). It’s very important to learn the basics before we actually start with the Programming.

The .Net Vision

The .NET framework is a software technology that is directed towards connecting information, people, systems and devices seamlessly. The high level of software integration that has been attempted through use of XML web services enables the creation of small, discrete, building block types of applications. These applications are connected to other applications over the Internet.

Central to the .NET framework architecture is the effort to provide support to for the next generation solutions. The direction of change is to ensure that software deployment and versioning conflicts are avoided or minimized in the .NET Framework. User defined codes are released from the confines of the local storage systems and can be stored anywhere on a network and executed from any point in the network. Performance problems associated with scripted and interpreted environments are removed. Codes can be safely executed and the developer experience is consistent across widely varying types of applications, such as Windows based applications and web based applications. The thrust of design is towards creating a standard communication facility that is standardized so that .NET codes can be integrated with other codes easily and seamlessly.

At the core of the .NET infrastructure is the Common Language Runtime (CLR) and the .NET Framework class library.

The runtime functions, like an agent, managing code at execution time. It provides core services such as memory management, thread management and ‘remoting’. It enforces strict type safety and ensures code accuracy. This makes for security and robustness and provides a platform for running managed code.

The .NET Framework provides several runtime hosts. It also supports the development of third-party runtime hosts. For example, ASP.NET hosts the runtime to provide a scalable, server-side environment for managed code. ASP.NET works directly with the runtime to enable ASP.NET applications and XML Web services.

The .NET Framework can be hosted by unmanaged components such as the Internet Explorer. These load the common language runtime into their processes and initiate the execution of managed code. This creates a software environment that can exploit both managed and unmanaged features. This makes for mobility of the code.

Common Language Runtime (CLR)

The common language runtime (CLR) is the foundation upon which developers construct various kinds of applications. The benefits of the CLR are many. It makes for a vastly simplified development. It enables a seamless integration of code written in different languages. It provides evidence-based security with code identity. The assembly based deployment eliminates the problems of DLL. The versioning of reusable components makes for ease of usage. The implementation inheritance feature enables the reuse of code. The automatic object lifetime management makes the application- development comfortable. The self descriptive nature of objects makes for ease of programming and debugging.

CLR includes both private and shared components. It provides for incremental download of code and caching; native platform interoperability and seamless integration with COM. It offers dynamic inspection capabilities, administration and configuration options. The developer is not required to know all the runtime supported infrastructure. The tools and frameworks are designed to expose a subset of the functionality in accordance with the kind of application being built.

The common language runtime is a virtual execution system that provides important services such as memory management, security and also has a Just-in-Time compiler (JIT) which converts the intermediate language (IL) into native code that can be executed by the physical machine. The infrastructure provides a machine independent environment for application execution.

Tight security is maintained in implementing common language runtime. Managed components are awarded varying degrees of trust. Access rights are determined by factors such as origin of the component (Internet, enterprise network, or local computer). On the flip side this restricts access to file and registry operations, even if the file being operated upon is on within the same active application.

The managed environment of the runtime eliminates many common software issues. For example, the runtime automatically handles object layout and manages references to objects, releasing them when they are no longer being used. This automatic memory management resolves the two most common application errors–memory leaks and invalid memory references.

The futuristic design of the infrastructure is takes into consideration the need to integrate with the legacy applications that are still in operation.

Interoperability between managed and unmanaged code enables developers to continue to use necessary COM components and DLLs.

The use of ‘Common type’ system implements the features of the .NET infrastructure. The common data types shared by multiple languages such as VB.NET, C#, ASP.NET eases the import of a class created in one language into another. A common error handling facility makes for tighter integration between languages and allows developers the freedom of working in the language of their choice.

.NET Framework Class Library

The .NET framework class library, as the name suggests, is a library of classes, interfaces and value types. The applications, components and controls for applications are built on this framework and it provides the developer the access to the system functionality. In other words, the classes and structures can be leveraged as base building blocks for application development. These classes are often described as an API and form a boundary interface between the application and the operating system. Though the concept is not new to Visual basic Developers who have been using the ADO library, the Win 32 API and COM + services, it forms a massive code base on which the application can be built.

The .NET Framework class Library is organized into namespaces. The namespace is a container for functionality. Similar classes and constructs are grouped together in a namespace to define parent-child relationships. Namespaces can be nested into namespaces.

All namespaces stem from the root namespace called System Namespace. It contains all data types including the Object data type. Though all namespaces are subordinated to the System namespace, User defined libraries can also coexist with the System namespace. They can have their own root namespace which can be language focused namespaces such as Microsoft.Csharp, Microsoft.VisualBasic.

The most significant feature of the .NET framework is the class Library collection of reusable types can be integrated with CLR. The programmer can accomplish a range of common programming tasks, such as string management; data collection; data base connectivity and file access using the .NET framework class library. The developer can create console applications, Windows GUI applications, ASP.NET applications, XML Web services or Windows services.

Microsoft Intermediate Language (MSIL)

MSIL are simple binary structures that are similar to the instruction sets of the CPU. Like the instruction set, it encodes a wide spectrum of operations such as loading, storing, arithmetic and logical operations and control flow. The similarity to the machine code enables the code to be compiled quickly in the Just in time compiler. The type verification also becomes simpler because the DotNet binary contains tables of metadata. The metadata defines each type and its signatures and also the other data that the runtime locates and extracts from the file at the time of execution. The presence of the metadata makes the module self descriptive and eliminates the need for IDL files and header files. However, unlike the CPU instruction set, MSIL expresses object oriented concepts such as object creation, method invocation, property access and exception handling.

It is the Microsoft Intermediate Language (MSIL) which makes applications language independent. Applications may be created by programmers in any language of their choice—ASP.NET or VB.NET—in a machine on which the .NET framework has been installed. The compiler then, gives an output in the Microsoft Intermediate language. This language can be defined as a kind of assembly language at a higher level of abstraction. The language is designed in a manner that makes it possible to convert it into any kind of native machine code with ease. The CLR detects the Intermediate language and invokes the Just-in-time compiler to convert the language into machine code.

Just-In-Time (JIT) compilation

As stated above, the Just in time compiler is a facility that is invoked by the CLR to convert the IL into machine code. The .NET framework assemblies (*.dll or *.exe) files cannot be executed by the target processor unless it is converted into native code. When the Assembly is loaded the method calls are invoked for compilation into native code. Since this is done just when the method needs to execute, it is called Just in time compilation. When the JIT is invoked for the first time there is an overhead or performance penalty, since the assembly is converted into its Native code or image using the Native Image Generator (Ngen.exe). All subsequent calls will load faster as the Native image alone is invoked thereafter from the Native image cache.

Significantly, the IL can coexist with the machine code in the application. This feature also enables the developer modify the code or add to the code even when the application is actively deployed. This functionality provided by JIT makes the .NET framework an efficient, agile and powerful means of application development.

Assemblies, native assemblies, and the Global Assembly Cache (GAC)

The primary unit of deployment is the assembly. The assembly is used by the .NET CLR as the smallest unit of deployment; version control; security; type grouping and code reuse. An assembly will consist of a manifest and one or more modules or files. It can be defined as a logical DLL that contains a manifest, type metadata, MSIL and resources. Assemblies can be application private or shared. Private assemblies are restricted to use in a single application whereas shared assemblies can be used across applications. Private applications also reside within the application folder while shared assemblies reside in the common area– GAC(Global Assembly Cache). They must have a globally unique name.

The existence of the manifest within the assembly makes it self descriptive. It can be viewed by the IL Disassembler (Ildasm.exe) which is part of the .NET framework SDK.

The assembly is identified by the manifest. It defines security requirements, lists other interdependent assemblies and all the types and resources exposed by the assembly. Localized resources exposed by assemblies and targeted by the application contain a default culture (language, currency, date/time format etc).

The manifest contains several sections. Identity, Referenced Assemblies, file list and Custom Attributes are some of the important sections of the manifest. The Identity section as the name suggests, identifies the assembly. It contains a .assembly directive. The version directive specifies the version of the assembly, so that the CLR identifies the different versions of an application. The Identity section contains a strong name for shared assemblies and a public/private encryption key is used to distinguish between assemblies of the same name. The Identity section also optionally contains the Culture which defines the country and language of the assembly target. The .locale directive is used for this purpose and the Culture-Neutral assemblies can be used by any assembly.
The Referenced Assemblies section of the manifest provides a reference to all the assemblies used in an application.