An assembly in ASP.NET is a collection of single-file or multiple files. The assembly that has more than one file contains either a dynamic link library (DLL) or an EXE file. The assembly also contains metadata that is known as assembly manifest. The assembly manifest contains data about the versioning requirements of the assembly, author name of the assembly, the security requirements that the assembly requires to run, and the various files that form part of the assembly.
The biggest advantage of using ASP.NET Assemblies is that developers can create applications without interfering with other applications on the system. When the developer creates an application that requires an assembly that assembly will not affect other applications. The assembly used for one application is not applied to another application. However one assembly can be shared with other applications. In this case the assembly has to be placed in the bin directory of the application that uses it.
This is in contrast to DLL in the past. Earlier developers used to share libraries of code through DLL. To use the DLL that is developed by another developer for another application, you have to register that DLL in your machine. In ASP.NET, the assembly is created by default whenever you build a DLL. You can check the details of the manifest of the assembly by using classes located in the System.Reflection namespace.
Thus you can create two types of ASP.NET Assemblies in ASP.NET: private ASP.NET Assemblies and shared assemblies. Private ASP.NET Assemblies are created whey you build component files like DLLs that can be applied to one application. Shared ASP.NET Assemblies are created when you want to share the component files across multiple applications. Shared ASP.NET Assemblies must have a unique name and must be placed in Global Assembly Cache (GAC). The GAC is located in the Assembly directory in WinNT. You can view both the manifest and the IL using ILDisassembler (ildasm.exe).
.NET Assemblies - I
This article is going to be based on three parts.
- In the first part we will be discussing the 3-tier architecture.
- In part-II of this article we will see what the .NET assemblies are and
- In the last part we will be practically implementing a simple .NET assembly.
Part-I :: The Three-Tier Architecture
A 3-tier architecture defines the division of a web based application into three layers. When we say 3-tier architecture, we are actually meaning the number of nodes (or computers) that are involved within the communication. Let's first of all see what do I mean when I say a 3-tier architecture or a n-tier architecture.
For example, consider a simple scenario in which we have a client/server architecture. A user simply connects with the server and communication starts. The server fulfills all the requirements of the client as all of the contents have been placed on the server. This type of architecture is called as a client/server architecture or it can also be said as a two-tier architecture.
In a two-tier architecture the server contains all the contents that would be requested from the client, the contents can include web pages, the server-side technology implemented and the data-store. If we separate the contents of the server-side technology from the web pages and place them on a separate machine then this architecture would become a three-tier architecture. Similarly if we keep on increasing the number of machines in between our client/server communication then we would be implementing the n-tier architecture for our communication.
Why do we need the three-tier architecture?
The three-tier architecture was introduced for better management of code and contents and to improve the performance of the web based applications. Within the three-tier architecture we divide our application into a set of three layers.
- Presentation
- Business Logic
- Database
- The first layer Presentation contains the interface code, that is going to be displayed to the user. This code could contain any technology that can be used on the client side like HTML, JavaScript or VBScript etc.
- The second layer Business Logic contains all the code of the server-side technology. This layer mainly contains the code that is used for accessing the database and to query, manipulate, pass data to user interface and handle any input from the UI as well.
- The third and last layer Data represents the data store like MS Access, SQL Server, an XML file, an Excel file or even a text file containing data.
The following diagram shows the layers we have just described.

(Figure Displaying the layers of the three-tier architecture)
One of the main benefits of the three-tier architecture is easy management of the contents of the web application. Consider a scenario in which you require to change the presentation of the web pages because you have recently designed a new look for your website. In the case of two-tier architecture you would require to change the web pages that would be containing both the contents for the presentation as well as the business logic, this would mean an headache for you to keep the code for business logic as it is and change the code for the presentation layer. In case of a three-tier architecture the code of the business logic would be residing on the middle layer, there for you would be requiring only to make a change on the presentation layer containing the code for your web page designs.Similarly if you require to change the database system for example, from Access to Sql Server, then you would require only to change the database on the data server without having to make any changes within the code of your business logics.
The three-tier application design also enables for better performing web applications, since the whole load of users is not placed upon a single server. As we can see in the figure displaying the design of a three-tier architectured web application, it can be seen that placing the business logic and the database on the different machines reduces the load upon a single server. Similarly we can implement any number of servers we like on the backend of the application and such an architecture would be known as n-tier architecture.
Part-II :: Introduction to .NET Assemblies
Part-III :: Implementing a simple .NET Assembly
Part-II :: Introduction to .NET Assemblies
ASP .NET introduces many techniques for better code management and code reusability. Code Behind and Custom Controls are one of the best implementations of these techniques where you can separate your HTML tags and coding from your server-side script. It also provides future facilities to manage the code easily.
The Code Behind and Custom Controls development is a good approach to better web development, but there is a performance hit that also exists. You may know about the CLR (Common Language Runtime), I will not go into the detail of this, but just for a brief introduction, it is the "black box" or the "heart" of the .NET where the compilation of your code takes place. Whenever an ASP .NET page is opened or requested by the user, the page gets compiled first and then is transferred to the user. The compilation process is also completed in two steps. First of all an IL (Intermediate Language) is generated and then this is handed over for JIT (Just In Time) compilation which produces the machine code and our pages get displayed with their dynamic content.
The performance of our web application can be improved by creating pre-compiled libraries of IL which can then be handed over to JIT directly without having the inclusion of an extra process to make the conversion. This method is called componentization. Components are pre-compiled set of classes that have been developed in the form of DLL files and can then be included within our projects. This is also known as an assembly.
"An assembly is a logical grouping of functionality in a physical file."
Understanding the assemblies and their concepts is the focus of this article. An assembly has many benefits some of which are.
- Increased performance.
- Better code management and encapsulation.
- Introduces the n-tier concepts and business logic.
You might have heard of the n-tier and three-tier architecture concepts. (To know about the detail of 3-tier architectural concepts please read Part-I of this article.). The practical implementation of the three-tier architecture is also implemented by using the assemblies. In the first part of this article we discussed about the Business Logic layer. This layer is where we place all the business rules that apply to the transactions that take place between the client and the server, and this layer is implemented through components. The reason for this is that one we hide our code and the business rules that have been applied (since the components are in compiled binary form, so they are a mean of code encapsulation) and second because they are more efficient (since the code is already transferred into IL therefore only the JIT compilation takes place instead of both the IL and JIT).
Part-III :: Implementing a simple .NET Assembly
This part of the article is going to be the last of the series. In the last part I introduced you to what the .NET Assemblies are and for what reasons are they used? You also were informed about the advantages of using the .NET Assemblies as compared to the other scripting techniques that we have available in the ASP .NET.
Before implementing an assembly I would like to add, that we implement many pre-built assemblies while we are working on ASP .NET based applications. For example; the System namespace and other namespaces like it i.e. System.Data, System.Web are also pre-built assemblies that have been provided in the .NET framwork to us by Microsoft. In addition to this they have also provided us the capability of making our own assemblies. As compared to the classic ASP making components in form of assemblies in ASP .NET is much more easier and flexible.
Now let's go ahead with creating our first component. This is going to be a simple component that would just provide us with a few options to display a message on the screen. Write the following code in Notepad or any other editor that you have.
Imports System Namespace HRM.Display Public Class DisplayConsole Public Function Welcome() As String Return "Welcome to your first component!" End Function Public Function Output(strParam As String) As String Return strParam End Function End Class End Namespace |
Save this file by the name "hrmDisplay.vb".
Now for the process of converting this file into a compiled DLL file, we require to make a batch file. Again open the Notepad and add the following to it. Save the following file by name "MakeComponent.bat"
set indir=c:\inetpub\wwwroot\hrmDisplay.vb set outdir=c:\inetpub\wwwroot\bin\hrmDisplay.dll set assemblies=System.dll vbc /t:library /out:%outdir% %indir% /r:%assemblies% |
Before running the file you require to create a directory by the name of bin. Create a directory in your current directory by name of bin and then run the file. Checkout the bin directory your DLL file would be placed there after its creation.
Before using the component we require to do one more thing! and that is to make a web.config file. Web.config is an XML based file that specifies important configuration for an ASP .NET application. Again open up the Notepad and write the following code.
configuration>
system.web>
sessionState timeout="10" />
compilation>
assemblies>
add assembly="hrmDisplay" />
/assemblies>
/compilation>
/system.web>
/configuration>
Save this file as "web.config". The reason that we made the bin directory and then placed the DLL file within it was that the web.config would always search for the file in the bin directory, not only this assembly, but other user defined and third party assemblies also require there references to be added into the web.config.system.web>
sessionState timeout="10" />
compilation>
assemblies>
add assembly="hrmDisplay" />
/assemblies>
/compilation>
/system.web>
/configuration>
Now we are ready to use our component within our ASP .NET page. Open up Notepad or any other editor and add the following code into the file and save it as "Test.aspx".
<%@ Import Namespace = "HRM.Display" %>
<%@ Page Language = "VB" %>
Script Language="VB" Runat="Server">
Sub Page_Load(Source As Object, Sender As EventArgs)
Dim objComponent As New DisplayConsole
Sub Page_Load(Source As Object, Sender As EventArgs)
Dim objComponent As New DisplayConsole
Response.Write (objComponent.Welcome())
Response.Write (objComponent.Output("This is my message!"))
Response.Write (objComponent.Output("This is my message!"))
End Sub
/Script>
Now open your page in Internet Explorer or any other browser through your server and see your component in action.
0 comments:
Post a Comment