top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Dynamic Loading Of Web User Controls - Architectural Aspect

0 votes
228 views

Background

.NET is Microsoft's most advanced programming model for building Web applications. This paper provides a solution for the application architects/designers on how to utilize web user controls effectively by loading them dynamically, while doing design for the complex web based applications.

Salient Features of this solution are:

  • Minimizing the development time
  • Maximum utilization of Reusable GUI components and Dynamic GUI generation.
  • Minimizing the maintenance time & Cost
  • Ease of change/enhancements with future requirements/Better control on the code

The focused technology area is .NET Web User Controls.

Traditional Approach

Suppose we are developing a complex web-based application with more number of pages. In this approach, the web pages are developed individually at the design time. The controls placed on the Page are specific to the page where it is placed and there is no concept of reusability.

The main concerns with this approach are

  • Development time is high � each page has to be specifically designed
  • Not much of code reuse � controls on each page specific to that only
  • Development cost is high � as each page developed separately
  • Control on the code is less � due to increase in number of pages
  • Maintenance time is high � due to increase in number of pages

This approach works well if we are developing web based application with few web pages.

Proposed Approach

The concerns in traditional approach while developing complex web applications with more number of pages can be overcome

  • By logically grouping the controls at the design time and the respective Web User Controls (generalized) are identified.
  • These web User Controls are loaded dynamically on the web Page at runtime (Dynamic GUI).
  • The position (X, Y co-ordinates), caption and other information about the web User control are retrieved from the database.
  • Also since the control�s information is coming from database localization can be easily implemented.

Proposed Architecture

Web User Control Architecture

image

  • Initially in design time the web page will have nothing except a container control and other buttons (Ex: Save button).
  • On Load of the each web page the respective web User Controls information that needs to be loaded are retrieved from the database (i.e. x-y coordinates, header etc).
  • Each Web User Control has its own Business Class, which intern interacts with the generic Data Access Class.
  • Each Web User Control has its own Save and Populate methods, which is backed by the respective business class having Insert, Update and GetData methods.
  • While loading the web User Controls all the individual web user controls Populate, SetPosition etc methods will be called in Loop.
  • To Save any changes done on the web form all the Save methods for each individual web User Controls need to be called in Loop.
  • The Save Button is on the Webform, not on individual web User Controls.
     

Example

Suppose we are developing a Health Care Product, we need to maintain the Doctor and Patient Information. If we logically group the scenario Name & Address, Appointment dates are common for both Doctor/Patient. In order to handle Name & Address for Doctor and Patient following generalized web User Control is identified image

In order to handle Appointment dates for Doctor and Patient following generalized web User Control is identified
image

Web User Controls on Doctor Information Page

When the web user control dynamically loaded on the Doctor Information web form, Headers of the generalized web user control are displayed as �Doctor Address� and �Doctor Appointment�. Also key information like �Doctor� is passed which will enable the web user controls to fetch and populate doctor specific data and populate the page accordingly.

Web User Control when Loaded on the Patient Information Page

When the web user control dynamically loaded on the Patient Information web form Headers of the generalized webuser control are passed as �Patient Address� and �Patient Appointment�. Also key information like �Patient� is passed which will enable the web user controls to fetch and populate patient specific data and populate the page accordingly.

Results

By using the proposed approach we are achieving

  • Minimizing the development time
  • Maximum Utilization of Reusable GUI components and Dynamic GUI generation.
  • Minimizing the maintenance time & Cost
  • Ease of change/enhancements with future requirements/Better control on the code

Conclusion

This approach is recommended for complex web application development, as there is more code reusability and better control over the code. This intern reduces the development and Maintenance time.

  • This solution is best suited for Migration Projects, where the similar concept is used in the existing system.
  • This architecture is ideally suited for complex web-based application (Products/Projects) with similar functionality in various web pages.
  • Logical grouping of controls should be done properly at the design time for better usage of this approach. This in turn requires a lot of Domain/Technical Expertise.
  • Even dynamic loading of web user controls takes place while loading the page; it will not affect much on the performance. The performance gain can be achieved by using caching technique.
posted Dec 29, 2016 by Shivaranjini

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

Introduction

Web services are also classes just like any other .NET classes. However, they typically have methods that are marked as web methods. In addition to web methods they can also contain normal methods.

Since a web service is a class it can utilize all the OO features like method overloading. In this article and demo we will see how to use method overloading in a web service.

Creating web methods

We will create a simple web service that has following three overloaded methods :

  • Public Function GetWelcomeMessage() As String
  • Public Function GetWelcomeMessage(ByVal name As String) As String
  • Public Function GetWelcomeMessage(ByVal name As String, ByVal dob As DateTime) As String

All the three methods return variants of a welcome message to the web service client. Next, we will mark two methods as web methods but keep last method as normal method of the class. To mark the methods as web methods just use following syntax :

VB.NET
<WebMethod()>
Public Function GetWelcomeMessage() As String
C#
[WebMethod()]
public string GetWelcomeMessage()
{...}

Compile the class and run it in the browser (I assume that you are using VS.NET). You should get a error saying that GetWelcomeMessage method must be marked with MessageName attribute. This is because even though our class can distinction between overloaded methods when we call them over SOAP there should be some unique identification for each one.

Adding MessageName attribute

To overcome the problem we will add MessageName attribute to the second web method as shown below :

VB.NET
<WebMethod(MessageName:="GetWelcomeMessageWithName")>
Public Function GetWelcomeMessage(ByVal name As String) As String
C#
[WebMethod(MessageName="GetWelcomeMessageWithName")]
Public Function GetWelcomeMessage(ByVal name As String) As String

Now, compile the service again and run in the browser. You will notice that you get GetWelcomeMessageWithName listed rather than GetWelcomeMessage for the second web method. In all your client code you will use this method name instead of original method name. Your typical client code will look like this :

'I assume that you have created web service proxy
'either by WSDL utility or adding a web reference
'in VS.NET
Dim x as new MyWebServices.Service1()
'call first web method
dim str1 as string=x.GetWelcomeMessage()
'now call overloaded version
dim str2 as string=x.GetWelcomeMessageWithName()

Note that in VB.NET MessageName attribute is represented by 'named argument'.

The third method is not marked as a web method and hence can be used just like any overloaded method inside the web service class.

READ MORE

Introduction

Web services are one of the building blocks of overall .NET architecture. This article explains the basics of ASP.NET web services along with examples.

What are Web Services?

To understand the meaning of term "Web Services" we will take analogous example form COM world. If you are programmer working with Microsoft Technologies, by this time you must have used third party components or at least components developed for your own applications. The COM components you developed provide "services" to your application. For example a component developed for a banking application might be providing services such as loan calculation, interest calculation etc. If you need same business logic at many places such component will be of great use. Components also isolate your business logic from the rest of the application. Web services offer similar functionality. Web services offer "services" over "web". The communication between your application and web service is via HTTP protocol using SOAP standards (Simple Object Access Protocol).

How web services work?

In simple terms it works as follows :

  • Your application requests a service (or a method) from a Web Service 
  • Your request is converted into SOAP format (which is nothing but XML) and routed to web service over web
  • The web service processes your request and sends back the result again in SOAP format

For routing your method calls to actual web service a special proxy (which is nothing  but a dll) is used. Following figure will make it clear :

Following points need to be noted about web services :

  • Web services are written as plain text files with extention .asmx
  • You generate a proxy source code for this .asmx file first into VB or C# source code using a utility provided with .NET (more on that later)
  • You then compile the source code into a DLL file (actual proxy dll) which makes the "Web Service" component
  • You can consume a web service from any type of application that supports HTTP e.g. Console, WinForms and WebForms or even plain old ASP.

Web Service Description Language

Web Service description language or WSDL is nothing but an XML document describing the web service. It is analogous to Interface Definition Language (IDL) or Type Libraries (TLB). It gives details like service method names, their parameter data types, return values etc.

Creating Your First Web Service

In this section we will create a simple web service called GreetingService. This web service will provide various greeting messages. You will need to pass an integer indicating the type of message you want e.g. Diwali Greetings, New Year Greetings etc. Following are the steps involved in creating a web service :

  • Create .asmx file containing source code for the service ( GreetingService.asmx in our case)
  • Convert .asmx file into VB or C# source code file
  • Compile VB or C# source code thus formed to obtain a proxy dll
  • Deploy the resulting DLL to the client applications bin folder

Create GreetingService.asmx file containing source code for the service

<%@ webservice language= "VB" class= "Greetings"%>Imports

System.Web.Services Public

Class Greetings <WebMethod()>Public
	Function GetMsg(id as integer) As String
		Select case id
		case 101
			GetMsg="Happy Diwali"
		case 102
			GetMsg="Happy New Year"
		case else
			GetMsg="Seasons Greetings"
		End Select
	End Function
End Class    

Let us dissect the code

  • Web service files have extention .asmx. In our case we created a file called GreetingService.asmx
  • <%@ webservice language="VB" class="Greetings"%>
    The webservice directive indicates that this is a web service. Language attribute specifies the language used to write the web service. We have used VB in our example. The class attribute specifies the class name which constitutes the web service. One .asmx file can have multiple class files (may be supporting classes). Out of available classes the class specified in the class attribute is considered for creating web service.
  • <WebMethod()>public function GetMsg(id as integer) as string
    This line marks GetMsg() method as "web callable" method using special attribute <WebMethod()>. This method returns the greeting message based on id we pass.

You can add as many web methods you want to the web service. You can also add normal methods i.e. methods without <WebMethod()> attribute. Such methods will not be exposed over web but can be used internal to the web service. You can also have more than one class per ASMX file. Typically, such classes will act as support class for the main class.

Convert .asmx file into VB or C# source code file

Now that we have our web service source code file ready, we need to convert it into VB source code. To do this .NET comes with a utility called WSDL.EXE. Type in the following command at the DOS prompt :

WSDL http://localhost/mywebapp/GreetingService.asmx?wsdl /l:VB /n:GreetingService

Here,

  • http://localhost/mywebapp/GreetingService.asmx?wsdl  We need to supply the WSDL of the web service. This can be achieved by appending WSDL to the path of our .asmx file. You can even invoke this URL in browser and view the resulting XML code.
  • /l:VB Specifies that the resulting code should be generated in VB
  • /n:GreetingService Indicates that name for the resulting namespace should be GreetingService

After executing above command you will get a file called Greetings.vb and it contains the source code for the proxy dll. Do not bother much about the code contained in this file since you will not be generally modifying it manually.

Compile VB or C# source code to obtain proxy DLL

Now we will compile the VB source code to get the proxy DLL.

Type in following Command

vbc
/out:GreetingService.dll
/t:library Greetings.vb
/r:system.dll
/r:system.xml.dll
/r:system.web.services.dll

You will get GreetingService.DLL.

Deploy the resulting DLL

In order to use the DLL you created you must copy it in the \bin folder of your web application.
If such folder is already not there you need to create it

Note : Here, we are assuming that we want to consume our web service from a web client developed in ASP.NET

Creating proxy dll using visual studio.nET

VS.NET provides very easy way to create this proxy dll. It almost hides all the complexities of the process. In your project you can simply right click on references and select "Add a web reference..." . Now select the web service and that's it ! VS.NET will automatically create a proxy for you and place in your web application's bin directory.

Testing our service

You can now test the web service you created by using it in an ASP.NET page. You might use following code :

<%@ language="VB"%>
<script runat=server>

public sub showmsg(s as Object,e as EventArgs)
	dim c as GreetingService.Greetings
	c=new GreetingService.Greetings()
	label1.text=c.GetMsg(101)
end sub

</script>

<form runat=server>
<asp:label id=label1 runat=server text="no msg set yet"/>
<asp:button id=btn1 runat=server onclick="showmsg" text="Click" />
</form>    

You can create instances of the web service component just like any other object.

Web services and session state

Just like any ASP.NET application web services can hold session or application state. However, there are some special things to be done.

  • Your web service class must derive from WebService. Our class used above is not derived from WebService class and hence can not use session state.
  • You need to mark each method that wants to access session state with following attribute :
    <WebMethod(EnableSessionState=true)>
    The EnableSessionState parameter tells the web service that we want to access session state inside the method.
READ MORE
...